Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7849393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:30:19+00:00 2026-06-02T18:30:19+00:00

I’m getting this error when I try to submit my form (/POSTS/SHOW): RuntimeError in

  • 0

I’m getting this error when I try to submit my form (/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 />

How do I fix this?

Relevant code is below:

/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">

Hello! My name is <%= f.text_field :subject %> and I’m contacting you in response to your ad. I’m interested in learning more so get in touch! Here’s my contact details: <%= f.text_field :body %>.

Submit
<% end %>

POST MODEL

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

USER MODEL

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

POSTS CONTROLLER

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 

APPLICATION CONTROLLER (this is where I am defining 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

MAIN CONTROLLER (send_message is defined here)

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:30:20+00:00Added an answer on June 2, 2026 at 6:30 pm

    You don’t have a user assigned to the post record represented by the @post instance variable.

    Presumably a user needs to be logged in to make a post?
    Also presumably you have a current user defined somewhere?

    Your controller actions that use this form need to assign the user to the post record

    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
    

    UPDATE

    To make sure that your current user is assigned you should add a check to ensure the user is logged in in the controller actions. This is normally done by adding a before filter to authorize the current user which will redirect back to the login page if the current use is logged out.
    Have a look at this rails cast to explain logging in and out and redirecting on a before filter http://railscasts.com/episodes/250-authentication-from-scratch

    There is a revised version of the cast here but you will need a subscription for that
    http://railscasts.com/episodes/250-authentication-from-scratch-revised

    well worth paying for IMO

    End of update

    You will need to / should also assign the current user in whatever actions update the post record – i.e. the create and update actions in EXACTLY the same way.

    Also, because you have not got a user assigned to a post record then you need to handle this scenario in the form so that you don’t get 500 errors

    You can use the @post.user.blank? boolean check to help you with this
    Something like

    <% 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 %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.