Search code examples
ruby-on-railsput

Update Attributes not well, updating attributes. id => "id"?


Good Evening, I've been trying for figure out why when I raise inspect.params I get id => "id"

Please see the code I have for updating the :approved to true from an admin form which doesn't update the attributes. I guess it's something I'm just overlooking

Form index.html.erb

  <% @snippets.each do |snippet| %>
    <tr>
      <td><%= link_to snippet.content %></td>
      <td><%= snippet.created_at.to_date %></td>
      <td><%= render snippet %></td> 
      <td>Status</td>
<td><%= button_to 'Approve', active_snippet_path(snippet.id) %></td>
</tr>

Controller (Snippets#approve)

def approve
    #@snippet = @book.snippet.find(params[:id])
    if @snippet.update_attribute(:approved, true)
    redirect_to users_path
  else
   render root_path
  end 
end

The error is being cause by this bit apparently but not sure why:

 def find_book
    raise params.inspect
    @book = Book.find(params[:id])
    @snippet = @book.snippets
    return @book
  end
end

This is the inspect I get.

{"authenticity_token"=>"D70njMSz3iYbVcCCkFIlolPBKeZUsVtFL5pabRT1CMo=", "controller"=>"snippets", "action"=>"approve", "id"=>"id"}

Please let me know if you need anything else. I would like to understand clearly why this put isn't working for a nested model.


Solution

  • The problem is you have what seems an array of Snippets (@book.snippets) you set to @snippet, and are attempting to use the update_attribute method, which only works on one instance. If you want to update an array, you could use something to the effect of:

    Snippet.where(book_id: params[:id]).update_all(approved: true)