在Rails中,“新”操作如何重定向到“创建”? [英] How is the 'new' action redirected to 'create' in Rails?
问题描述
在Rails中,我可以使用路由文件中的资源
自动为CRUD操作创建一组路由。
这将创建索引
,新
,创建
,编辑
,显示
,更新
并销毁路线。
我了解这些路线的工作流程,通常是根据传递给相应动作的参数创建相应的视图并创建相应的视图时,通常会在调用路线时创建模型对象客户端将重定向到另一个指定的操作。
新建
和修改
视图可以使用相同的 _form
部分来更新其各自操作中的模型对象。但是,我在努力地理解之后如何将客户端重定向到另一个动作。
例如在 new
动作中客户端被重定向到 create
操作,并且在 new
中更新的模型对象被传递给创建
作为参数。我不了解这种情况的发生,因为没有指定重定向,也看不到如何将模型对象转换为参数。
def new
@article = Article.new
end
def create
@article = Article.create(article_params)
redirect_to(article_path(@ article.id))
结尾
_form部分:
<%= form_for(@article)做| f | %>
< ul>
<%@ article.errors.full_messages.each | error | %>
< li>%=错误%>< / li>
<%end%>
< / ul>
< p>
<%= f.label(:title)%>< br>
<%= f.text_field(:title)%>
< / p>
< p>
<%= f.label(:body)%>< br>
<%= f.text_area(:body)%>
< / p>
< p>
<%= f.submit%>
< / p>
<%end%>
OOP
您问题的底线答案是Rails 的性质归结于同一想法,即您需要在应用程序中使用 objects -因此,资源
指令提供了7个关键操作来操作这些对象
要完全理解Rails,您确实需要研究它如何与对象一起工作,特别是它们如何工作彼此互动
重定向
要回答有关重定向
的问题,简单的答案是 Rails不会重定向到任何具体的操作
请记住, Rails是无状态 -它不会通过请求保留数据-仅 具有您当时初始化或已发送的数据
您感到困惑的是一些Rails动作似乎如何将您的请求发送到适当的 request动作。答案在于您使用的帮助程序/方法,特别是 form_for
form_for 使用其ActiveRecord对象构建表单。
因此,如果执行以下操作:
#app / controllers / your_controller.rb
Class YourController< ActiveRecord :: Base
def new
@model = Model.new
结束
结束
这将使Rails知道它正在加载 new
对象,因此将使用 form_for
方法,将请求发送到 create 操作
如果使用 form_tag
,您将无法重定向到 create
操作-这就是Rails的魔力-它是为容纳对象而构建的
In Rails I can automatically create a set of routes for CRUD actions using resources
in the routes file.
This creates index
, new
, create
, edit
, show
, update
and destroy routes.
I understand the general flow of how these routes work and usually when a route is called a model object is created according to the parameters passed into the corresponding action and the corresponding view is created or the client is redirected to another specified action.
Both the new
and edit
views can use the same _form
partial to update the model object in their respective actions. However I am struggling to understand how the client is redirected to another action afterwards.
For example in the new
action the client is redirected to the create
action and the model object that was updated in new
is passed to create
as a parameter. I don’t understand how this is happening since no redirect is specified and I cannot see how the model object is turned into a parameter.
def new
@article = Article.new
end
def create
@article = Article.create(article_params)
redirect_to(article_path(@article.id))
end
_form Partial:
<%= form_for(@article) do |f| %>
<ul>
<% @article.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
<p>
<%= f.label( :title ) %><br>
<%= f.text_field( :title ) %>
</p>
<p>
<%= f.label( :body ) %><br>
<%= f.text_area( :body ) %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
OOP
The bottom line answer to your question is that Rails is object orientated (by virtue of being built on top of Ruby). This is very important, as it means everything inside Rails should be based around objects:
This leads me to the routes - the resourceful
nature of Rails' routes is down to the same idea, that you need to work with objects in your application - hence the resources
directive providing 7 key actions to manipulate those objects
To fully understand Rails, you really need to look into how it works with objects, specifically how they interact with each other
Redirect
To answer your question regarding the redirect
, the simple answer is that Rails doesn't "redirect" to any action specifically
Remember, Rails is stateless - it does not persist data through requests - it only has the data which you either initialize at the time, or have sent it
What you're confused about is how some of the Rails actions seem to be sending your requests to the appropriate "request" action. The answer to this lies in the helpers / methods you use, specifically form_for
form_for builds forms from their ActiveRecord objects.
Therefore, if you perform the following:
#app/controllers/your_controller.rb
Class YourController < ActiveRecord::Base
def new
@model = Model.new
end
end
This will give Rails the knowledge that it's loading a new
object, and therefore will use the form_for
method to send the request to the create action
If you used form_tag
, you would not get a redirect to the create
action -- that's the magic of Rails -- it's been built to accommodate objects
这篇关于在Rails中,“新”操作如何重定向到“创建”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!