3 use case and the index method


#1

I wrote the code below which doesn’t seem to work, but If I delete the
last if statement, it does work, obviously for the first two use cases
only. Is there a better way to write this code? In my routes file I have

map.resources :galleries
map.resources :users, :has_many => :galleries

A user clicks on the link “galleries” and see a list of all published
galleries.
mysite.com/galleries

A user can click on a link “my galleries” and sees all her own
galleries.
mysite.com/users/21/galleries

A user can clicks on a link on some other user’s profile and see that
persons published galleries
mysite.com/users/35/galleries

if params[:user_id].blank?
@galleries = Gallery.find(:all, :conditions => [‘visibility_status
= ?’, true])
end

if (params[:user_id] && current_user.id.to_s == params[:user_id])
@galleries = current_user.galleries
end

if params[:user_id]
@galleries = Gallery.find(:all, :conditions => [‘user_id=? and
visibility_status = ?’, params[:user_id], true])
end


#2

On Wed, Jan 21, 2009 at 10:12 AM, Mohammad A. <
removed_email_address@domain.invalid> wrote:

mysite.com/galleries
@galleries = Gallery.find(:all, :conditions => ['visibility_status
end

What part doesn’t seem to work?

Could it be fixed if you structured the code as:

if params[:user_id].blank?
blah_blah_blah
else
if current_user.id.to_s == params[:user_id]
blah_blah
else
blah
end
end

–wpd


#3

Yep, I did that, I get this error

“Called id for nil, which would mistakenly be 4 – if you really wanted
the id of nil, use object_id”

Patrick D. wrote:

On Wed, Jan 21, 2009 at 10:12 AM, Mohammad A. <
removed_email_address@domain.invalid> wrote:

mysite.com/galleries
@galleries = Gallery.find(:all, :conditions => ['visibility_status
end

What part doesn’t seem to work?

Could it be fixed if you structured the code as:

if params[:user_id].blank?
blah_blah_blah
else
if current_user.id.to_s == params[:user_id]
blah_blah
else
blah
end
end

–wpd


#4

Did you call logged_in? before current_user to make sure that it is
valid?

-Rob


#5

That is not necessary, Now I get the same error for the first if clause
(when the param is null, and I just type “mysite.com/galleries”, the
other two clauses work ok. This what my code looks like

if params[:user_id].blank?
@galleries = Gallery.find(:all, :conditions => [‘visibility_status =
?’, true])
elseif current_user.id.to_s == params[:user_id]
@galleries = current_user.galleries
else
@galleries = Gallery.find(:all, :conditions => [‘user_id=? and
visibility_status = ?’, params[:user_id], true])
end

Rob B. wrote:

Did you call logged_in? before current_user to make sure that it is
valid?

-Rob


#6

Hi –

On Wed, 21 Jan 2009, Mohammad A. wrote:

That is not necessary, Now I get the same error for the first if clause
(when the param is null, and I just type “mysite.com/galleries”, the
other two clauses work ok. This what my code looks like

if params[:user_id].blank?
@galleries = Gallery.find(:all, :conditions => [‘visibility_status =
?’, true])

I’m not sure how that if clause can give a “called id on nil” error,
since it doesn’t call id. Can you examine the stacktrace and see if
you can pinpoint it?

Also, though I know this isn’t your question, I’d advise putting all
of this logic in the model(s). For example:

class User < AR::Base
has_many :galleries
has_many :visible_galleries,
:class_name => “Gallery”,
:conditions => [“visibility_status = ?”, true]

Then you can do:

@galleries = user.visible_galleries

or something like that, in the controller, instead of in-lining the
business logic of the model.

David


David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!


#7

On Jan 21, 2009, at 2:00 PM, Mohammad A. wrote:

Parameters: {“action”=>“index”, “controller”=>“galleries”}
Gallery Load (2.0ms) SELECT * FROM galleries

RuntimeError (Called id for nil, which would mistakenly be 4 – if you
really wanted the id of nil, use object_id):
/app/controllers/galleries_controller.rb:8:in `index’

Aha! OK, what is on line 8? (If that doesn’t solve it for you, paste
the first part of the galleries_controller.rb up to the end of the
index method.)

-Rob


#8

This is line 8
“elseif current_user.id.to_s == params[:user_id]”

And here is the code form the start of the file to the end of the index
method.

class GalleriesController < ApplicationController
#before_filter :require_user, :only => [:new, :create, :edit, :update,
:destroy]

def index

if params[:user_id].blank?
    @galleries = Gallery.find(:all)
elseif current_user.id.to_s == params[:user_id]
     @galleries = current_user.galleries
else
  @user = User.find(params[:user_id])
  @galleries = @user.visible_galleries
end

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @galleries }
end

end

Rob B. wrote:

On Jan 21, 2009, at 2:00 PM, Mohammad A. wrote:

Parameters: {“action”=>“index”, “controller”=>“galleries”}
Gallery Load (2.0ms) SELECT * FROM galleries

RuntimeError (Called id for nil, which would mistakenly be 4 – if you
really wanted the id of nil, use object_id):
/app/controllers/galleries_controller.rb:8:in `index’

Aha! OK, what is on line 8? (If that doesn’t solve it for you, paste
the first part of the galleries_controller.rb up to the end of the
index method.)

-Rob


#9

Thanks for the advice David, I did remove the code from the controller.
I’ve looked the stack trace. What is interesting, it does generate the
correct sql. Unfortunately, I don’t understand enough about RoR to
decipher the log


Processing GalleriesController#index (for 127.0.0.1 at 2009-01-21
13:56:31) [GET]
Parameters: {“action”=>“index”, “controller”=>“galleries”}
Gallery Load (2.0ms) SELECT * FROM galleries

RuntimeError (Called id for nil, which would mistakenly be 4 – if you
really wanted the id of nil, use object_id):
/app/controllers/galleries_controller.rb:8:in index' /vendor/rails/actionpack/lib/action_controller/base.rb:1253:insend’
/vendor/rails/actionpack/lib/action_controller/base.rb:1253:in
perform_action_without_filters' /vendor/rails/actionpack/lib/action_controller/filters.rb:617:incall_filters’
/vendor/rails/actionpack/lib/action_controller/filters.rb:610:in
perform_action_without_benchmark' /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue’
/usr/local/lib/ruby/1.8/benchmark.rb:293:in measure' /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue’
/vendor/rails/actionpack/lib/action_controller/rescue.rb:136:in
perform_action_without_caching' /vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:13:inperform_action’
/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in
cache' /vendor/rails/activerecord/lib/active_record/query_cache.rb:8:incache’
/vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:12:in
perform_action' /vendor/rails/actionpack/lib/action_controller/base.rb:524:insend’
/vendor/rails/actionpack/lib/action_controller/base.rb:524:in
process_without_filters' /vendor/rails/actionpack/lib/action_controller/filters.rb:606:inprocess_without_session_management_support’
/vendor/rails/actionpack/lib/action_controller/session_management.rb:134:in
process' /vendor/rails/actionpack/lib/action_controller/base.rb:392:inprocess’
/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:183:in
handle_request' /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:110:indispatch_unlocked’
/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:123:in
dispatch' /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:122:insynchronize’
/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:122:in
dispatch' /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:132:indispatch_cgi’
/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:39:in
dispatch' /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:76:inprocess’
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/…/lib/mongrel/rails.rb:74:in
synchronize' /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:74:inprocess’
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in
process_client' /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:ineach’
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in
process_client' /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:inrun’
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in
initialize' /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:innew’
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in
run' /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:ininitialize’
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in
new' /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:inrun’
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:in
run' /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:ineach’
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in
run' /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:128:inrun’
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/command.rb:212:in
run' /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281 /vendor/rails/activesupport/lib/active_support/dependencies.rb:142:inload_without_new_constant_marking’
/vendor/rails/activesupport/lib/active_support/dependencies.rb:142:in
load' /vendor/rails/activesupport/lib/active_support/dependencies.rb:521:innew_constants_in’
/vendor/rails/activesupport/lib/active_support/dependencies.rb:142:in
load' /vendor/rails/railties/lib/commands/servers/mongrel.rb:64 /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:ingem_original_require’
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
require' /vendor/rails/activesupport/lib/active_support/dependencies.rb:153:inrequire’
/vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in
new_constants_in' /vendor/rails/activesupport/lib/active_support/dependencies.rb:153:inrequire’
/vendor/rails/railties/lib/commands/server.rb:49
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
gem_original_require' /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:inrequire’
script/server:3

David A. Black wrote:

Hi –

On Wed, 21 Jan 2009, Mohammad A. wrote:

That is not necessary, Now I get the same error for the first if clause
(when the param is null, and I just type “mysite.com/galleries”, the
other two clauses work ok. This what my code looks like

if params[:user_id].blank?
@galleries = Gallery.find(:all, :conditions => [‘visibility_status =
?’, true])

I’m not sure how that if clause can give a “called id on nil” error,
since it doesn’t call id. Can you examine the stacktrace and see if
you can pinpoint it?

Also, though I know this isn’t your question, I’d advise putting all
of this logic in the model(s). For example:

class User < AR::Base
has_many :galleries
has_many :visible_galleries,
:class_name => “Gallery”,
:conditions => [“visibility_status = ?”, true]

Then you can do:

@galleries = user.visible_galleries

or something like that, in the controller, instead of in-lining the
business logic of the model.

David


David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!


#10

Hmm! If the I Sign in to the site. I type the url “mysite.com/galleries
I get a different error

“undefined method `elseif’ for #GalleriesController:0x3517a18

Mohammad A. wrote:

This is line 8
“elseif current_user.id.to_s == params[:user_id]”

And here is the code form the start of the file to the end of the index
method.

class GalleriesController < ApplicationController
#before_filter :require_user, :only => [:new, :create, :edit, :update,
:destroy]

def index

if params[:user_id].blank?
    @galleries = Gallery.find(:all)
elseif current_user.id.to_s == params[:user_id]
     @galleries = current_user.galleries
else
  @user = User.find(params[:user_id])
  @galleries = @user.visible_galleries
end

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @galleries }
end

#11

Hi –

On Wed, 21 Jan 2009, Mohammad A. wrote:

def index

if params[:user_id].blank?
@galleries = Gallery.find(:all)
elseif current_user.id.to_s == params[:user_id]
@galleries = current_user.galleries

One way or another, current_user is returning nil. So you’d have to
track that down and fix it, or work around not having a current user
in this action.

David


David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!


#12

Hi –

On Wed, 21 Jan 2009, Mohammad A. wrote:

Hmm! If the I Sign in to the site. I type the url “mysite.com/galleries
I get a different error

Getting a different error is always a hopeful sign :slight_smile:

“undefined method `elseif’ for #GalleriesController:0x3517a18

It’s elsif (no second e).

David


David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!


#13

Yep, It does take a while to learn Ruby! This worked when I reordered
the if statements and checked if the current_user is nil. Thanks David
for pointing me in the right direction, also thanks to Rob for
mentioning it above. Here is what worked

if params[:user_id]
@user = current_user
if params[:user_id] && !@user.nil?
@galleries = @user.galleries
end
@user = User.find(params[:user_id])
@galleries = @user.visible_galleries
else
galleries = Gallery.find(:all, :conditions => [‘visibility_status =
?’, true])
end

David A. Black wrote:

Hi –

On Wed, 21 Jan 2009, Mohammad A. wrote:>

def index

if params[:user_id].blank?
@galleries = Gallery.find(:all)
elseif current_user.id.to_s == params[:user_id]
@galleries = current_user.galleries

One way or another, current_user is returning nil. So you’d have to
track that down and fix it, or work around not having a current user
in this action.

David


David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!


#14

Thanks David, yeah, corrected to elsif, that doesn’t work either!. I’ve
something that works for the most part. The only case where my code
doesn’t work: If the user is signed-in, it does pull all the user
gallery, I get the same galleries as if I called user.visible_galleries

if params[:user_id]
@user = User.find(params[:user_id])
@galleries = @user.visible_galleries
elsif params[:user_id] && current_user && params[:user_id] ==
current_user.id
@galleries = current_user.galleries
else
@galleries = Gallery.find(:all, :conditions => [‘visibility_status =
?’, true])
end

David A. Black wrote:

Hi –

On Wed, 21 Jan 2009, Mohammad A. wrote:

Hmm! If the I Sign in to the site. I type the url “mysite.com/galleries
I get a different error

Getting a different error is always a hopeful sign :slight_smile:

“undefined method `elseif’ for #GalleriesController:0x3517a18

It’s elsif (no second e).

David


David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!