簡體   English   中英

Micropost在用戶頁面上的評論(Ruby on Rails)

[英]Micropost's comments on users page (Ruby on Rails)

在用戶頁面上,我有很多微博,我想向每個微博添加評論表格和評論。

我有三種模型:用戶,微博,評論。

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>

評論表

<%= 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 %>

每個微博都必須有自己的評論。 在我的數據庫中,我有注釋表

id / comment_content / user_id / micropost_id

列。

未創建評論,因為RoR無法理解此新評論屬於哪個微博。 我應該怎么做才能在數據庫中保存所有需要的信息?

更新

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] = "Comment created!"
       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

resources :microposts do
  resources :comments
end

我會在您的route.rb文件中將嵌套資源用於微博和注釋:

resources :microposts do
  resources :comments
end

然后,在您的注釋控制器中,可以通過micropost_comments_path(@micropost) url幫助器對其進行訪問,您可以執行以下操作以在create動作中建立關聯:

def create
  @micropost = Micropost.find(params[:micropost_id])
  @comment = Comment.new(params[:comment])
  @comment.micropost = @micropost
  @comment.user = current_user

  if @comment.save
    ...
end

您可以使用merge方法減少代碼行數,但是我發現這有時會降低代碼的可讀性,並且由於在驗證錯誤后重新顯示表單,您將需要@micropost對象,因此您也可以直接獲取記錄。

嘗試將當前微博傳遞給部分評論

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

然后將微博添加到注釋form_for調用中

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

確保您的路線是嵌套的

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

然后在CommentsController中的微博上建立CommentsController

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM