如何将评论表单与正确的帖子相关联

时间:2013-02-05 12:46:02

标签: ruby-on-rails form-for

我有用户/微博/评论模型,用户可以评论他人的微博。在每个帖子下都会显示一个文本字段,以便用户可以输入评论但是我很难找到Micropost Id。我认为问题出在我的form_for评论或控制器,但我不是很确定。希望得到一些帮助,谢谢。

错误:找不到没有ID的微博

型号:

User Model: has many microposts, has many comments
Micropost Model: belongs to user, has many comments
Comment Model: belongs to micropost, belongs to user

用户控制器:

def show #(the profile page where all the posts and comments are)
  @user = User.find(params[:id])
  @microposts = @user.microposts.paginate(page: params[:page])
  @micropost  = current_user.microposts.build if signed_in?
  @comments = @micropost.comments
  @comment = current_user.comments.build(:micropost => @micropost) if signed_in?
end

评论控制器:

def create
  @micropost = Micropost.find(params[:id])
  @comment = current_user.comments.build(:micropost => @micropost) #can someone explain what happens in the parentheses? 
  @comment.user = current_user
  @comment.save
  redirect_to :back
end

查看/评论/ _comment_form:

<%= form_for(@comment) do |f| %>
  <div id="comment_field">
    <%= f.text_field :content, placeholder: "Say Something..." %>
  </div>
<% end %>

路线:

resources :users
resources :microposts, only: [:create, :destroy]
resources :comments, only: [:create, :destroy]

2 个答案:

答案 0 :(得分:2)

只需为micropost_id添加隐藏字段

<%= form_for(@comment) do |f| %>
  <%= f.hidden_field :micropost_id, value: @micropost.id %>
  <div id="comment_field">
    <%= f.text_field :content, placeholder: "Say Something..." %>
  </div>
<% end %>

更新:传递micropost_id而不对控制器进行任何更改

根据您的评论控制器,您会在提交表单时找不到基于micropost的{​​{1}} params[:id]。下面的代码修复了这个问题。但是,我建议您查看嵌套资源,这将使控制器代码更漂亮,更光滑

<%= form_for @comment do |f| %>
  <%= hidden_field_tag :id, @micropost.id %>
  <div id="comment_field">
    <%= f.text_field :content, placeholder: "Say Something..." %>
  </div>
<% end %>

或更新表单的action

<%= form_for @comment, url: comments_path(id: @micropost.id) do |f| %>
  <div id="comment_field">
    <%= f.text_field :content, placeholder: "Say Something..." %>
  </div>
<% end %>

更新:对评论控制器进行编辑

# view
<%= form_for @comment do |f| %>
  <%= hidden_field_tag :micropost_id, @micropost.id %>
  <div id="comment_field">
    <%= f.text_field :content, placeholder: "Say Something..." %>
  </div>
<% end %>

# comments_controller.rb

def create
  @micropost = Micropost.find params[:micropost_id]
  @comment = current_user.comments.build
  @comment.micropost = @micropost
  @comment.save
end

答案 1 :(得分:1)

Yous应该以这种方式设置您的评论资源:

resources :users
resources :microposts, only: [:create, :destroy] do
  resources :comments, only: [:create, :destroy]
end

以上资源称为嵌套资源。在你的情况下评论总是与微博有关,你应该 嵌套 评论资源到微博 并在评论控制器中:

def create
  @micropost = Micropost.find(params[:id])
  @comment = current_user.comments.build(:micropost => @micropost) #can someone explain what happens in the parentheses?
  @comment.save
  redirect_to :back
end

上面的构建方法创建了一个新的Comment模型对象/实例,并且正如您使用current_user.comments这意味着,对象将自动user_id = current_user.id,您无需再次指定它。并且'build(:micropost =&gt; @micropost)'会将micropost's id添加到@comment对象。