1

我有用户/微博/评论模型,用户可以在其中评论其他人的微博。在每个帖子下都会显示一个文本字段,以便用户可以输入评论,但是我很难找到 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]
4

2 回答 2

3

只需为 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根据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
于 2013-02-05T12:56:29.867 回答
1

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

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

上面的 build 方法创建了一个 Comment 模型的新对象/实例,正如您使用current_user.comments的那样,该对象将user_id = current_user.id自动拥有,您无需再次指定它。并且 'build(:micropost => @micropost)' 将添加micropost's id@comment对象。

于 2013-02-05T13:08:06.143 回答