gpt4 book ai didi

ruby-on-rails-3 - ActiveAdmin - 将本地人传递给表单

转载 作者:行者123 更新时间:2023-12-04 02:51:05 26 4
gpt4 key购买 nike

在我的 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_forsemantic_form_foractive_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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com