简体   繁体   English

Rails has_many在单独的表单/视图中嵌套了属性

[英]Rails has_many nested attributes in a separate form/view

I found plenty of example on how to construct a multi-model form and multi-model display. 我发现了很多有关如何构造多模型表单和多模型显示的示例。 But what if I want to have separate forms and displays? 但是,如果我想使用单独的表格和显示该怎么办?

post.rb: post.rb:

class Post < ActiveRecord::Bas
  has_many :comments, dependent: :destroy
  attr_accessible :comments_attributes
  accepts_nested_attributes_for :comments
end

comment.rb: comment.rb:

class Comment < ActiveRecord::Base
  belongs_to :post
end

posts_controller.rb: posts_controller.rb:

def new
  @post = Post.new
  @post.comments.build
  ...
end

routes.db: routes.db:

resources posts do
  resources comments
end

I have a link to post comments index in my post index: 我有一个链接可以在我的帖子索引中发布评论索引:

views/posts/index.html.erb: 意见/职位/ index.html.erb:

...
<%= link_to 'Comments', post_comments_path(post) %>
...

Post and Comment each have their own scaffold generated form (not nested). Post和Comment都有自己的支架生成形式(不嵌套)。

<%= form_for(@post) do |f| %>
...

<%= form_for(@comment) do |f| %>
...

In the comments index I loop over post comments: 在评论索引中,我遍历帖子评论:

views/comments/index.html.erb: 观点/评论/ index.html.erb:

<% @post = Post.find(params[:post_id]) %>  //works fine  
<% @post.comments.each do |comment| %>
...
<% end %>

Yet after adding a new comment (under a specific post id) the table in the post comments index is empty! 但是,在添加新评论(在特定帖子ID下) 之后,帖子评论索引中的表为空!

Please help. 请帮忙。 Thanks :) 谢谢 :)

I figured it out. 我想到了。

In the comments forms it should be: 在评论表中应为:

<%= form_for([@post, @comment]) do |f| %>
...

Paths should be used like: 路径应按以下方式使用:

post_comments_path(@post)
edit_post_comment_path(@post,@comment)

etc. 等等

In the Comments controller: 在评论控制器中:

def index
    @post= Post.find(params[:post_id])
    @comments= @post.comments.all
...

def show
    @post= Post.find(params[:post_id])
    @comment= @post.comments.find(params[:id])
...

etc. 等等

Hope others will find this useful! 希望其他人会发现这个有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 嵌套属性has_many通过rails 4的表单助手 - Form helpers for nested attributes has_many through in rails 4 Rails 4具有simple_form_for,has_many和嵌套属性 - Rails 4 with simple_form_for, has_many through and nested attributes Rails 4嵌套属性和has_many:通过表单中的associaton - Rails 4 nested attributes and has_many :through associaton in a form 通过关系在 has_many 中具有嵌套属性的 Rails 表单 - Rails form with nested attributes in has_many through relationship 嵌套参数不包括 <name> 提交嵌套属性时,_rails在Rails 4中具有has_many关联形式 - Nested parameter does not include <name>_attributes in Rails 4 when submitted nested attributes, has_many associated form Rails 3 - has_many通过嵌套表单 - Rails 3 - has_many through with nested form Rails has_many:通过嵌套表单 - Rails has_many :through nested form 使用accepts_nested_attributes_for(b / t和has_many)以单一形式创建单独的模型 - Creating separate models in single form with accepts_nested_attributes_for (b/t & has_many) Rails嵌套属性form_for for has_many,使用javascript无限制嵌套模型 - Rails Nested Attributes form_for for has_many with unlimited nested models using javascript 在提交嵌套表单时,Rails has_many通过连接模型undefined id:“undefined方法`属性&#39;为#” - Rails has_many through join model undefined id: “undefined method `attributes' for #” when submitting nested form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM