I used scaffold to generate a product The scaffold created some code in the product controller def create @product = Product.new(params[:product]) respond_to do |format| if @product.save flash[:notice] = ''Product was successfully formcreated.'' format.html { redirect_to(@product) } format.xml { render :xml => @product, :status => :created, :location => @product } else format.html { render :action => "new" } format.xml { render :xml => @product.errors, :status => :unprocessable_entity } end end end format.html {redirect_to(@product)} I could not understand the this line and since its does not contain any action. pls help in understanding the code. --~--~---------~--~----~------------~-------~--~----~ 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 13 Apr 2008, at 03:28, Duke wrote:> > I used scaffold to generate a product > The scaffold created some code in the product controller > def create > @product = Product.new(params[:product]) > > respond_to do |format| > if @product.save > flash[:notice] = ''Product was successfully formcreated.'' > format.html { redirect_to(@product) } > format.xml { render :xml => @product, :status > => :created, :location => @product } > else > format.html { render :action => "new" } > format.xml { render :xml => @product.errors, :status > => :unprocessable_entity } > end > end > end > > format.html {redirect_to(@product)}That says ''if the client requested html, the redirect them to the url that shows @product''. Because you''ve got your nice restful routing redirect_to knows that it should send you to /products/show/123 (assuming that the id of @product is 123) Fred> > I could not understand the this line and since its does not contain > any action. > pls help in understanding the code. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Duke, The respond_to is a new feature of Rails 2 (It might have been in rails 1.2 at some point) and is intended to let you define different responses depending upon the "type" of the request. The most standard examples are: - normal HTML request, you respond with a normal Rails response, usually a default render, another render or a redirect - XML request, you respond with rendering XML, usually with the object or objects - RJS request, you respond with an update block or with an RJS file, usually because of AJAX calls So, when you have: format.html { redirect_to(@product) } It''s saying: if the "type" of the request is a normal HTML request, then redirect to the URL for handling a specific product. This is the "show" action, by default, in REST-style routes. So, it''s the functional equivalent of: redirect_to :action => "show", :controller => "products", :id => @product.id Also, the respond_to is nice if you need it, but also unnecessary. If your app doesn''t need to support it, just take it off, which means that method could simply be: def create @product = Product.new(params[:product]) if @product.save flash[:notice] = ''Product was successfully created.'' redirect_to(@product) else render :action => "new" end end HTH! -Danimal On Apr 12, 8:28 pm, Duke <p.sathish...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I used scaffold to generate a product > The scaffold created some code in the product controller > def create > @product = Product.new(params[:product]) > > respond_to do |format| > if @product.save > flash[:notice] = ''Product was successfully formcreated.'' > format.html { redirect_to(@product) } > format.xml { render :xml => @product, :status > => :created, :location => @product } > else > format.html { render :action => "new" } > format.xml { render :xml => @product.errors, :status > => :unprocessable_entity } > end > end > end > > format.html {redirect_to(@product)} > I could not understand the this line and since its does not contain > any action. > pls help in understanding the code.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---