Question

I have two forms on one view page. One of these is an update form. The other form is a single button that goes to the same controller action but does not actually update the form. Here is the first form:

<%= form_for [@project, @schedule] do |f| %>
    <%= f.fields_for :tasks do |builder| %>
        <%= render 'task_fields', :f => builder %>
    <% end %>
    <p><%= link_to_add_fields "Add task", f, :tasks %>
    <p><%= f.submit "Submit" %></p>
<% end %>

And here is the second form:

<%= form_tag project_schedule_path(@project, @schedule), method: "patch" do %>
         <div><%= hidden_field_tag :emp_accepts, true  %></div>
          <%= submit_tag "Accept schedule", class: "btn btn-large btn-primary" %>
<% end %>

The problem is the strong parameters. The strong parameters require :schedule, which only the first form provides. So when I try to use the second form, an error is returned. Here are the strong params:

def schedule_params
      params.require(:schedule).permit(:emp_accepts, 
        tasks_attributes: [:title, :content, :_destroy])    
end
Was it helpful?

Solution

you should change the second form to

<%= form_for [@project, @schedule], http: { method: 'patch' } do |f| %>
  <div><%= f.hidden_field :emp_accepts, value: true %></div>
  <%= f.submit 'Accept schedule', class: 'btn btn-large btn-primary' %>
<% end %>

OTHER TIPS

It looks like you should really use different controller methods for each action. It would make it easy for you to make changes in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top