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なので、この問題を解決するための助けが必要です。 ありがとうございます。
には、このメソッドを入れて!どうもありがとう! –