I have two models:
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
Using the acts_as_list gem I want to update the order of entries (entry.position) within the view of a credit.
I am using the following setup:
routes.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 action)
def update_position
@entry.find(params[:id])
@entry.insert_at(entry_params[:position].to_i)
head :ok
end
Under the credits/show I am rendering the respective entries of that credit and want to call the credit_update_position_path to insert the new position.
<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>
I get the follwoing error when trying to open the show for a credit (in this case @credit_id=7)
No route matches {:action=>"update_position", :controller=>"entries", :credit_id=>"7", :entry_id=>nil}, missing required keys: [:entry_id]
I do have access to the entry.id if I invoke it not as part of the path as you can see in the later part of the code. What am I doing wrong? Thanks