i''m having a routing problem with my UPDATE action. I''ve pasted my CATEGORIES controller and view below. I ran scaffold for categories and i consolidated everything into a single view so that I could show list, create new, edit, destroy all from a single page. my create, destroy actions work and my index view works as expected. my UPDATE action however returns the following: Processing CategoriesController#16 (for 127.0.0.1 at 2008-09-08 13:08:22) [POST] Session ID: c05797629988686fd3951de29fca7e24 Parameters: {"commit"=>"Update", "category"=>{"name"=>"Entertainment", "description"=>"Latest gossip about your favourite stars12", "short_name"=>"entertainment"}, "authenticity_token"=>"b6a61d7dc0890d83a923366cd5c93e672b16f62e", "action"=>"16", "controller"=>"categories"} and it identifies the object id as my action and redirects me to / categories/:id. it should redirect me to simply /categories with a flash notice saying my object has been updated. i should also note that it also doesn''t save the updated entry to my database. I''m running rails 2.1 and I haven''t changed my default routes in routes.rb Thanks for your help. HERE''S MY CONTROLLER class CategoriesController < ApplicationController layout ''staff'' #verify :method => :post, :only => [ :destroy, :create, :update ], #:redirect_to => { :action => :list } # GET /categories # GET /categories.xml def index list render :action => ''list'' end def list @categories = Category.find(:all) @category = Category.find(params[:id]) if params[:id] @category = Category.new if @category.nil? #respond_to do |format| #format.html # index.html.erb #format.xml { render :xml => @categories } #end end # POST /categories # POST /categories.xml def create @category = Category.new(params[:category]) respond_to do |format| if @category.save flash[:notice] = ''Category was successfully created.'' format.html { redirect_to categories_url } format.xml { render :xml => @category, :status => :created, :location => @category } else format.html { render categories_url } format.xml { render :xml => @category.errors, :status => :unprocessable_entity } end end end # PUT /categories/1 # PUT /categories/1.xml def update @category = Category.find(params[:id]) respond_to do |format| if @category.update_attributes(params[:category]) flash[:notice] = ''Category was successfully updated.'' format.html { redirect_to categories_url } format.xml { render :xml => @category, :status => :created, :location => @category } else format.html { render categories_url } format.xml { render :xml => @category.errors, :status => :unprocessable_entity } end end end # DELETE /categories/1 # DELETE /categories/1.xml def destroy @category = Category.find(params[:id]) @category.destroy respond_to do |format| flash[:notice] = ''Category was successfully removed.'' format.html { redirect_to categories_url } format.xml { head :ok } end end end HERE''S MY VIEW --> categories/list.html.erb <% @page_title = ''Categories'' -%> <%= content_tag(''p'', link_to(''« Back to Menu'', :controller => ''staff'', :action => ''menu'')) %> <table> <tr> <th>Name</th> <th>Short Name</th> <th>Description</th> </tr> <% for category in @categories -%> <tr class=''<%= cycle(''row1'', ''row2'')%>''> <td><%= h(category.name) -%></td> <td><%= h(category.short_name) -%></td> <td><%= h(category.description) -%></td> <td><%= link_to(''Edit'', :action => ''list'', :id => category) -%></ td> <td><%= link_to(''Delete'', {:action => ''destroy'', :id => category}, :confirm => ''Are you sure you want to remove this category?'', :method => :delete) -%></td> </tr> <% end %> </table> <p><%= link_to(''New Category'', categories_url) %></p> <% form_tag(params[:id].blank? ? {:action => ''create''} : {:action => ''update'', :id => @category}) do -%> <%= error_messages_for ''category'' -%> <table> <tr> <th>Name</th> <th>Short Name</th> <th>Description</th> </tr> <tr> <td><%= text_field(:category, :name, :size => 20) -%></td> <td><%= text_field(:category, :short_name, :size => 20) -%></td> <td><%= text_field(:category, :description, :size => 40) -%></td> <td><%= submit_tag(params[:id].blank? ? ''Create'' : ''Update'') -%></ td> </tr> </table> <% end %> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
What does your routes.rb look like? Does it have map.resources :categories in it? For the create/update form try: <% form_for @category do |c| %> <%= error_messages_for ''category'' -%> <table> <tr> <th>Name</th> etc... </tr> <tr> <td><%= c.text_field :name %></td> etc... <td><%= submit_tag ''save'' %></ td> </tr> </table> <% end %> On Sep 8, 6:26 pm, chris <chris.ye...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> i''m having a routing problem with my UPDATE action. I''ve pasted my > CATEGORIES controller and view below. I ran scaffold for categories > and i consolidated everything into a single view so that I could show > list, create new, edit, destroy all from a single page. my create, > destroy actions work and my index view works as expected. my UPDATE > action however returns the following: > > Processing CategoriesController#16 (for 127.0.0.1 at 2008-09-08 > 13:08:22) [POST] > Session ID: c05797629988686fd3951de29fca7e24 > Parameters: {"commit"=>"Update", > "category"=>{"name"=>"Entertainment", "description"=>"Latest gossip > about your favourite stars12", "short_name"=>"entertainment"}, > "authenticity_token"=>"b6a61d7dc0890d83a923366cd5c93e672b16f62e", > "action"=>"16", "controller"=>"categories"} > > and it identifies the object id as my action and redirects me to / > categories/:id. it should redirect me to simply /categories with a > flash notice saying my object has been updated. i should also note > that it also doesn''t save the updated entry to my database. > > I''m running rails 2.1 and I haven''t changed my default routes in > routes.rb > > Thanks for your help. > > HERE''S MY CONTROLLER > class CategoriesController < ApplicationController > > layout ''staff'' > > #verify :method => :post, :only => [ :destroy, :create, :update ], > #:redirect_to => { :action => :list } > > # GET /categories > # GET /categories.xml > def index > list > render :action => ''list'' > end > > def list > @categories = Category.find(:all) > @category = Category.find(params[:id]) if params[:id] > @category = Category.new if @category.nil? > > #respond_to do |format| > #format.html # index.html.erb > #format.xml { render :xml => @categories } > #end > end > > # POST /categories > # POST /categories.xml > def create > @category = Category.new(params[:category]) > > respond_to do |format| > if @category.save > flash[:notice] = ''Category was successfully created.'' > format.html { redirect_to categories_url } > format.xml { render :xml => @category, :status > => :created, :location => @category } > else > format.html { render categories_url } > format.xml { render :xml => @category.errors, :status > => :unprocessable_entity } > end > end > end > > # PUT /categories/1 > # PUT /categories/1.xml > def update > @category = Category.find(params[:id]) > > respond_to do |format| > if @category.update_attributes(params[:category]) > flash[:notice] = ''Category was successfully updated.'' > format.html { redirect_to categories_url } > format.xml { render :xml => @category, :status > => :created, :location => @category } > else > format.html { render categories_url } > format.xml { render :xml => @category.errors, :status > => :unprocessable_entity } > end > end > end > > # DELETE /categories/1 > # DELETE /categories/1.xml > def destroy > @category = Category.find(params[:id]) > @category.destroy > > respond_to do |format| > flash[:notice] = ''Category was successfully removed.'' > format.html { redirect_to categories_url } > format.xml { head :ok } > end > end > > end > > HERE''S MY VIEW --> categories/list.html.erb > <% @page_title = ''Categories'' -%> > > <%= content_tag(''p'', link_to(''« Back to Menu'', :controller => > ''staff'', :action => ''menu'')) %> > > <table> > <tr> > <th>Name</th> > <th>Short Name</th> > <th>Description</th> > </tr> > > <% for category in @categories -%> > <tr class=''<%= cycle(''row1'', ''row2'')%>''> > <td><%= h(category.name) -%></td> > <td><%= h(category.short_name) -%></td> > <td><%= h(category.description) -%></td> > <td><%= link_to(''Edit'', :action => ''list'', :id => category) -%></ > td> > <td><%= link_to(''Delete'', {:action => ''destroy'', :id => category}, > :confirm => ''Are you sure you want to remove this category?'', :method > => :delete) -%></td> > </tr> > <% end %> > </table> > > <p><%= link_to(''New Category'', categories_url) %></p> > > <% form_tag(params[:id].blank? ? {:action => ''create''} : {:action => > ''update'', :id => @category}) do -%> > <%= error_messages_for ''category'' -%> > > <table> > <tr> > <th>Name</th> > <th>Short Name</th> > <th>Description</th> > </tr> > <tr> > <td><%= text_field(:category, :name, :size => 20) -%></td> > <td><%= text_field(:category, :short_name, :size => 20) -%></td> > <td><%= text_field(:category, :description, :size => 40) -%></td> > <td><%= submit_tag(params[:id].blank? ? ''Create'' : ''Update'') -%></ > td> > </tr> > </table> > <% end %>--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
hey. thanks for the reply. my routes.rb does contain: map.resources :categories. i''m not sure how i would use a form_for in place of the form_tag. if you look at my form_tag line of code, it reads: <%# form_tag(params[:id].blank? ? {:action => ''create''} : {:controller => ''categories'', :action => ''update'', :id => @category}) do -%> I use a ternary operator to execute either the ''create'' action or the ''update'' action depending on whether or not params or passed. The idea is that if the params are blank, the form is to create a new category and thus the fields will be blank. If the params exist, the form is to update an existing category and thus the fields will be populated with the current params. the same goes for my submit_tag code which i''ve also applied a ternary operator with the same idea in mind: <td><%= submit_tag(params[:id].blank? ? ''Create'' : ''Update'') -%></td> again, when i click the ''update'' button, it redirects me to http://localhost:3000/users/:id and sends the :id as the action. thanks, On Sep 9, 4:10 pm, 0li <otur...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> What does your routes.rb look like? Does it have > map.resources :categories in it? > > For the create/update form try: > > <% form_for @category do |c| %> > <%= error_messages_for ''category'' -%> > > <table> > <tr> > <th>Name</th> > etc... > </tr> > <tr> > <td><%= c.text_field :name %></td> > etc... > <td><%= submit_tag ''save'' %></ > td> > </tr> > </table> > <% end %> > > On Sep 8, 6:26 pm,chris<chris.ye...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > i''m having a routing problem with my UPDATE action. I''ve pasted my > > CATEGORIES controller and view below. I ran scaffold for categories > > and i consolidated everything into a single view so that I could show > > list, create new, edit, destroy all from a single page. my create, > > destroy actions work and my index view works as expected. my UPDATE > > action however returns the following: > > > Processing CategoriesController#16 (for 127.0.0.1 at 2008-09-08 > > 13:08:22) [POST] > > Session ID: c05797629988686fd3951de29fca7e24 > > Parameters: {"commit"=>"Update", > > "category"=>{"name"=>"Entertainment", "description"=>"Latest gossip > > about your favourite stars12", "short_name"=>"entertainment"}, > > "authenticity_token"=>"b6a61d7dc0890d83a923366cd5c93e672b16f62e", > > "action"=>"16", "controller"=>"categories"} > > > and it identifies the object id as my action and redirects me to / > > categories/:id. it should redirect me to simply /categories with a > > flash notice saying my object has been updated. i should also note > > that it also doesn''t save the updated entry to my database. > > > I''m running rails 2.1 and I haven''t changed my default routes in > > routes.rb > > > Thanks for your help. > > > HERE''S MY CONTROLLER > > class CategoriesController < ApplicationController > > > layout ''staff'' > > > #verify :method => :post, :only => [ :destroy, :create, :update ], > > #:redirect_to => { :action => :list } > > > # GET /categories > > # GET /categories.xml > > def index > > list > > render :action => ''list'' > > end > > > def list > > @categories = Category.find(:all) > > @category = Category.find(params[:id]) if params[:id] > > @category = Category.new if @category.nil? > > > #respond_to do |format| > > #format.html # index.html.erb > > #format.xml { render :xml => @categories } > > #end > > end > > > # POST /categories > > # POST /categories.xml > > def create > > @category = Category.new(params[:category]) > > > respond_to do |format| > > if @category.save > > flash[:notice] = ''Category was successfully created.'' > > format.html { redirect_to categories_url } > > format.xml { render :xml => @category, :status > > => :created, :location => @category } > > else > > format.html { render categories_url } > > format.xml { render :xml => @category.errors, :status > > => :unprocessable_entity } > > end > > end > > end > > > # PUT /categories/1 > > # PUT /categories/1.xml > > def update > > @category = Category.find(params[:id]) > > > respond_to do |format| > > if @category.update_attributes(params[:category]) > > flash[:notice] = ''Category was successfully updated.'' > > format.html { redirect_to categories_url } > > format.xml { render :xml => @category, :status > > => :created, :location => @category } > > else > > format.html { render categories_url } > > format.xml { render :xml => @category.errors, :status > > => :unprocessable_entity } > > end > > end > > end > > > # DELETE /categories/1 > > # DELETE /categories/1.xml > > def destroy > > @category = Category.find(params[:id]) > > @category.destroy > > > respond_to do |format| > > flash[:notice] = ''Category was successfully removed.'' > > format.html { redirect_to categories_url } > > format.xml { head :ok } > > end > > end > > > end > > > HERE''S MY VIEW --> categories/list.html.erb > > <% @page_title = ''Categories'' -%> > > > <%= content_tag(''p'', link_to(''« Back to Menu'', :controller => > > ''staff'', :action => ''menu'')) %> > > > <table> > > <tr> > > <th>Name</th> > > <th>Short Name</th> > > <th>Description</th> > > </tr> > > > <% for category in @categories -%> > > <tr class=''<%= cycle(''row1'', ''row2'')%>''> > > <td><%= h(category.name) -%></td> > > <td><%= h(category.short_name) -%></td> > > <td><%= h(category.description) -%></td> > > <td><%= link_to(''Edit'', :action => ''list'', :id => category) -%></ > > td> > > <td><%= link_to(''Delete'', {:action => ''destroy'', :id => category}, > > :confirm => ''Are you sure you want to remove this category?'', :method > > => :delete) -%></td> > > </tr> > > <% end %> > > </table> > > > <p><%= link_to(''New Category'', categories_url) %></p> > > > <% form_tag(params[:id].blank? ? {:action => ''create''} : {:action => > > ''update'', :id => @category}) do -%> > > <%= error_messages_for ''category'' -%> > > > <table> > > <tr> > > <th>Name</th> > > <th>Short Name</th> > > <th>Description</th> > > </tr> > > <tr> > > <td><%= text_field(:category, :name, :size => 20) -%></td> > > <td><%= text_field(:category, :short_name, :size => 20) -%></td> > > <td><%= text_field(:category, :description, :size => 40) -%></td> > > <td><%= submit_tag(params[:id].blank? ? ''Create'' : ''Update'') -%></ > > td> > > </tr> > > </table> > > <% end %>--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
chris wrote:> i''m having a routing problem with my UPDATE action. I''ve pasted my > CATEGORIES controller and view below. I ran scaffold for categories > and i consolidated everything into a single view so that I could show > list, create new, edit, destroy all from a single page. my create, > destroy actions work and my index view works as expected. my UPDATE > action however returns the following: > > Processing CategoriesController#16 (for 127.0.0.1 at 2008-09-08 > 13:08:22) [POST] > Session ID: c05797629988686fd3951de29fca7e24 > Parameters: {"commit"=>"Update", > "category"=>{"name"=>"Entertainment", "description"=>"Latest gossip > about your favourite stars12", "short_name"=>"entertainment"}, > "authenticity_token"=>"b6a61d7dc0890d83a923366cd5c93e672b16f62e", > "action"=>"16", "controller"=>"categories"}Chris, update under REST should be a PUT request, and if you compare html resultant from the scaffolding code for new.html.erb and edit.html.erb, you''ll find that edit contains the following after the <form> tag: <input name="_method" type="hidden" value="put" /> I suspect if you add that in somehow programmatically, your problems will be solved. ;) Cheers, Darrik --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey i took your advice and took a closer look at the code scaffold generates for my NEW & EDIT views and it seems that form_for takes the same argument for edit and new which is the object itself, in this case @category. I used the form_for and it worked. can you explain again why it works? that would be really helpful. thanks, On Sep 13, 3:28 am, Darrik Mazey <darrikma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> chris wrote: > > i''m having a routing problem with my UPDATE action. I''ve pasted my > > CATEGORIES controller and view below. I ran scaffold for categories > > and i consolidated everything into a single view so that I could show > > list, create new, edit, destroy all from a single page. my create, > > destroy actions work and my index view works as expected. my UPDATE > > action however returns the following: > > > Processing CategoriesController#16 (for 127.0.0.1 at 2008-09-08 > > 13:08:22) [POST] > > Session ID: c05797629988686fd3951de29fca7e24 > > Parameters: {"commit"=>"Update", > > "category"=>{"name"=>"Entertainment", "description"=>"Latest gossip > > about your favourite stars12", "short_name"=>"entertainment"}, > > "authenticity_token"=>"b6a61d7dc0890d83a923366cd5c93e672b16f62e", > > "action"=>"16", "controller"=>"categories"} > > Chris, > update under REST should be a PUT request, and if you compare html > resultant from the scaffolding code for new.html.erb and edit.html.erb, > you''ll find that edit contains the following after the <form> tag: > > <input name="_method" type="hidden" value="put" /> > > I suspect if you add that in somehow programmatically, your problems > will be solved. ;) > > Cheers, > Darrik--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
chris wrote:> Hey i took your advice and took a closer look at the code scaffold > generates for my NEW & EDIT views and it seems that form_for takes the > same argument for edit and new which is the object itself, in this > case @category. > > I used the form_for and it worked. can you explain again why it > works? that would be really helpful. > > thanks,Perhaps this will illustrate. >> @old_user = User.find :first => #<User id: 1, ...> >> @old_user.new_record? => nil >> @new_user = User.new => #<User id: nil, ...> >> @new_user.new_record? => true form_for() takes the same parameters, but it isn''t passed the same object for a new record as opposed to an edit of a previously existing record, and therefore it doesn''t generate the same html. I haven''t looked two closely, but the big difference I noticed was that form_for() generates the input element with the name "_method" and value "put" when new_record? is nil. The target for the form will be the same for both, and the only difference between them as requests go will be that the update request will use the PUT method whereas the create request will use the POST method. If you look in your controller, you''ll see that prior to the create method is this: # POST /enquiries and prior to the update method is this: # PUT /enquiries/1 The target for the form should be "/enquiries" and in rails this is given by form_for :enquiry. When rails inspects the request to determine the route, it will use the id from the form and the method to come to the conclusion that the target of the update request is PUT /enqueries/:id and the target for the create request is POST /enquiries. Hope that makes sense. Cheers, Darrik --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---