嵌套 form_for
我以这种方式组织项目: 用户是主要资源,每个用户都有一个配置文件,每个配置文件都有一个位置,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想在同一表单中访问不同的模型,可以使用accepts_nested_attributes_for。以下是有关该主题的精彩截屏视频: http://railscasts.com/ Episodes/196-nested-model-form-part-1
你的代码应该是这样的。
在您看来:
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-1Your code should something like this.
In your view:
使用 :
use :