Hello ... I am using the acts_as_ratable plugin, but am trying to tweak it a bit ... I have this code in my view: <%= render :partial => "ratings/rate", :locals => { :asset => current_user, :thing_id => @thing.id } %> And in my Ratings controller I have: def rate return unless logged_in? rateable = @rateable_class.find(params[:id]) thingID = thing_id # Delete the old ratings for current user Rating.delete_all(["rateable_type = ? AND rateable_id = ? AND user_id = ? AND errand_id = ?", @rateable_class.base_class.to_s, params[:id], @current_user.id, thing_id]) rateable.add_rating Rating.new(:rating => params[:rating], :user_id => @current_user.id, :errand_id => thing_id) render :update do |page| page.replace_html "star-ratings-block-#{rateable.id}", :partial => "rate", :locals => { :asset => rateable, : thing_id => thingID} page.visual_effect :highlight, "star-ratings-block- #{rateable.id}" end end in the RJS file I have: page.replace_html "star-ratings-block-#{current_user.id}", :partial => ''/ratings/rate'', :locals => { :asset => current_user, :thing_id => @thing.id } When I click on a rating, which renders the partial I get this error: NameError (undefined local variable or method `thing_id'' for #<RatingsController:0xb7b40f48>): Why isn''t the local variable I am passing from the partial available to me in my controller? Thanks for any insight!!! LAB --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Feb-28 11:14 UTC
Re: rendering partials with local variables questions
On 28 Feb 2008, at 03:14, LAB wrote:> > Hello ... I am using the acts_as_ratable plugin, but am trying to > tweak it a bit ... > > I have this code in my view: > <%= render :partial => "ratings/rate", :locals => { :asset => > current_user, :thing_id => @thing.id } %> > > And in my Ratings controller I have: > def rate > return unless logged_in? > > rateable = @rateable_class.find(params[:id]) > thingID = thing_idAre you sure the error doesn''t occur here ? thing_id seems to be defined nowhere. Fred>--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
@thing.id is defined in the same view that I am rendering the partial in ... just above my render :partial code. :thing_id, is a new local variable I was trying to pass through the partial. So ...... <%= render :partial => "ratings/rate", :locals => { :asset => current_user, :thing_id => @thing.id } %> I am passing :thing_id = a number, lets say 4, and I want to use that number 4 in the Ratings controller. Basically, I''m just trying to get this information up to the Ratings Controller from the view level. Maybe there is another way? But I still think this should work, and I probably have some sort of syntax wrong? Thanks for any help! On Feb 28, 6:14 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 28 Feb 2008, at 03:14, LAB wrote: > > > > > Hello ... I am using the acts_as_ratable plugin, but am trying to > > tweak it a bit ... > > > I have this code in my view: > > <%= render :partial => "ratings/rate", :locals => { :asset => > > current_user, :thing_id => @thing.id } %> > > > And in my Ratings controller I have: > > def rate > > return unless logged_in? > > > rateable = @rateable_class.find(params[:id]) > > thingID = thing_id > > Are you sure the error doesn''t occur here ? thing_id seems to be > defined nowhere. > > Fred > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Feb-28 12:59 UTC
Re: rendering partials with local variables questions
On 28 Feb 2008, at 12:52, LAB wrote:> > @thing.id is defined in the same view that I am rendering the partial > in ... just above my render :partial code. :thing_id, is a new local > variable I was trying to pass through the partial. So ...... >In the source that you pasted you were access thing_id right at the top of the controller. Is that the line throwing the error ? Fred> <%= render :partial => "ratings/rate", :locals => { :asset => > current_user, :thing_id => @thing.id } %> > > I am passing :thing_id = a number, lets say 4, and I want to use that > number 4 in the Ratings controller. Basically, I''m just trying to get > this information up to the Ratings Controller from the view level. > Maybe there is another way? But I still think this should work, and I > probably have some sort of syntax wrong? > > Thanks for any help! > > On Feb 28, 6:14 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On 28 Feb 2008, at 03:14, LAB wrote: >> >> >> >>> Hello ... I am using the acts_as_ratable plugin, but am trying to >>> tweak it a bit ... >> >>> I have this code in my view: >>> <%= render :partial => "ratings/rate", :locals => { :asset => >>> current_user, :thing_id => @thing.id } %> >> >>> And in my Ratings controller I have: >>> def rate >>> return unless logged_in? >> >>> rateable = @rateable_class.find(params[:id]) >>> thingID = thing_id >> >> Are you sure the error doesn''t occur here ? thing_id seems to be >> defined nowhere. >> >> Fred >> >> > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
LAB wrote:> <%= render :partial => "ratings/rate", :locals => { :asset => > current_user, :thing_id => @thing.id } %> > > I am passing :thing_id = a number, lets say 4, and I want to use that > number 4 in the Ratings controller. Basically, I''m just trying to getRemember that the flow is: Controller action Render view Send view to browser So, your *instance* variables you set in the controller are visible to your view. Anything set in the view can only be used in the view before it is sent to the user. If you want a value to pass to the browser then back to the controller when a link or button is clicked, then you can add the association to the options in an appropriate link_to or button_to call in the view (or use a hidden field). The value can then be used in the next controller action. So, if you set :thing_id in a link_to, you can see it as params[:thing:id] next time. -- 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 -~----------~----~----~----~------~----~------~--~---
Mark Bush wrote:> params[:thing:id] next time.That''s params[:thing_id] of course. -- 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 -~----------~----~----~----~------~----~------~--~---
Thanks, Mark! That sounds like my problem. I didn''t understand that the :locals in the :partial can''t really be used by the controller. Still not intuitive to me ... but okay! Fred - the error is at the top of my controller: NameError (undefined local variable or method `thing_id'' for #<RatingsController:0xb7b40f48>):/app/controllers/ ratings_controller.rb:9:in `rate'' line 9 ... which is: thingID = thing_id This is where I am first trying to use the local variable thing_id in the Ratings Controller. If leveraging the :locals can be done, that would have been much prettier, but I will try passing the thing_id up to the Ratings Controller with a link_to or a form. Thanks!!! LAB On Feb 28, 8:26 am, Mark Bush <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Mark Bush wrote: > > params[:thing:id] next time. > > That''s params[:thing_id] of course. > > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---