form_for 中:model 和@model 的区别? [英] Difference between :model and @model in form_for?
本文介绍了form_for 中:model 和@model 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下方式使用form_for有什么区别:
What is the difference between using form_for the following way:
<% form_for @user do |f| %>
<%= f.label :name %>:
<%= f.text_field :name, :size => 40 %>
...
<% end %>
和:
<% form_for :user, :url => {:action => 'create'} do |f| %>
<%= f.label :name %>:
<%= f.text_field :name, :size => 40 %>
...
<% end %>
使用@user 是否会自动为 URL 操作使用 CRUD 方法?
Does using @user just automatically use CRUD methods for the URL actions?
推荐答案
如果给 form_for
一个没有实例变量的符号,它会查找同名的实例变量.
If you give form_for
a symbol without an instance variable it looks for an instance variable with the same name.
文档说:
例如,如果@post 是一个现有的您要编辑的记录
For example, if @post is an existing record you want to edit
<% form_for @post do |f| %>
...
<% end %>
相当于:
<% form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %>
...
<% end %>
这篇关于form_for 中:model 和@model 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文