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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Wed, Jan 21, 2009 at 10:12 AM, Mohammad Abed < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 > -- >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Doyle wrote:> On Wed, Jan 21, 2009 at 10:12 AM, Mohammad Abed < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Did you call logged_in? before current_user to make sure that it is valid? -Rob On Jan 21, 2009, at 12:58 PM, Mohammad Abed wrote:> > 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 Doyle wrote: >> On Wed, Jan 21, 2009 at 10:12 AM, Mohammad Abed < >> rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Biedenharn wrote:> Did you call logged_in? before current_user to make sure that it is > valid? > > -Rob-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi -- On Wed, 21 Jan 2009, Mohammad Abed 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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:in `send'' /vendor/rails/actionpack/lib/action_controller/base.rb:1253:in `perform_action_without_filters'' /vendor/rails/actionpack/lib/action_controller/filters.rb:617:in `call_filters'' /vendor/rails/actionpack/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'' /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'' /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'' /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_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:in `perform_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:in `cache'' /vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:12:in `perform_action'' /vendor/rails/actionpack/lib/action_controller/base.rb:524:in `send'' /vendor/rails/actionpack/lib/action_controller/base.rb:524:in `process_without_filters'' /vendor/rails/actionpack/lib/action_controller/filters.rb:606:in `process_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:in `process'' /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:183:in `handle_request'' /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:110:in `dispatch_unlocked'' /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:123:in `dispatch'' /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:122:in `synchronize'' /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:122:in `dispatch'' /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:132:in `dispatch_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:in `process'' /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:in `process'' /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:in `each'' /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:in `run'' /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:in `new'' /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:in `initialize'' /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:in `run'' /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:in `each'' /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:in `run'' /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:in `load_without_new_constant_marking'' /vendor/rails/activesupport/lib/active_support/dependencies.rb:142:in `load'' /vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_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:in `gem_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:in `require'' /vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in'' /vendor/rails/activesupport/lib/active_support/dependencies.rb:153:in `require'' /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:in `require'' script/server:3 David A. Black wrote:> Hi -- > > On Wed, 21 Jan 2009, Mohammad Abed 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!-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 21, 2009, at 2:00 PM, Mohammad Abed wrote:> > 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''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> > /vendor/rails/actionpack/lib/action_controller/base.rb:1253:in > `send'' > /vendor/rails/actionpack/lib/action_controller/base.rb:1253:in > `perform_action_without_filters'' > /vendor/rails/actionpack/lib/action_controller/filters.rb:617:in > `call_filters'' > /vendor/rails/actionpack/lib/action_controller/filters.rb:610:in > `perform_action_without_benchmark'' > /vendor/rails/actionpack/lib/action_controller/benchmarking.rb: > 68:in > `perform_action_without_rescue'' > /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'' > /vendor/rails/actionpack/lib/action_controller/benchmarking.rb: > 68:in > `perform_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:in > `perform_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:in > `cache'' > /vendor/rails/actionpack/lib/action_controller/caching/ > sql_cache.rb:12:in > `perform_action'' > /vendor/rails/actionpack/lib/action_controller/base.rb:524:in > `send'' > /vendor/rails/actionpack/lib/action_controller/base.rb:524:in > `process_without_filters'' > /vendor/rails/actionpack/lib/action_controller/filters.rb:606:in > `process_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:in > `process'' > /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:183:in > `handle_request'' > /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:110:in > `dispatch_unlocked'' > /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:123:in > `dispatch'' > /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:122:in > `synchronize'' > /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:122:in > `dispatch'' > /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:132:in > `dispatch_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:in > `process'' > /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:in > `process'' > /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:in > `each'' > /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:in > `run'' > /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:in > `new'' > /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:in > `initialize'' > /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:in > `run'' > /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:in > `each'' > /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:in > `run'' > /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:in > `load_without_new_constant_marking'' > /vendor/rails/activesupport/lib/active_support/dependencies.rb: > 142:in > `load'' > /vendor/rails/activesupport/lib/active_support/dependencies.rb: > 521:in > `new_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:in > `gem_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:in > `require'' > /vendor/rails/activesupport/lib/active_support/dependencies.rb: > 521:in > `new_constants_in'' > /vendor/rails/activesupport/lib/active_support/dependencies.rb: > 153:in > `require'' > /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:in > `require'' > script/server:3 > > > > > > David A. Black wrote: >> Hi -- >> >> On Wed, 21 Jan 2009, Mohammad Abed 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! > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Biedenharn wrote:> On Jan 21, 2009, at 2:00 PM, Mohammad Abed 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi -- On Wed, 21 Jan 2009, Mohammad Abed 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.galleriesOne 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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Abed 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Abed 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!-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi -- On Wed, 21 Jan 2009, Mohammad Abed wrote:> > Hmm! If the I Sign in to the site. I type the url "mysite.com/galleries" > I get a different errorGetting a different error is always a hopeful sign :-)> "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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Abed 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 :-) > >> "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!-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---