Restful Rails 编辑与更新
我试图在编辑条目后重定向到不同的页面,我认为它正在使用更新代码,因为您正在更新数据库。我花了一些时间才意识到我在控制器中使用了错误的操作。有人可以解释一下编辑和更新是如何工作的吗?为什么会有两种不同的动作?它们之间有什么区别?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑操作负责渲染视图
更新操作负责与模型交互(数据库更新等)
如果您运行
rake paths
,您将看看动词和动作之间的区别。通常,创建/更新操作在提交表单时使用。这与新建操作和编辑操作不同,因为它们用于呈现视图(显示要提交的表单)。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).另一个观点 - 强调相似性和差异有点多余:
New 是呈现表单的先驱操作,在提交时运行Create 操作。
(视图通常会重定向回索引视图,显示您已经创建的类似项目的列表)
编辑是呈现表单的前导操作,在提交时运行更新操作。
(该视图通常会重定向回索引视图,显示您已经创建的类似项目的列表)
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)