Restful Rails 编辑与更新

发布于 2024-09-27 05:03:15 字数 120 浏览 5 评论 0原文

我试图在编辑条目后重定向到不同的页面,我认为它正在使用更新代码,因为您正在更新数据库。我花了一些时间才意识到我在控制器中使用了错误的操作。有人可以解释一下编辑和更新是如何工作的吗?为什么会有两种不同的动作?它们之间有什么区别?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二货你真萌 2024-10-04 05:03:15

编辑操作负责渲染视图

更新操作负责与模型交互(数据库更新等)

如果您运行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).

红焚 2024-10-04 05:03:15

另一个观点 - 强调相似性和差异有点多余:

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文