我有一个带有3个型号的rails应用程序,例如:
我正在尝试创建一个表单来编辑连接表(例如,更改示例模型中的appointment_date):
1 <%= semantic_form_for [:active_admin, :organization, @organization_membership] do |f| %>
2 <%= f.inputs "Organization" do %>
3 <h2><%= @organization_membership.organization.name %></h2>
4 <% @organization_membership.organization.organization_memberships.each do |organization_member| %>
5 <%= organization_member.user.name %></br>
6 <% end %>
7 <% end %>
8 <%= f.inputs "New Member" do %>
9 <span id="new-member"></span>
10 <% if !resource.user.nil? %>
11 <h3><%= resource.user.login %></h3>
12 <% end %>
13 <%= f.input :user_id, :as => :hidden, :input_html => { :id => 'user-id' } %>
14 <%= f.input :organization_id, :as => :hidden, :value => @organization_membership.organization.id %>
15 <%= f.input :start_date, :as => :date_picker %>
16 <%= f.input :end_date, :as => :date_picker %>
17 <% end %>
18 <%= f.actions %>
19 <% end %>
问题是这会呈现一个包含以下html的页面:
<form accept-charset="UTF-8"
action ="/active_admin/organizations/8/organization_memberships/8"
class ="formtastic organization_membership"
id ="edit_organization_membership_8"
method ="post"
novalidate ="novalidate">
问题是action
。它应该是这样的:
/active_admin/organizations/:organization_id/organization_membership/organization_membership_id
但是,而不是:organization_id
,formtastic正在加入organization_membership_id
。我怀疑我的问题是在顶线<%= semantic_form_for [:active_admin, :organization, @organization_membership]
。
编辑:我已经设法通过扩展update()
方法与以下丑陋的hack得到这个“工作”:
def update
params[:organization_id] = params[:organization_membership][:organization_id]
super
end
仍在寻找这个问题的真正解决方案。
你是对的,问题在于以下几行
“<%= semantic_form_for [:active_admin,:organization,@organization_membership]”
对于嵌套资源,您需要显式传递组织对象。例如:
def edit
@organization = Organization.find(id)
end
然后在视图中,您需要稍作修正:
<%= semantic_form_for [:active_admin, @organization , @organization_membership] do |f| %>
您必须将组织作为资源而不是符号传递。您可以在formtastic文档中找到更多相关信息。(在使用部分下查找嵌套资源。)