当我尝试为微博创建评论时出现错误 "undefined method `comment_content'"
When I tried to create comment to micropost I have an error as "undefined method `comment_content'"
当我为微博创建评论时出现"undefined method `comment_content'"错误,@comment.save方法有问题。
请帮我解决问题。感谢您的关注。
comments_controller
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(comment_params)
@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
private
def comment_params
params.require(:comment).permit(:content)
end
_comment_form
<%= form_for([micropost, @comment]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content %>
</div>
<button class="btn" type="submit">
Create
</button>
<% end %>
comment.rb
belongs_to :micropost
belongs_to :user
validates :comment_content, presence: true
validates :user_id, presence: true
validates :micropost_id, presence: true
static_pages_controller
def home
if logged_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
@comment = Comment.new
end
end
_micropost.html.erb
<%= render 'shared/comment_form', micropost: micropost %>
首先,您可以通过以下方式改善您的动作:
def create
@comment = Comment.new(comment_params.merge(micropost_id: params[:micropost_id], user: current_user))
# ....
end
您正在验证字段 "comment_content" 是否存在:
validates :comment_content, presence: true
不过,您的专栏好像叫"content"。您确定不应该输入 :
validates :content, presence: true
检查您的 config/schema.rb 以查看名称是 "content" 还是 "comment_content"。还要检查你是否有 运行 你的迁移与 rake db:migrate.
当我为微博创建评论时出现"undefined method `comment_content'"错误,@comment.save方法有问题。
请帮我解决问题。感谢您的关注。
comments_controller
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(comment_params)
@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
private
def comment_params
params.require(:comment).permit(:content)
end
_comment_form
<%= form_for([micropost, @comment]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content %>
</div>
<button class="btn" type="submit">
Create
</button>
<% end %>
comment.rb
belongs_to :micropost
belongs_to :user
validates :comment_content, presence: true
validates :user_id, presence: true
validates :micropost_id, presence: true
static_pages_controller
def home
if logged_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
@comment = Comment.new
end
end
_micropost.html.erb
<%= render 'shared/comment_form', micropost: micropost %>
首先,您可以通过以下方式改善您的动作:
def create
@comment = Comment.new(comment_params.merge(micropost_id: params[:micropost_id], user: current_user))
# ....
end
您正在验证字段 "comment_content" 是否存在:
validates :comment_content, presence: true
不过,您的专栏好像叫"content"。您确定不应该输入 :
validates :content, presence: true
检查您的 config/schema.rb 以查看名称是 "content" 还是 "comment_content"。还要检查你是否有 运行 你的迁移与 rake db:migrate.