1

当我尝试提交表单 (/POSTS/SHOW) 时出现此错误:

RuntimeError in Posts#show

Showing /Users/fkhalid2008/loand/app/views/posts/show.html.erb where line #1 raised:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Extracted source (around line #1):

1: <%= form_remote_tag (:update => 'message', :url => {:controller => 'main', :action => 'send_message', :user_id => @post.user.id}) do %>
2: <br>
3: <br />
4: <br />

我该如何解决?

相关代码如下:

/观点/帖子/节目

<%= form_remote_tag (:update => 'message', :url => {:controller => 'main', :action => 'send_message', :user_id => @post.user.id}) do %>
<br>
<br />
<br />
<div class="field">

你好!我的名字是 <%= f.text_field :subject %>,我正在与您联系以回应您的广告。我有兴趣了解更多,所以取得联系!这是我的联系方式:<%= f.text_field :body %>。提交 <% end %>

后模型

class Post < ActiveRecord::Base

belongs_to :user

attr_accessible :title, :job, :location, :salary

validates :title, :job, :location, :salary, :presence => true 
validates :salary, :numericality => {:greater_than_or_equal_to => 1} 

default_scope :order => 'posts.created_at DESC'
end

用户模型

class User < ActiveRecord::Base

has_many :posts  
has_one :profile
has_private_messages

attr_accessible :email

validates_presence_of :email
validates_uniqueness_of :email, :message =>"Hmm, that email's already taken"
validates_format_of :email, :with => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i, :message => "Hi! Please use a valid email"


end

帖子控制器

def show
@post = Post.find(params[:id]) 

respond_to do |format|
  format.html # show.html.erb
  format.json { render :json => @post }
end
end

def new
@post = Post.new
@post.user = current_user

respond_to do |format|
  format.html # new.html.erb
  format.json { render :json => @post }
end
end

def edit
@post = Post.find(params[:id])
end

def create
    @post = Post.new(params[:post])
    @post.user = current_user

    respond_to do |format|
        if verify_recaptcha && @post.save
            format.html { redirect_to :action=> "index"}
            format.json { render :json => @post, :status => :created, :location => @post }
        else
            format.html { render :action => "new" }
            format.json { render :json => @post.errors, :status => :unprocessable_entity }
        end
    end
end

def update
@post = Post.find(params[:id])
@post.user = current_user

respond_to do |format|
  if @post.update_attributes(params[:post])
    format.html { redirect_to @post, :notice => 'Post was successfully updated.' }
    format.json { head :ok }
  else
    format.html { render :action => "edit" }
    format.json { render :json => @post.errors, :status => :unprocessable_entity }
  end
end
end 

应用程序控制器(这是我定义 current_user 的地方)

class ApplicationController < ActionController::Base
protect_from_forgery

private

def current_user
    @_current_user ||= session[:current_user_id] &&
    User.find_by_id(session[:current_user_id])
end

end

主控制器(send_message 在这里定义)

class MainController < ApplicationController

def send_message
message = Message.new
message.subject = params[:subject]
message.body = params[:message]
message.sender = User.find session[:user]
message.recipient = User.find params[:user_id]
if message.save
  ContactMailer.deliver_message_email message.recipient.email, message.id, request.host
  return redirect_to "/posts"
else
  render :text => "Hmm. Something seems to be wrong...let me look into it"
end
end
4

2 回答 2

1

您收到错误是@post.user因为nil:user_id => @post.user.id.

确保您@post在后期控制器的显示操作中定义并且它具有有效的用户关联。

于 2012-04-23T09:45:43.250 回答
1

您没有将用户分配给由 @post 实例变量表示的帖子记录。

大概用户需要登录才能发帖?也可能你在某处定义了当前用户?

使用此表单的控制器操作需要将用户分配给发布记录

def new
  @post = Post.new
  @post.user = current_user # You will need to get the current user from somewhere
  respond_to do |format|
    format.html # new.html.erb
    format.json { render :json => @post }
  end
end

更新

为了确保您当前的用户被分配,您应该添加一个检查以确保用户在控制器操作中登录。这通常是通过添加一个 before 过滤器来授权当前用户,如果当前用户退出,它将重定向回登录页面。看看这个 rails cast 来解释登录和注销以及在过滤器之前重定向http://railscasts.com/episodes/250-authentication-from-scratch

这里有演员表的修订版,但您需要订阅 http://railscasts.com/episodes/250-authentication-from-scratch-revised

值得为 IMO 支付费用

更新结束

您将需要/还应该在更新帖子记录的任何操作中分配当前用户 - 即以完全相同的方式创建和更新操作。

此外,由于您没有将用户分配给发布记录,因此您需要在表单中处理这种情况,以免出现 500 错误

您可以使用@post.user.blank?布尔检查以帮助您解决此问题

<% if @post.user.blank? %>
  <h2>There is no user assigned to this post record! This should never happen ad you should never see this message, please contact support if etc... </h2>
<% else %>
<!-- Place all your current form code here -->
<% end %>
于 2012-04-23T11:51:52.383 回答