我有一個擁有許多「優先級」的父「帳戶」。在Ruby on Rails中更新子協會表格
我可以很容易地爲這些帳戶創建新的優先級,但是一旦創建它們,我就無法編輯/更新它們。
Account模型(父):
class Account < ApplicationRecord
has_many :priorities
accepts_nested_attributes_for :priorities
end
優先模式(子):
class Priority < ApplicationRecord
belongs_to :account
end
路線:
resources :accounts do
resources :priorities
end
priorities_controller.rb(只是編輯,更新和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
最後,我edit.html.erb(所以這最終被帳戶/#/優先級/#/編輯)
<%= 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 %>
一切進展得很好,直到這一點。表單完美地獲取正確優先級的表單數據,如果您嘗試輸入與該帳戶ID無關的優先級ID,它甚至會失敗。然而,當我點擊「更新優先級」,我得到:
「動作‘更新’無法找到AccountsController」
現在,我可以按照錯誤,並創建該控制器的更新,但我認爲它甚至不應該試圖觸發AccountsController,它似乎應該試圖使用優先級控制器。
事實上,如果我檢查控制檯,請求似乎是怎麼回事: 請求URL:http://127.0.0.1:3000/accounts/2
對不起,我已經尋找至少10小時爲回答這個問題,並能找不到它。謝謝你的幫助。
可能重複[form \ _for with nested resources](https://stackoverflow.com/questions/2034700/form-for-with-nested-resources) – jvillian