Micropost的用户页面评论(Ruby on Rails) [英] Micropost's comments on users page (Ruby on Rails)

查看:93
本文介绍了Micropost的用户页面评论(Ruby on Rails)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户的页面上有很多微博,我想为每个微博添加评论表单和评论。



我有三个模型:User,Micropost, / p>

user.rb

  < ActiveRecord :: Base 
has_many:microposts,dependent::destroy
has_many:comments
end

micropost.rb

  class Micropost< ActiveRecord :: Base 
belongs_to:user
has_many:comments,dependent::destroy
end

comment.rb

  ActiveRecord :: Base 
attr_accessible:comment_content

belongs_to:user
belongs_to:micropost

validates:comment_content,presence:true
validates: user_id,presence:true
validates:micropost_id,presence:true
end

comments_controller.rb

  class CommentsController< ApplicationController 

def create
@comment = current_user.comments.build(params [:comment])
如果@ comment.save
flash [:success] =评论已创建!
redirect_to current_user
else
render'shared / _comment_form'
end
end
end

_micropost.html.erb

 < tr> 
< td class =micropost>
< span class =content><%= wrap(micropost.content)%>< / span>
< span class =timestamp>
发布日期<%= time_ago_in_words(micropost.created_at)%>前。
< / span>
<%= render'shared / comment_form'%>
< / td>
< / tr>

评论表

 <%= form_for(@comment)do | f | %> 
<%= render'shared / error_messages',object:f.object%>
< div class =field>
<%= f.text_area:comment_content%>
< / div>
< button class =btntype =submit>
Create
< / button>
<%end%>

每个微博必须有自己的评论。
在我的数据库我有注释表与


id / comment_content / user_id / micropost_id


列。



注释不会创建,因为RoR无法识别此新注释属于哪个微博。
我应该如何在我的DB中拥有所有需要的信息?



UPDATE



users_controller



  def show 
@user = User.find params [:id])
@microposts = @ user.microposts.paginate(page:params [:page])
@comment = Comment.new
end

microposts_controller

  def create 
@micropost = current_user.microposts.build(params [:micropost])
if @ micropost.save
flash [:success] =Micropost created!
redirect_to current_user
else
render'shared / _micropost_form'
end
end

解决方案!!!



感谢carlosramireziii和Jon!他们都是正确的



comments_controller

  def create 
@micropost = Micropost.find(params [:micropost_id])
@comment = Comment.new(params [:comment])
@ comment.micropost = @micropost
@ comment.user = current_user
if @ comment.save
flash [:success] =注释创建!
redirect_to current_user
else
render'shared / _comment_form'
end
end

_micropost.html.erb

 <%= render'shared / comment_form',micropost:micropost%> 

评论表

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

routes.rb

 资源:microposts do 
资源:comments
end


解决方案

尝试将当前微博传递给部分评论

 <%= render'shared / comment_form',micropost:micropost%> 

然后将微博添加到注释 form_for

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

确保您的路线已嵌套

 #in routes.rb 
resources:microposts do
resources:comments
end

然后在 CommentsController

中构建微博的注释

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


On user's page i have many microposts and i want to add comment form and comments to each micropost.

I have three models: User, Micropost, Comment.

user.rb

class User < ActiveRecord::Base
  has_many :microposts, dependent: :destroy
  has_many :comments
end

micropost.rb

class Micropost < ActiveRecord::Base
  belongs_to :user
  has_many :comments, dependent: :destroy
end

comment.rb

class Comment < ActiveRecord::Base
  attr_accessible :comment_content

  belongs_to :user
  belongs_to :micropost

  validates :comment_content, presence: true
  validates :user_id, presence: true
  validates :micropost_id, presence: true  
end

comments_controller.rb

class CommentsController < ApplicationController

  def create
    @comment = current_user.comments.build(params[:comment])
    if @comment.save
       flash[:success] = "Comment created!"
       redirect_to current_user
    else
      render 'shared/_comment_form'
    end
  end
end

_micropost.html.erb

<tr>
  <td class="micropost">
    <span class="content"><%= wrap(micropost.content) %></span>
    <span class="timestamp">
      Posted <%= time_ago_in_words(micropost.created_at) %> ago.    
    </span>
    <%= render 'shared/comment_form' %>
   </td>
</tr>

Comment form

<%= form_for(@comment) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :comment_content %>
  </div>
  <button class="btn" type="submit">
    Create
  </button>
<% end %>

Every micropost must have its own comments. In my DB i have comment table with

id / comment_content / user_id / micropost_id

columns.

Comment is not creating because RoR can't understand to which micropost belongs this new comment. What should i do to have all needed information in my DB?

UPDATE

users_controller

  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])
    @comment = Comment.new
  end

microposts_controller

  def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to current_user
    else
      render 'shared/_micropost_form'
    end
  end

SOLUTION!!!

Big thanks to carlosramireziii and Jon! They are both right

comments_controller

  def create
    @micropost = Micropost.find(params[:micropost_id])
    @comment = Comment.new(params[:comment])
    @comment.micropost = @micropost
    @comment.user = current_user
    if @comment.save
       flash[:success] = "Comment created!"
       redirect_to current_user
    else
      render 'shared/_comment_form'
    end
  end

_micropost.html.erb

<%= render 'shared/comment_form', micropost: micropost %>

Comment form

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

routes.rb

resources :microposts do
  resources :comments
end

解决方案

Try passing in the current micropost to the comment partial

<%= render 'shared/comment_form', micropost: micropost %>

Then add the micropost to the comment form_for call

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

Make sure your routes are nested

# in routes.rb
resources :microposts do
  resources :comments
end

Then build the comment off of the micropost in the CommentsController

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

这篇关于Micropost的用户页面评论(Ruby on Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆