当我尝试提交表单 (/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