提问人:Jan 提问时间:4/2/2023 更新时间:4/2/2023 访问量:38
Rails 嵌套资源 - 缺少必需的键:[:entry_id]
Rails nested ressources - missing required keys: [:entry_id]
问:
我有两个模型:
class Credit < ApplicationRecord
belongs_to :user
has_many :entries, -> { order(position: :asc) }, dependent: :destroy
end
class Entry < ApplicationRecord
belongs_to :credit
acts_as_list scope: :credit
end
使用 acts_as_list gem,我想更新信用视图中条目的顺序 (entry.position)。
我正在使用以下设置:
routes.rb (路由.rb)
Rails.application.routes.draw do
devise_for :users
resources :credits do
resources :entries
patch '/entries/:entry_id/update_position', to: 'entries#update_position', as: 'update_position'
end
get 'render/index'
root 'render#index'
end
entries_controller.rb(update_position操作)
def update_position
@entry.find(params[:id])
@entry.insert_at(entry_params[:position].to_i)
head :ok
end
在演职员表/节目下,我正在渲染该演职员表的相应条目,并希望致电credit_update_position_path以插入新位置。
<div class="container">
<div class="row">
<div class="col-8">
<div class="row">
<%= render @credit %>
<%= render partial: "entries/form", locals: { entry: @entry } %>
</div>
<div class="row">
<ul
data-controller="sortable"
data-sortable-resource-name-value="entry"
data-sortable-handle-value=".card">
<% @credit.entries.each do | entry | %>
<li
class="card"
data-sortable-update-url="<%= credit_update_position_path(@credit,entry.id) %>">
<div class="card-body">
<%= entry.name %>
<%= entry.id %>
</div>
</li>
<% end %>
</ul>
</div>
</div>
<div class="col-4">
<div class="credits">
<% @credit.entries.each do |entry| %>
<%= entry.name %>
<%= entry.id %>
<%= entry.role %><br>
<% end %>
</div>
</div>
</div>
<div class="row">
<%= link_to "Back to credits", credits_path %>
<%= button_to "Destroy this credit", @credit, method: :delete %>
</div>
</div>
尝试打开节目以获取信用时出现以下错误(在本例中为 @credit_id=7)
No route matches {:action=>"update_position", :controller=>"entries", :credit_id=>"7", :entry_id=>nil}, missing required keys: [:entry_id]
如果我不将其作为路径的一部分调用,我确实可以访问该 entry.id,正如您在代码的后面部分看到的那样。我做错了什么?谢谢
答: 暂无答案
评论
credit_update_position_path(@credit, entry_id: entry.id)
No route matches {:action=>"update_position", :controller=>"entries", :credit_id=>#<Credit id: 7, name: "Die Wallnacht", user_id: 2, created_at: "2023-03-29 10:11:40.325533000 +0000", updated_at: "2023-03-29 10:11:40.325533000 +0000">, :entry_id=>nil, :id=>"7"}, possible unmatched constraints: [:entry_id] Did you mean? credit_update_position_url