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.