【发布时间】:2020-05-03 03:09:23
【问题描述】:
我正在尝试将我的 XslSheet 模型的 data_file_names 列表放入我的 js.erb 文件中的 select 下拉列表中。
我在 input.js.erb 中尝试了以下内容:
inputEx.registerType("button", inputEx.ButtonField, [
{
type: "select",
label: I18n.t('form.field.xsl'),
name: "xsl",
choices: <% if User.current_user %>
<% if User.current_user.current_scope['ab'] %>
<%= XslSheet.where(:assetable_id=>User.current_user.current_scope['ab']).pluck(:data_file_name) %>
<% end %>
<% end %>,
required: false
}], true);
我也试过把它放在一个部分并渲染它:
input.js.erb:
inputEx.registerType("button", inputEx.ButtonField, [
{
type: "select",
label: I18n.t('form.field.xsl'),
name: "xsl",
choices: <%= Erubis::Eruby.new(File.read(File.join(Rails.root, 'app/views/admin/forms', '_xsl_sheet_names.html.erb'))).result(period: @period) %>,
required: false
}], true);
_xsl_sheet_names.html.erb:
<% if User.current_user %>
<% if User.current_user.current_scope['ab'] %>
<%= XslSheet.where(:assetable_id=>User.current_user.current_scope['ab']).pluck(:data_file_name) %>
<% end %>
<% end %>
由于我的 input.js.erb 位于 assets 文件夹中,因此需要使用 Erubis 来渲染部分内容。
这些方法都不起作用。我需要清除 tmp/cache 文件夹才能在选择中查看更新的值。 清除缓存并重新加载页面后,我可以看到更新的值。
还有其他方法可以尝试吗?
编辑:
现在尝试在 XslSheet 模型中创建一个方法并在 input.js.erb 中调用它。
xsl_sheet.rb
def xsl_names
if User.current_user
if User.current_user.current_scope['Application']
x = XslSheet.where(:assetable_id=>User.current_user.current_scope['Application']).pluck(:data_file_name)
x
end
end
end
input.js.erb
choices: <% x = XslSheet.new %>
<%= x.xsl_names %>,
这也需要在显示更新值之前清除缓存
【问题讨论】:
-
input.js.erb 位于何处?如果它在
app/assets中,则意味着它将成为资产管道的一部分,它应该只包含静态资产。不是针对每个请求而更改的资产。 -
@3limin4t0r 是的,它在应用程序/资产中。我不确定如果没有这个文件,我还能如何操作选择的内容!
标签: javascript ruby-on-rails ruby asset-pipeline erb