Ruby-on-rails :url, :action, :method in form_for 之间的 RoR 差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13576745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:05:31  来源:igfitidea点击:

RoR differences between :url, :action, :method in form_for

ruby-on-railsruby-on-rails-3form-for

提问by sovanlandy

There might be answers in documentation, but i don't seem to find good answers. So among the three :url, :action, :method, what are their differences when used in form_for in Rails?

文档中可能有答案,但我似乎没有找到好的答案。那么在:url、:action、:method这三个中,它们在Rails的form_for中使用时有什么区别呢?

回答by Dipak Panchal

Difference between :url, :actionand :method

之间的差异:url:action以及:method

:url

:url

If you want to submit your form for any particular controller, any particular action and want to pass some extra parameter (use action that define in controller that you pass on controller )

如果您想为任何特定控制器提交表单,任何特定操作并希望传递一些额外的参数(使用在控制器中定义的操作,您在控制器上传递)

for example

例如

<%= form_for @post, :url => {:controller => "your-controller-name", :action => "your-action-name"} do |f| %>

In the above code the form is submitted to that controller(that you pass on url) and goto that (you pass on action) action. it will take defaults to the current action.

在上面的代码中,表单被提交给那个控制器(你传递 url)并转到那个(你传递动作)动作。它将默认为当前操作。

now suppose you want to pass extra parameter then for example

现在假设你想传递额外的参数然后例如

form_for @post, :url => { :action => :update, :type => @type, :this => @currently_editing } do |f| ...

you can pass extra parameter like :type => @type

你可以传递额外的参数,比如 :type => @type

so :urlis The URL the form is submitted to. It takes the same fields you pass to url_for or link_to. In particular you may pass here a named route directly as well.

:url表单提交到的 URL 也是如此。它采用您传递给 url_for 或 link_to 的相同字段。特别是,您也可以直接在此处传递命名路线。



:action

:行动

 form_for @post, :url => { :action => :update, :type => @type, :this => @currently_editing } do |f| ...

In the above example we pass :actionif we want to submit the form in different action then we pass :actionand your-action-namethe form is post to that action

在上面的例子中,:action如果我们想在不同的动作中提交表单,那么我们传递:actionyour-action-name并且表单被发布到该动作



:method

:方法

method is used for which method you want to pass for that action. There are several methods like put,post,get...

方法用于您要为该操作传递的方法。有几种方法,例如put, post, get...

for example

例如

form_for @post, :url => post_path(@post), :method => :put, ....

In the above form_forwe pass :method => :putwhen this form is submit it will use putmethod

在上面form_for我们通过:method => :put当这个表单被提交时它将使用put方法

回答by dealer

form_for is basically used on object. For example:

form_for 基本上用于对象。例如:

      <% form_for @person do |f| %>
       ...
      <% end %>

When you click submit it will go to default action like from :new to :create, :edit => :update. If you want to specify your own action then you have to use :url and :method is used to force to post or get. For example:

当您单击提交时,它将转到默认操作,例如从 :new 到 :create、:edit => :update。如果要指定自己的操作,则必须使用 :url 和 :method 用于强制发布或获取。例如:

      <% form_for @person :url => {:action => "my_action"}, :method => "post" do |f| %>
       ...
      <% end %>

回答by Bibin Venugopal

URL:

网址:

Url is the path where your form data should go. whatever you write inside the :url symbol is considered as the path where your data should go when u click a submit button in the form.

Url 是表单数据应该去的路径。您在 :url 符号中写入的任何内容都被视为当您单击表单中的提交按钮时您的数据应该去的路径。

Action:

行动:

Action is the method in your controller, in your form_for @user (where @user is a object of User model), if you say :action => create then it sumbit the data to users_controller 'create' function (def create). You will mention this inside :url to tell that the data should go to the specifiled action.

Action 是您的控制器中的方法,在您的 form_for @user(其中 @user 是 User 模型的对象)中,如果您说 :action => create 那么它将数据 sumbit 到 users_controller 'create' 函数(def create)。您将在 :url 中提到这一点,以告诉数据应该转到指定的操作。

Method:

方法:

Is http method, there are 'get', 'post', 'update', 'patch' and 'delete' method. You can learn about this in google.

是http方法,有'get'、'post'、'update'、'patch'和'delete'方法。你可以在谷歌中了解这一点。