Rails 3.0.3
ruby 1.9.2p0
The Problem:
I have a Users table which has many items, the item(s) in turn therefore belongs to the Users.
In my model item.rb i attempt to save the item along with the value for the user.id so i have:
self.User_ID = @user.id
this however give me the error
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
this is causing some confusion that it can’t find this as in the show.html.erb that ‘wraps’ this page <%= @user.id %> displays the correct ID on the page
Many thanks in advance
** EDIT **
The Shorten action is the action upon which i want to parameter to be passed
class ItemsController < ApplicationController
def redirect
@item = Item.find_by_shortened(params[:shortened])
if @item
#redirect_to @item.original
redirect_to @item.original
else
redirect_to :shorten
end
end
def shorten
@host = request.host_with_port
@user = current_user
You need to load the
@usermodel in every action that will require access to it. Having it render properly in theshowaction will not guarantee it is loaded in theupdateaction.Usually you need to have something like this in your controller: