sov*_*ndy 19 ruby-on-rails form-for ruby-on-rails-3
文档中可能有答案,但我似乎没有找到好的答案.所以在三个:url,:action,:方法中,在Rails中的form_for中使用它们时有什么不同?
Dip*_*hal 34
之间的差异:url,:action以及:method
:网址
如果要为任何特定控制器提交表单,请执行任何特定操作并希望传递一些额外参数(使用在控制器中定义的操作,并在控制器上传递)
例如
<%= form_for @post, :url => {:controller => "your-controller-name", :action => "your-action-name"} do |f| %>
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,表单被提交给该控制器(您传递url)并转到(传递操作)动作.它将默认为当前操作.
现在假设您想要传递额外的参数,例如
form_for @post, :url => { :action => :update, :type => @type, :this => @currently_editing } do |f| ...
Run Code Online (Sandbox Code Playgroud)
你可以传递额外的参数 :type => @type
:url表单提交的URL也是如此.它需要传递给url_for或link_to的相同字段.特别是你也可以直接传递一个命名路线.
:行动
form_for @post, :url => { :action => :update, :type => @type, :this => @currently_editing } do |f| ...
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,:action如果我们想要在不同的操作中提交表单然后我们通过:action并且your-action-name表单发布到该操作,则传递
:方法
method用于为该操作传递哪种方法.有几种方法,如put,post,get...
例如
form_for @post, :url => post_path(@post), :method => :put, ....
Run Code Online (Sandbox Code Playgroud)
在上面form_for我们通过:method => :put这个表单提交它将使用put方法
dea*_*ler 13
form_for基本上用于对象.例如:
<% form_for @person do |f| %>
...
<% end %>
Run Code Online (Sandbox Code Playgroud)
当您单击提交时,它将转到默认操作,例如:new to:create,:edit =>:update.如果要指定自己的操作,则必须使用:url和:method用于强制发布或获取.例如:
<% form_for @person :url => {:action => "my_action"}, :method => "post" do |f| %>
...
<% end %>
Run Code Online (Sandbox Code Playgroud)