2017-02-18 42 views
0

我終於能向用戶模型後模型關聯關聯:添加註釋模型與後/用戶模型

def create 
@post = Post.new(post_params) 
@post.user = current_user 

現在我試圖創建在Post#Show評論頁面,但仍然收到有關我的表單的錯誤。這兩個錯誤,我遇到有:

NoMethodError in Posts#show undefined method `comments_path' 

,當我在Post#show@comment = Comment.new。當它被刪除時,我得到:

ArgumentError in Posts#show First argument in form cannot contain nil or be empty 

我的表單可能有什麼問題?此外,如果有人可以推薦更好的方式來關聯我的3個模型(基本上有一個user_id和一個post_id當評論創建時,我願意提供建議)。我的評論表單將出現在Post#Show頁面中。我當前的代碼是:

評論模型

belongs_to :user 
belongs_to :post 

用戶模型

has_many :posts 
has_many :comments 

日誌模型

has_many :comments 
belongs_to :user 

評論控制器

class CommentsController < ApplicationController 
before_action :find_comment, only: [:show, :edit, :update, :destroy] 

def index 
    @comments = Comment.all 
end 

def new 
    @comment = Comment.new 
end 

def create 
    @post = Post.find(params[:id]) 
    @comment = @post.comments.build(comment_params) 
    @comment.user = current_user 

    if @comment.save 
    flash[:notice] = "Successfully created..." 
    redirect_to comments_path 
    else 
    flash[:alert] = "failed" 
    redirect_to root_path 
    end 
end 

def show 
end 

def edit 
end 

def update 
    if @comment.update 
    flash[:notice] = "You updated your comment" 
    else 
    flash[:alert] = "Failed to update" 
end 

def destroy 
    @comment.destroy 
    redirect_to '/' 
end 

private 

    def find_comment 
    @comment = Comment.find(params[:id]) 
    end 

    def comment_params 
    params.require(:comment).permit(:comment) 
    end 
end 

柱控制器

class PostsController < ApplicationController 
    before_action :find_post, only: [:show, :edit, :update, :destroy] 

    def index 
    @posts = Post.all 
    end 

    def new 
    @post = Post.new 
    end 

    def create 
    @post = Post.new(post_params) 
    @post.user = current_user 
    if @post.save! 
     flash[:notice] = "Successfully created..." 
     redirect_to posts_path 
    else 
     flash[:danger] = "failed to add a post" 
     render 'new' 
    end 
    end 

    def show 
    end 

    def edit 
    end 

    def update 
    if @post.update 
     flash[:notice] = "Successfully updated" 
     redirect_to post_path 
    else 
     flash[:alert] = "Failed to update Post" 
     redirect_to :back 
    end 
    end 

    def destroy 
    if @post.destroy 
     flash[:notice] = "Successfully delete" 
     redirect_to posts_path 
    else 
     flash[:danger] = "Wasn't able to delete Blog post." 
     redirect_to :back 
    end 
    end 

    private 
    def find_post 
     @post = Post.find(params[:id]) 
    end 

    def post_params 
     params.require(:post).permit(:title, :body, :description) 
    end 
end 

帖子#顯示視圖

<%= render '/comments/form' %> 

評論#形式

<%= form_for @comment do |f| %> 
    <%= f.label :comment, "Title" %> 
    <%= f.text_field :comment, placeholder: "Write a comment" %> 
    <%= f.submit "Submit" %> 
<% end %> 

如果有什麼遺漏,請讓我更新我的文章。謝謝所有幫助我更好地理解我的問題的人。

回答

1

你的關聯看起來很好。錯誤

NoMethodError in Posts#show undefined method `comments_path' 

意味着你沒有路線comments_pathComments#Create。基本上,當你有一個form_for作爲參數Comment,它假定你想要去createupdate路線,這取決於它是一個新的記錄。最簡單的做法是將您的路線文件添加到

resources :comments 

。不過,既然你想與後相關的評論,你想修改你的路由文件有:

resources :posts do 
    resources :comments 
end 

然後,改變你的形式看起來像這樣:

<%= form_for [@post, @comment] do |f| %> 

所以當你提交一個表格,你將有一個params[:post_id]和一個params[:id]玩。用params[:post_id]找到Post。在創建評論時忽略params[:id],但在更新評論時使用它。

編輯:Here是有關嵌套資源的一些幫助的鏈接。

+0

謝謝您的回覆。不知何故,我錯過了添加資源:評論。有一個關於嵌套關聯的問題。如果我這樣做,那麼在列出所有評論時將會顯示posts /:id/comments。當試圖讓他們展示在後期展示中時,這是否更好? –

+0

進行更改後,我也遇到了錯誤。這是說我的評論創建控制器無法找到與'id'=後。創建評論後,我注意到它將我帶到posts /:id/comments。是因爲我的redirect_to在控制器中?感謝您的幫助。 –

+0

你可以這樣做。嵌套資源是一種更有組織的做事方式,特別是對於初學者。 你需要用'params [:post_id]'和'Comment'用'params [:id]'(如果註釋已經創建)找到'Post'。 –