Joe*_*Joe 2 resources ruby-on-rails form-for
我以这种方式组织项目:用户是主要资源,每个用户都有一个配置文件,每个配置文件有一个位置如下:
resources :users do
resource :profile, :controller => "profiles" do
resource :location
end
Run Code Online (Sandbox Code Playgroud)
现在我必须构建一个表单来插入所有的配置文件信息,但也包括位置信息(地址等).如果我写下面的代码,它不会关心位置.
<%= form_for(@profile, :url=>{:action=>'update'}, :html => {:multipart => true}) do |f| %>
Run Code Online (Sandbox Code Playgroud)
有人对这种情况有什么建议吗?
TNX
如果要在同一表单中访问不同的模型,可以使用accepts_nested_attributes_for.以下是关于该主题的精彩截屏视频:http://railscasts.com/episodes/196-nested-model-form-part-1
你的代码应该是这样的.
#profile.rb
accepts_nested_attributes_for :location
Run Code Online (Sandbox Code Playgroud)
在你看来:
<%= form_for(@profile, :url=>{:action=>'update'}, :html => {:multipart => true}) do |f| %>
<%= f.fields_for :location do |l| %>
//location fields here, for example:
<%=l.text_field :city %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7494 次 |
| 最近记录: |