2016-12-11 7 views
0

Railsの基本を学びながら、Railsでブログシステムを作っています。しかし、私はエラーが発生しました: "私はコメントフォームが投稿の内容の下になりたいので、フォームの最初の引数にはnilを含めることはできません。私はスタックオーバーフローで他の人の回答をブラウジングすることを含め、この問題に対するあらゆる解決策を試してきましたが、これまで役に立たなかったのです。Ruby on Rails - フォームの最初の引数には、nilを含めることはできません。または、ポストで空にすることはできません。#show

CommentsController.rb

class CommentsController < ApplicationController 
    def index 
    @comments = Comment.all 
    end 

    def new 
    @comment = Comment.new 
    end 

    def create 
    @post = Post.find(params[:comment[:id]) 
    @comments = @post.comments.create(params[:comment]) 
    if @comments.save 
     flash[:success] = "Your comment has been successfully created." 
     redirect_to @post 
    else 
     flash[:danger] = "Oops! Something went wrong!" 
     redirect_to @post 
    end 
    end 

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

    def update 
    @comment = Comment.find(params[:id]) 
    if @comment.update_attributes 
     flash[:success] = "Your comment has been successfully updated." 
     redirect_to user_path 
    else 
     redirect_to user_path 
    end 
    end 

    def destroy 
    @comment = Comment.find(params[:id]) 
    if @comment.destroy 
     flash[:success] = "Your comment has been successfully deleted." 
     redirect_to post_path 
    else 
     flash[:danger] = "Oops! Something went wrong. Try again." 
    end 
    end 

    private 

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

end 

show.html.erb(ポスト)

<div class="show_post"> 
    <div class="container"> 
    <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12 col-lg-offset-3 col-md-offset-3"> 
     <h2 class="heading"> 
      <%= @post.title %> 
     </h2> 

     <%= raw(@post.content) %> 
    </div> 

    <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12 col-lg-offset-3 col-md-offset-3"> 
     <h4> 
      Liked this post? Add a comment below! 
     </h4> 

    <%= form_for(@comment) do |c| %> # Line that raises the error 
     <% if @comment.errors.any? %> 
     <div id="error_explanation"> 
      <h2> 
      <%= "#{pluralize(@comment.errors.count, "error")} errors were detected in your form:" %> 
      </h2> 
    <ul> 
     <% @comment.errors.full_messages.each do |message| %> 
     <li> 
      <%= message %> 
     </li> 
     <% end %> 
    </ul> 
    </div> 
<% end %> 

<div class="form-group"> 
    <%= c.label :content, "Your comment goes here: " %> 
    <%= c.cktext_area :content, rows: 10, class: "form-control" %> 
</div> 

<div class="form-group"> 
    <%= c.submit "Add Comment", class: "btn btn-primary" %> 
</div> 
    <% end %> 
</div> 

    <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12 col-lg-offset-3 col-md-offset-3"> 
     <h3> 
      Comments: 
     </h3> 
      <% if @comments && @comments.any? %> 
       <% @comments.each do |comment| %> 
        <%= raw(comment.content) %> 
       <% end %> 
      <% end %> 
    </div> 
</div> 

routes.rbを

Rails.application.routes.draw do 
    root 'pages#home' 

    get 'help' => 'pages#help' 
    get 'contact' => 'pages#contact' 

    get '/index' => 'users#index' 
    get '/signup' => 'users#new' 
    post '/users' => 'users#create' 
    get '/show' => 'users#show' 

    get '/login' => 'sessions#new' 
    post '/login' =>'sessions#create' 
    get '/logout' => 'sessions#destroy' 

    get '/new_post' => 'posts#new' 
    post '/new_post' => 'posts#create' 
    get '/show_post' => 'posts#show' 
    get '/delete_post' => 'posts#destroy' 

    resources :users 

    resources :posts 
    resources :comments 

私はまだRailsでn00bなので、この問題を解決するための助けが必要です。 ありがとうございます。

答えて

0

あなたの番組表の変数@commentnilです。新しいコメントへのコントローラのアクションでそれを設定します。

def show 
    #... 
    @comment = Comment.new 
end 
+0

には、このメソッドを入れて!どうもありがとう! –

0

あなたのコメントフォームは、ユーザーの表示ビューに表示された場合は、ユーザーコントローラーのshowアクションに@commentインスタンス変数を指定する必要がありますか、他Railsはdoesnの@commentが何を意味するのか知りません。

UserController.rb 

def show 
    @comment = Comment.new 
end 

ユーザアクションのコメントID(user/1/show)がないため、作成アクションの@post変数によってエラーが発生します。コメントIDをコメント作成アクションに渡す方法を見つける必要があります。

0

CommentsControllerクラスにshowメソッドがないようです。 showビューはshowメソッドから呼び出されます。 @commentインスタンス変数をどこにも初期化していないため、エラーが発生します。だからこそ、@commentはnilであると言っているのです。

だけ助けたあなたのコントローラ

def show 
    @comment = Comment.new 
end 
関連する問題