フォーム(/ 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 />
これを修正するにはどうすればよいですか?
関連するコードは以下のとおりです。
/ VIEWS / POSTS / SHOW
<%= 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