简体   繁体   中英

How do I fix my edit form from editing all the articles in my database in Ruby/Rails?

I'm following along with the Getting Started with Ruby on Rails guide and I've run into an issue when editing/updating an article in the database. I created the form, and have a link from the index and defined all the methods in the controller as the guide asks(see code in pastebin links). However, when I click the 'Update Article' button on the edit view and I go back to the index, instead of updating just the one article it updates all of them.

I've run through the code and from what I can tell it's identical to the code from the guide, so I'm not sure why I'm getting different results to them.

articles_controller.rb

index.html.erb

edit.html.erb

I think the error is in this line since maybe it isn't passing the id parameter correctly or something?

<%= form_with(model: @article, local: true) do |form| %>

From what I can tell, I pass the specific article after finding it in the edit definition in the controller to the edit view. Then the form submits a PATCH request to the server to the /articles/:id which will be handled by the update definition in the controller. This update will just find it, and update it's parameters by the given article object and then bring to the show view, or display the errors if it somehow didn't meet the model validation.

Could you please change the object from the Main Article Model to instance object( @article )? Update works on a single object.

if Article.update(article_params) 

to

if @article.update(article_params)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM