I'm trying to figure out how to toggle a "finish" action with nested resources in rails. For whatever reason I can't get it to do what I want. I keep getting 'Couldn't find List without an ID'. Which makes sense but I can't configure it so that it will work. Was curious if anyone knew how to configure things as to get this to function properly. I'm assuming it probably has something to do with my routes file or the each block maybe?? in the partial. Thanks.
Code Below.
Books Controller
def finish
@list = List.find(params[:list_id])
@book = @list.books.find(params[:id])
@book.update(finished: true)
respond_to do |format|
format.html {redirect_to list_path(@list)}
format.js {}
render @list
end
end
def unfinish
@list = List.find(params[:list_id])
@book = @list.books.find(params[:id])
@book.update(finished: false)
respond_to do |format|
format.html {redirect_to list_path(@list)}
format.js {}
render @list
end
end
Books Partial
<table class="table table-hover">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Pages</th>
<th>Did you finish the book?</th>
<th>Remove book from list</th>
</tr>
</thead>
<tbody>
<% @books.each do |book| %>
<tr>
<td><%=book.name %></td>
<td><%=book.author %></td>
<td><%=book.pages %></td>
<% if book.finished? %>
<td class="unfinish-book"><%=link_to 'Finished', unfinish_book_path(book), :method => :put, remote: true %></td>
<% else %>
<td class="finish-book"><%=link_to 'Mark as Finished', finish_book_path(book), :method => :put, remote: true %></td>
<% end %>
<td class><%= link_to '|Remove Book|', list_book_path(@list, book), method: :delete, data: { confirm: 'Are you sure?'} %></td>
<% end %>
</tr>
</tbody>
</table>
Routes
Rails.application.routes.draw do
root 'lists#index'
resources :lists do
resources :books
end
resources :books do
member do
put :finish
put :unfinish
end
end
end