RailsNewbie
2011-Apr-10 19:11 UTC
Passing a parameter from a view to a different controller
I am new to Rails and have a simple problem where I am trying to pass a parameter from a view to another controller outside the scope...The model is about a Customer having multiple Prescriptions. The GET request is : /prescription/list_per_customer/2 This is from the view of Customers : <%= link_to "Customers Prescription", :action => "list_per_customer", :controller => "prescription", :id => @customer.id %> This is from the other controller, PrescriptionController : def index @referred_customer=params[:id] end def list_per_customer @cust_prescriptions Prescription.find_all_by_customer_id(@referred_customer) redirect_to :action => ''list_per_customer'' end How can this be done with or without sessions ? Will appreciate very much if the complete code is provided for these two parts of the code in Customer->view.rhtml and Prescription->list_per_customer...thx -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Apr-11 07:10 UTC
Re: Passing a parameter from a view to a different controller
On Apr 10, 8:11 pm, RailsNewbie <dba...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am new to Rails and have a simple problem where I am trying to pass > a parameter from a view to another controller outside the scope...The > model is about a Customer having multiple Prescriptions. The GET > request is : /prescription/list_per_customer/2 >There''s really not any difference between a link that will be routed to the same controller and one that won''t.> This is from the view of Customers : > > <%= link_to "Customers Prescription", :action => > "list_per_customer", :controller => "prescription", :id => @customer.id > %>This is fine as long as their is a route backing this (the default one will do if it exists)> > This is from the other controller, PrescriptionController : > > def index > @referred_customer=params[:id] > end > def list_per_customer > @cust_prescriptions > Prescription.find_all_by_customer_id(@referred_customer) > redirect_to :action => ''list_per_customer'' > endYou haven''t said what isn''t working, but at the very least you need to assign params[:id] to @referred_customer (although personally I find that a bit misleading - it makes it sound like @referred_customer is a customer object rather than just an id) and I doubt you want to redirect - presumably you have a list_by_customer template that knows how to display the prescriptions. If you''re new I''d also try using more of the rails conventions (eg make prescriptions a nested resource (see the routing guide on guides.rubyonrails.org for more info) Fred> > How can this be done with or without sessions ? Will appreciate very > much if the complete code is provided for these two parts of the code > in Customer->view.rhtml and Prescription->list_per_customer...thx-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Dibyendu Baksi
2011-Apr-13 02:53 UTC
Re: Re: Passing a parameter from a view to a different controller
Thanks a lot, there was a problem in the list_per_customer template..it works now. On Mon, Apr 11, 2011 at 3:10 AM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Apr 10, 8:11 pm, RailsNewbie <dba...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I am new to Rails and have a simple problem where I am trying to pass > > a parameter from a view to another controller outside the scope...The > > model is about a Customer having multiple Prescriptions. The GET > > request is : /prescription/list_per_customer/2 > > > There''s really not any difference between a link that will be routed > to the same controller and one that won''t. > > > This is from the view of Customers : > > > > <%= link_to "Customers Prescription", :action => > > "list_per_customer", :controller => "prescription", :id => @customer.id > > %> > This is fine as long as their is a route backing this (the default one > will do if it exists) > > > > > This is from the other controller, PrescriptionController : > > > > def index > > @referred_customer=params[:id] > > end > > def list_per_customer > > @cust_prescriptions > > Prescription.find_all_by_customer_id(@referred_customer) > > redirect_to :action => ''list_per_customer'' > > end > > You haven''t said what isn''t working, but at the very least you need to > assign params[:id] to @referred_customer (although personally I find > that a bit misleading - it makes it sound like @referred_customer is a > customer object rather than just an id) and I doubt you want to > redirect - presumably you have a list_by_customer template that knows > how to display the prescriptions. > If you''re new I''d also try using more of the rails conventions (eg > make prescriptions a nested resource (see the routing guide on > guides.rubyonrails.org for more info) > > Fred > > > > How can this be done with or without sessions ? Will appreciate very > > much if the complete code is provided for these two parts of the code > > in Customer->view.rhtml and Prescription->list_per_customer...thx > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.