Restful Rails Edit vs Update

时光毁灭记忆、已成空白 提交于 2019-12-31 10:27:06

问题


I was trying to redirect to a different page after editing an entry, I assumed that it was using the update code because you are updating the database. It took me some time to realise that I was using the wrong action in the controller. Can someone please explain how edit and update work. Why are there two different actions? what are the differences between them?


回答1:


edit action is responsible for rendering the view

update action is responsible for interacting with the model (db updates etc)

If you run rake routes you will see the difference between the verb and the action. Typically, the create/update actions are used when submitting a form. This differs from the new and edit actions as these are used to render the view (that displays the form to be submitted).




回答2:


Another perspective - a bit redundant to highlight similarities and differences:

New is the precursor action to render a form, that upon submitting, runs the Create action. (the view is typically redirected back to the index view showing a list of similar items you already created)

Edit is the precursor action to render a form, that upon submitting, runs the Update action. (the view is typically redirected back to the index view showing a list of similar items you already created)



来源:https://stackoverflow.com/questions/3910783/restful-rails-edit-vs-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!