嵌套 form_for

发布于 2024-10-31 15:37:15 字数 429 浏览 2 评论 0原文

我以这种方式组织项目: 用户是主要资源,每个用户都有一个配置文件,每个配置文件都有一个位置,如下所示:

 resources :users do    
    resource :profile, :controller => "profiles" do
      resource :location
end

现在我必须构建一个表单来插入所有配置文件信息,但也插入位置信息(地址ETC)。 如果我编写以下代码,它不会关心位置。

<%= form_for(@profile, :url=>{:action=>'update'}, :html => {:multipart => true}) do |f| %>

有人对这种情况有什么建议吗?

总氮

I have the project organized in this way: User is the main resource, each user has one profile and each profile has one location as following:

 resources :users do    
    resource :profile, :controller => "profiles" do
      resource :location
end

Now I have to build a form for insert all the profile information but the location information as well (address etc).
If I write the following code, it doesn't take care about the location.

<%= form_for(@profile, :url=>{:action=>'update'}, :html => {:multipart => true}) do |f| %>

Do someone have some suggestion for this situation ?

Tnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抽个烟儿 2024-11-07 15:37:15

如果您想在同一表单中访问不同的模型,可以使用accepts_nested_attributes_for。以下是有关该主题的精彩截屏视频: http://railscasts.com/ Episodes/196-nested-model-form-part-1

你的代码应该是这样的。

#profile.rb

accepts_nested_attributes_for :location

在您看来:

<%= 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 %>

If you want to access different models within the same form you can use accepts_nested_attributes_for. Here is a great screencast about the topic: http://railscasts.com/episodes/196-nested-model-form-part-1

Your code should something like this.

#profile.rb

accepts_nested_attributes_for :location

In your view:

<%= 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 %>
总以为 2024-11-07 15:37:15

使用 :

form_for [@user, @profile, @location], :action => :update, :html => {:multipart => true}

use :

form_for [@user, @profile, @location], :action => :update, :html => {:multipart => true}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文