aquaflow
2007-Jul-22 06:03 UTC
Executing a model from two different controllers - one controller requires authenticated access the other doesn''t
My appllication uses a "Video" table. I have a video_controller and associated video model The video_controller requirs the user to login to the system. class VideoController < ApplicationController before_filter :login_required When I initially wrote code for the Video model, I assumed that only authenticated users would read, write Video records. Hence, I''ve got code in the model such as: @dir = ''public/uploaded_videos'' + "/" + User.current_user.id The above works fine when a logged in user accesses video functions on the site. Now, I want to display a list of videos on the Index/home page of the web application. Users who access the web page don''t need to log in to the system. I created a home_controller with an index method. def index @videos = Video.find(:all, :conditions => ["public = 0"], :select => [''id, file_name, file_size,thumbnail'']) end Whenever, this method is executed, RoR makes a call to the video.rb file and fails on the following line: @dir = ''public/uploaded_videos'' + "/" + User.current_user.id It''s failing because User.current_user.id is null. This value is only populated when a user logs into the system. How can I get around this particular issue? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Shai Rosenfeld
2007-Jul-22 07:44 UTC
Re: Executing a model from two different controllers - one c
not that it makes any difference (you''ll still solve the problem regardless, but for general knowledge, where is the @dir statement being called .. ? either way, just add a ''unless User.current_user.nil?''> @dir = ''public/uploaded_videos'' + "/" + User.current_user.id@dir = ''pub ... + User.current_user.id unless User.current_user.nil? should solve the problem, no? -- 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 -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan
2007-Jul-22 08:28 UTC
Re: Executing a model from two different controllers - one c
aquaflow wrote: Hence, I''ve got> code in the model such as: > @dir = ''public/uploaded_videos'' + "/" + User.current_user.id >I just want to point out something, the code above should not be in the model, but in the helpers. -- 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 -~----------~----~----~----~------~----~------~--~---
aquaflow
2007-Jul-22 15:00 UTC
Re: Executing a model from two different controllers - one c
Thank you. That worked perfectly. I had previsouly tried using an if statement with the same condition: # if !(User.current_user.nil?) # DIRECTORY = ''public/uploaded_videos'' + "/" + User.current_user.id # end It seemed like the if statement was being ignored. @dir is being called from other methods within the model. I''m using it ti define the destination file path for encoded video files. On Jul 22, 12:44 am, Shai Rosenfeld <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> not that it makes any difference (you''ll still solve the problem > regardless, but for general knowledge, where is the @dir statement being > called .. ? > > either way, just add a ''unless User.current_user.nil?'' > > > @dir = ''public/uploaded_videos'' + "/" + User.current_user.id > > @dir = ''pub ... + User.current_user.id unless User.current_user.nil? > > should solve the problem, no? > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---