Rails 带有引用 ID 的回形针插入查询
Rails Paperclip interpolate query with reference id
我正在尝试从 Paperclip 插值系统中的另一个 table 检索字段。
到目前为止,当我使用像 3
这样的现有参考编号时它可以工作,但是当我尝试使用 container_id
参考字段时它不会工作,即使它保存在数据库中.
Rails 显示此错误:
undefined local variable or method `container_id' for Paperclip::Interpolations:Module
形式
<%= form_for @upload, :remote => true, :authenticity_token => true do |f| %>
<p>
<%= f.label :file %><br />
<%= f.file_field :file %>
<%= f.hidden_field :container_id, value: @page.container.id %>
</p>
<p><%= f.submit "submit"%></p>
<% end %>
模特
Paperclip.interpolates('container') do |attachment, style|
attachment.instance.get_container(container_id).to_s
end
def get_container(container_id)
return Container.find(container_id).url
end
有没有办法查询另一个 table 并使用带有回形针插值的字段?
那是因为container_id不在你使用的方法范围内(插值)
您可以访问的是 attachment.instance。
如果@upload 是一个模型,我建议您添加 container_id 作为一列,然后您可以这样做:
Paperclip.interpolates('container') do |attachment, style|
attachment.instance.container.url.to_s
end
我不知道是不是因为你需要提供更多信息,比如涉及的模型和关联。像容器 class
希望对您有所帮助
我正在尝试从 Paperclip 插值系统中的另一个 table 检索字段。
到目前为止,当我使用像 3
这样的现有参考编号时它可以工作,但是当我尝试使用 container_id
参考字段时它不会工作,即使它保存在数据库中.
Rails 显示此错误:
undefined local variable or method `container_id' for Paperclip::Interpolations:Module
形式
<%= form_for @upload, :remote => true, :authenticity_token => true do |f| %>
<p>
<%= f.label :file %><br />
<%= f.file_field :file %>
<%= f.hidden_field :container_id, value: @page.container.id %>
</p>
<p><%= f.submit "submit"%></p>
<% end %>
模特
Paperclip.interpolates('container') do |attachment, style|
attachment.instance.get_container(container_id).to_s
end
def get_container(container_id)
return Container.find(container_id).url
end
有没有办法查询另一个 table 并使用带有回形针插值的字段?
那是因为container_id不在你使用的方法范围内(插值)
您可以访问的是 attachment.instance。
如果@upload 是一个模型,我建议您添加 container_id 作为一列,然后您可以这样做:
Paperclip.interpolates('container') do |attachment, style|
attachment.instance.container.url.to_s
end
我不知道是不是因为你需要提供更多信息,比如涉及的模型和关联。像容器 class
希望对您有所帮助