# HELP: sends my id as my action

**URL:** <https://www.ruby-forum.com/t/help-sends-my-id-as-my-action/148061>\
**Category:** Rails\
**Created:** [September 8, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/help-sends-my-id-as-my-action/148061 "2008-09-08T20:11:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 8, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/help-sends-my-id-as-my-action/148061/1 "2008-09-08T20:11:34Z")

</div>

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’)) %\>

\<% for category in @categories -%\>

'\> \<% end %\>

| Name | Short Name | Description |
| --- | --- | --- |
| \<%= h(category.name) -%\> | \<%= h(category.short\_name) -%\> | \<%= h(category.description) -%\> | \<%= link\_to('Edit', :action =\> 'list', :id =\> category) -%\> | \<%= link\_to('Delete', {:action =\> 'destroy', :id =\> category}, :confirm =\> 'Are you sure you want to remove this category?', :method =\> :delete) -%\> |

\<%= link\_to('New Category', categories\_url) %\>

\<% form\_tag(params[:id].blank? ? {:action =\> ‘create’} : {:action =\>  
‘update’, :id =\> @category}) do -%\>  
\<%= error\_messages\_for ‘category’ -%\>

| Name | Short Name | Description |
| --- | --- | --- |
| \<%= text\_field(:category, :name, :size =\> 20) -%\> | \<%= text\_field(:category, :short\_name, :size =\> 20) -%\> | \<%= text\_field(:category, :description, :size =\> 40) -%\> | \<%= submit\_tag(params[:id].blank? ? 'Create' : 'Update') -%\> |

\<% end %\>

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 10, 2008, 7:52am UTC](https://www.ruby-forum.com/t/help-sends-my-id-as-my-action/148061/2 "2008-09-10T07:52:13Z")

</div>

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\>  
  
  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 13, 2008, 5:05am UTC](https://www.ruby-forum.com/t/help-sends-my-id-as-my-action/148061/3 "2008-09-13T05:05:32Z")

</div>

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:

\<%= submit\_tag(params[:id].blank? ? 'Create' : 'Update') -%\>

again, when i click the ‘update’ button, it redirects me to  
[http://localhost:3000/users/:id](http://localhost:3000/users/:id) and sends the :id as the action.

thanks,

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 13, 2008, 9:29am UTC](https://www.ruby-forum.com/t/help-sends-my-id-as-my-action/148061/4 "2008-09-13T09:29:55Z")

</div>

chris wrote:

> 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 tag:

I suspect if you add that in somehow programmatically, your problems  
will be solved. 😉

Cheers,  
Darrik

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 13, 2008, 6:15pm UTC](https://www.ruby-forum.com/t/help-sends-my-id-as-my-action/148061/5 "2008-09-13T18:15:10Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 14, 2008, 3:36am UTC](https://www.ruby-forum.com/t/help-sends-my-id-as-my-action/148061/6 "2008-09-14T03:36:05Z")

</div>

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
