Ruby on rails 用户_id:nil当评论发布到Heroku上的Rails应用程序时
当我尝试查看显示页面时,我的应用程序给了我一个错误。以下是错误:Ruby on rails 用户_id:nil当评论发布到Heroku上的Rails应用程序时,ruby-on-rails,heroku,Ruby On Rails,Heroku,当我尝试查看显示页面时,我的应用程序给了我一个错误。以下是错误: 2014-04-23T03:26:23.544575+00:00 app[web.1]: ActionView::Template::Error (undefined method `image' for nil:NilClass): 2014-04-23T03:26:23.544575+00:00 app[web.1]: 1: <%= div_for(comment) do %> 2014-04-23
2014-04-23T03:26:23.544575+00:00 app[web.1]: ActionView::Template::Error (undefined method `image' for nil:NilClass):
2014-04-23T03:26:23.544575+00:00 app[web.1]: 1: <%= div_for(comment) do %>
2014-04-23T03:26:23.544575+00:00 app[web.1]: 2: <%= h(comment.body) %>
2014-04-23T03:26:23.544575+00:00 app[web.1]: 3: <p class="comments">
2014-04-23T03:26:23.544575+00:00 app[web.1]: 4: <strong>Posted <%= time_ago_in_words(comment.created_at) %> ago by <%= image_tag comment.user.image, class: "smallpic_comment" %><%= comment.user.name %></strong></br>
2014-04-23T03:26:23.544575+00:00 app[web.1]: 5: </p>
2014-04-23T03:26:23.544575+00:00 app[web.1]: 6: <% if user_signed_in? %>
2014-04-23T03:26:23.544575+00:00 app[web.1]: 7: <% if current_user == comment.user || current_user.admin? %>
注释模型:
class Comment < ActiveRecord::Base
attr_accessible :body, :pin_id, :author
belongs_to :pin
belongs_to :user
end
我假设它突然出现了@comment.user=current_user的问题
我感谢任何帮助,如果你需要更多信息,请告诉我。以下是现场站点:www.thetens.us和github:我的邮件信息已更改,当应用程序尝试发送电子邮件时,它无法发送。由于控制器在保存帖子时试图执行电子邮件,因此它正在破坏所保存的文件 我暂时删除了邮件程序,直到我解决了这个问题,现在它工作正常
def create
@pin = Pin.find(params[:pin_id])
@comment = @pin.comments.create(params[:comment])
@comment.user = current_user
#MyMailer.comment_email(@pin.user).deliver
respond_to do |format|
if @comment.save
format.html { redirect_to @pin, notice: 'Comment was successfully created.' }
format.json { render json: @comment, status: :created, location: @comment }
else
format.html { render action: "new" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
您确定当前用户不是nil并且用户已登录吗?我想你用的是Desive gem。你有授权用户的前文件管理器吗?@AntonGrigoryev我确信当前用户是可以的,因为我可以添加实际的唱片集评论。这是一个音乐评论网站,当我添加评论时,应用程序会识别我。我将在上面添加网站url。你能显示评论模型吗?@AntonGrigoryev-Sure-见上面。这些评论被称为pins。每个注释都属于一个用户和一个pin。当你在我的展示页面查看评论pin时,你可以看到它下面的所有评论。同样,这在本地运行良好,因此这是Heroku DB的问题。几天前才开始。在你看来,“评论”是如何获得“@comment”的价值的?
def create
@pin = Pin.find(params[:pin_id])
@comment = @pin.comments.create(params[:comment])
@comment.user = current_user
#MyMailer.comment_email(@pin.user).deliver
respond_to do |format|
if @comment.save
format.html { redirect_to @pin, notice: 'Comment was successfully created.' }
format.json { render json: @comment, status: :created, location: @comment }
else
format.html { render action: "new" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end