1

I have a parent "Accounts" that has many "Priorities."

I can very easily create new priorities for these accounts, but I can't edit/update them once I've created them.

Account model (parent):

class Account < ApplicationRecord
  has_many :priorities
  accepts_nested_attributes_for :priorities
end

Priorities model (child):

class Priority < ApplicationRecord
  belongs_to :account
end

Routes:

  resources :accounts do
    resources :priorities
  end

priorities_controller.rb (just the edit, update, and params)

class PrioritiesController < ApplicationController

  def edit
    @account = Account.find(params[:account_id])
    @priority = @account.priorities.find(params[:id])
  end

  def update
    @account = Account.find(params[:account_id])
    @priority = @account.priorities.update(priority_params)

  end

  private
    def priority_params
      params.require(:priority).permit(:name, :narrative, :kpis)
    end

end

and finally, my edit.html.erb (so this ends up being accounts/#/priorities/#/edit)

<%= form_for(@account) do |a| %>

<%= a.fields_for :priorities, @priority do |p| %>
  <p>
    <%= p.label :name %><br>
    <%= p.text_field :name %>
  </p>

  <p>
    <%= p.label :narrative %><br>
    <%= p.text_area :narrative %>
  </p>

  <p>
    <%= p.label :kpis, "KPIs" %><br>
    <%= p.text_field :kpis %>
  </p>

  <p>
    <%= p.submit %>
  </p>
<% end %>
<% end %>

Everything goes really well up until this point. The form perfectly gets the form data for the correct priority, and it even fails if you try to enter a priority ID that's not associated with that account id. HOWEVER, when I click "Update Priority" I get:

"The action 'update' could not be found for AccountsController"

Now, I can just follow the error and create an update for this controller, but I don't think it should even be trying to trigger the AccountsController, it seems like it should be trying to use the priorities controller.

Indeed if I check the console, the request seems to be going here: Request URL:http://127.0.0.1:3000/accounts/2

I'm sorry, I've search for at least 10 hours for the answer to this question and can't find it. Thanks for your help.

Jake
  • 43
  • 3
  • Possible duplicate of [form\_for with nested resources](https://stackoverflow.com/questions/2034700/form-for-with-nested-resources) – jvillian Jul 18 '17 at 02:02

1 Answers1

0

As discussed in this answer and this answer, I believe you need to do something like:

<%= form_for [@account, @priority] do |f| %>
  blah blah
<% end %>

(Fair attribution: Lifted directly from second linked answer.)

As you point out, this:

    <%= form_for(@account) do |a| %>

Is going to generate a URL for the update action on the AccountsController. Because, that's exactly how rails works. If you want a URL to the nested resource, you need to include both instance variables in the form_for declaration.

jvillian
  • 19,953
  • 5
  • 31
  • 44
  • Ok, so this seems to be the right direction, however when I tested the update, it changed ALL priorities within that account - so I must have something wrong with my controller. Thank you. – Jake Jul 18 '17 at 10:47
  • Yes, I changed the priorities update controller and it worked. Thank you so much! – Jake Jul 18 '17 at 10:54