Form_for中的:url,:action和:method之间的RoR差异 [英] RoR differences between :url, :action, :method in form_for

查看:106
本文介绍了Form_for中的:url,:action和:method之间的RoR差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

: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 )

例如

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

在上面的代码中,表单被提交到该控制器(您传递URL)并转到该控制器(您传递动作)操作.它将默认使用当前操作.

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.

现在假设您要传递额外的参数,然后例如

now suppose you want to pass extra parameter then for example

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

您可以传递:type => @type

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

so :url is 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.

:操作

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

在上面的示例中,如果要通过其他操作提交表单,则传递:action,然后传递:actionyour-action-name表单将发布到该操作

In the above example we pass :action if we want to submit the form in different action then we pass :action and your-action-name the form is post to that action

:方法

方法用于要对该操作传递的方法.有几种方法,例如putpostget ...

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

例如

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

在上面的form_for中,当提交此表单时,我们通过:method => :put,它将使用put方法

In the above form_for we pass :method => :put when this form is submit it will use put method

这篇关于Form_for中的:url,:action和:method之间的RoR差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆