0

I have a rails app with 3 models something like:

has_many_through

I'm trying to create a form to edit the join table (eg. change the appointment_date in the example model):

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 %>

The problem is that this renders a page with the following 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">

The problem is the action. It's should look like this:

/active_admin/organizations/:organization_id/organization_membership/organization_membership_id

But, instead of :organization_id, formtastic is putting in the organization_membership_id. I suspect that my problem is in the top line <%= semantic_form_for [:active_admin, :organization, @organization_membership].

EDIT: I've managed to get this "working" by extending the update() method with the following ugly hack:

def update
    params[:organization_id] = params[:organization_membership][:organization_id]                                                                            
    super
end

Still searching for a real solution to this problem.

4

1 回答 1

0

你是对的,这里的问题在于以下行

“<%= 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 文档中找到有关它的更多信息。(在使用部分下查找嵌套资源。)

于 2017-12-29T10:02:23.297 回答