简体   繁体   English

user_id:无(在Heroku上的Rails应用程序中发布评论时)

[英]user_id:nil when a comment is posted to Rails app on Heroku

My app is giving me an error when I try to view show page. 尝试查看显示页面时,我的应用程序给我一个错误。 Here is the error: 这是错误:

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? %>    

Whats happening is that when the app tries to display the show view, it looks at a section I display at the bottom for comments. 发生的情况是,当应用尝试显示显示视图时,它会显示在底部显示的部分以供注释。 For some reason, the comments are now posting with user_id:nil. 由于某种原因,这些评论现在使用user_id:nil发布。 It works fine locally. 它在本地工作正常。 Here's my comment controller: 这是我的评论控制器:

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     

Comment Model: 评论模型:

class Comment < ActiveRecord::Base
 attr_accessible :body, :pin_id, :author
 belongs_to :pin
 belongs_to :user
end

I assume it has some issue all of a sudden with @comment.user = current_user. 我认为@ comment.user = current_user突然有一些问题。

I appreciate any help, and let me know if you need more info. 感谢您的帮助,如果需要更多信息,请告诉我。 Here is the live site: www.thetens.us and github: https://github.com/Tambe257/thetens 这是实时站点:www.thetens.us和github: https : //github.com/Tambe257/thetens

My mailer information has changed and when teh app tries to email, it can't send. 我的邮件信息已更改,当该应用尝试发送电子邮件时,无法发送。 Because the controller tries to execute the email when it saves the post, it's corrupting the file that it's saving. 由于控制器在保存帖子时尝试执行电子邮件,因此破坏了它保存的文件。

I commented out the mailer temporarily until I fix that issue and it works fine now. 我暂时注释了邮件程序,直到解决该问题为止,现在它可以正常工作。

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM