Ruby on rails Rails 4中MicroPost的注释
成功完成micropost系统后。登录用户可以通过表单发布并创建帖子。我开始对此进行补充,但也允许任何用户对帖子发表评论 我犯了很多错误。在大多数情况下,我不知道问题是什么,所以我想就如何完成这项工作提出一些建议。目前的错误是: StaticPageshome中的NameError 未定义的局部变量或“micropost”方法 StaticPageshome是一个显示每个人的微标题的页面,因此也显示评论表单。usersshow是用户的公共配置文件,显示方式与statispages相同。目前,他们还没有解决如何向用户墙发布帖子的问题,最终主页将成为通用提要,用户页面将成为用户对用户提要 评论模型Ruby on rails Rails 4中MicroPost的注释,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,成功完成micropost系统后。登录用户可以通过表单发布并创建帖子。我开始对此进行补充,但也允许任何用户对帖子发表评论 我犯了很多错误。在大多数情况下,我不知道问题是什么,所以我想就如何完成这项工作提出一些建议。目前的错误是: StaticPageshome中的NameError 未定义的局部变量或“micropost”方法 StaticPageshome是一个显示每个人的微标题的页面,因此也显示评论表单。usersshow是用户的公共配置文件,显示方式与statispages相同。目前,他们
create_table "comments", force: true do |t|
t.string "content"
t.integer "user_id"
t.integer "micropost_id"
t.datetime "created_at"
t.datetime "updated_at"
end
User.rb
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :comments
comment.rb
belongs_to :user
belongs_to :micropost
注释\u controller.rb
class CommentsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(params[:comment])
@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
end
class MicropostsController < ApplicationController
before_action :authenticate_user!
before_action :correct_user, only: :destroy
def index
@microposts = Micropost.all
@comment = @micropost.comments.build(params[:comment])
@comment.user = current_user
end
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
@feed_items = []
render root_url
end
end
def destroy
@micropost.destroy
redirect_to root_url
end
private
def micropost_params
params.require(:micropost).permit(:content)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end
def show
@user = User.find(params[:id])
@microposts = @user.microposts
@comment = Comment.new
if user_signed_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed
end
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
任何调用评论表单的地方
<%= render 'shared/comment_form', micropost: micropost %>
_注释_form.html.erb
<%= form_for([micropost, @comment]) do |f| %> #Here's the current error#
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, :placeholder => "Leave a comment" %>
</div>
<button class="btn" type="submit">
Create
</button>
<% end %>
架构显示模型的所有正确字段。所有数据库迁移都已完成。我看到它说,在[micropost,@comment]的form_中,第一个字段不能为零。我假设这意味着注释没有正确地将micropost_id填入它们所附加的micropost的id。我真的不能帮你解决这个错误,但我可以提供一个建议来帮助你调试-试试gem-这会在失败时提供一个交互式shell,带有完整的堆栈跟踪,等等。例如,你可以用它来找出micropost是否为零或为什么为零。谢谢你,我会尝试一下。将此更改为,看看它是否有效,不确定…调试的另一个资源在这里:我尝试了你的建议Rahul,better_errors显示错误在这一行
<%= form_for([micropost, @comment]) do |f| %> #Here's the current error#
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, :placeholder => "Leave a comment" %>
</div>
<button class="btn" type="submit">
Create
</button>
<% end %>