2013-02-05 169 views
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 

查看/評論/_評論_形式:

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

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

UPDATE只需添加一個隱藏字段爲micropost_id:基於傳遞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 %> 

或更新的形式

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

UPDATE的action:與編輯評論控制器

# 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 
+0

我仍然得到相同的錯誤沒有發現micropost id指向評論控制器創建方法 – Jaqx

+0

我調整了我的答案根據您當前的控制器代碼:) – jvnill

+0

我遇到的問題是,在評論控制器Micropost查找params id返回爲零。我不知道如何瞄準正確的職位。當我在form_for中聲明micropost的值爲4時,它會在db中插入註釋但是我必須將控制器中的@comment更改爲= current_user.comments.build(params [:comment]) – Jaqx

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 

構建上述方法創建一個新的對象/實例的評論模型,因爲您已經使用current_user.comments這意味着對象將自動有user_id = current_user.id您不需要再指定它。 'build(:micropost => @micropost)'會將micropost's id添加到@comment對象。

相關問題