在我的 photos.rb
文件中我有:
ActiveAdmin.register Photo do
form :partial => 'admin/forms/photo', :locals => { :events => Event.all }
end
在我的 _photo.html.erb
文件中,我希望能够访问事件的值,但它似乎无法检测到。我该怎么做?
根据评论中的要求,这是我的表格:
<% if events.any? %>
<%= form_for [:admin, @photo], :validate => true do |f| %>
<!-- insert a bunch of standard field divs here -->
<div class="actions" style="margin-top: 20px">
<% if @photo.new_record? %>
<%= f.submit 'Create photo' %>
<% else %>
<%= f.submit 'Update photo' %>
<% end %>
</div>
<% end %>
<% else %>
<div style="font-size: 14px">
You cannot add photos until you have added at least one event.
</div>
<% end %>
我收到的错误消息围绕 events.any?
行:
Completed 500 Internal Server Error in 108ms
ActionView::Template::Error (undefined local variable or method `events' for #<#<Class:0x007fdb8e841b80>:0x007fdb8a93e7a8>)
最佳答案
form do |f|
f.render partial: 'admin/forms/photo', locals: { f: f, events: Event.all }
end
请注意,您需要从您的部分中删除 form_for
、semantic_form_for
、active_admin_form_for
等,因为它将被 form do
部分在admin/photos.rb
中,否则会出现两个嵌套的form。
部分示例:
# app/views/admin/forms/_photo.html.arb
if events.any?
f.inputs do
f.input :title, label: 'Etc'
f.input :file
end
f.actions
else
f.div 'You cannot add photos until you have added at least one event.',
style: 'font-size: 14px'
end
关于ruby-on-rails-3 - ActiveAdmin - 将本地人传递给表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17645412/