Search code examples
ruby-on-railssimple-form

Passing params to a simpleforms f.association in rails


I have many equipments that belongs to an Office

From the list of Offices, I put this link to pass the id to the form of a new equipment
<%= link_to "", new_equipment_path(office: params[:id])" %>

The generated url appears to be ok, passes the correct id of the clicked office
/equipments/new?office=6

And I check with an open cell like this and I see the number id (6 for example)
<%= f.input :observations, input_html: { value: params[:office]} %>

The problem is when I try with the real cell that is f.association, is just in blank, manually I can pick a number (1,2,3,4,5,6, etc) but It's not putting the number that I pass in the params, I try as hidden and other options and no luck
<%= f.association :office, label_method: :id, value_method: :id, input_html: { value: params[:office] } %>

#show.html.erb (offices)

<% @offices.each do |office| %>
    <tr>
        <td><%= office.name %></td>
        <td><%= link_to "Add new equipment", new_equipment_path(office:params[:id])%></td>
    </tr>
<% end %>

form.html.erb (new equipment)

<%= simple_form_for(@equipment) do |f| %>
    <%= f.association :office, label_method: :id, value_method: :id, input_html: { value: params[:office] } %>
    <%= f.input :observations, input_html: { value: params[:office]}%>
<% end %>

Solution

  • First I try without simpleforms and work without a problem

    <%= form.hidden_field :office_id, value: params['office'] %>
    

    The problem was that simpleforms does not take input_html: { value: params[:office] in f.association, I change for this and it work

    <%= f.input :office_id, value_method: :id, label_method: :id, input_html: { value: params[:office] }, as: :hidden %>