# Strange (for me) Scaffold source code generated

**URL:** <https://www.ruby-forum.com/t/strange-for-me-scaffold-source-code-generated/154097>\
**Category:** Rails\
**Created:** [November 25, 2008, 10:53am UTC](https://www.ruby-forum.com/t/strange-for-me-scaffold-source-code-generated/154097 "2008-11-25T10:53:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jujuju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jujuju](https://www.ruby-forum.com/u/jujuju)\
**Post date:** [November 25, 2008, 10:53am UTC](https://www.ruby-forum.com/t/strange-for-me-scaffold-source-code-generated/154097/1 "2008-11-25T10:53:19Z")

</div>

Hello 🙂

I generated model, view and controler for products database table  
using scaffold

I try to change controler name from product to admin but I can’t ☹ (I  
didn’t know how to specifi diferent controler name when use scaffold)

I controllers/s\_controller.rb to controllers/admin\_controller.rb  
view/products directory to view admin directory  
layout/products.html.erb to layout/admin.html.erb  
and make some modifications

but i can’t do it because there are some strange thinks

view\products\index.html  
\<%= link\_to ‘Show’, product %\>  
\<%= link\_to ‘Edit’, edit\_product\_path(product) %\>  
\<%= link\_to ‘Destroy’, product, :confirm =\> ‘Are you  
sure?’, :method =\> :delete %\>

what are these edit\_product\_path functions? And why edit link is  
something like  
[Edit](https://www.ruby-forum.com/products/1/edit)  
and not  
[Edit](https://www.ruby-forum.com/products/edit/1)

when i try to make /admin/1/edit request I get an error that I have  
not action 1, it works only with /admin/edit/1

in products controler it works both ways:  
/products/1/edit  
and  
/products/edit/1/

Can you help me, pls 🙂

---

<div class="post-metadata">

**Author:** ![jujuju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jujuju](https://www.ruby-forum.com/u/jujuju)\
**Post date:** [November 25, 2008, 11:11am UTC](https://www.ruby-forum.com/t/strange-for-me-scaffold-source-code-generated/154097/2 "2008-11-25T11:11:09Z")

</div>

If I have to short my question it is:

view/products/index.html.erb

# Listing products

\<% for product in @products %\>

\<% end %\>

| Title | Description | Image url |
| --- | --- | --- |
| \<%=h product.title %\> | \<%=h product.description %\> | \<%=h product.image\_url %\> | \<%= link\_to 'Show', product %\> | \<%= link\_to 'Edit', edit\_product\_path(product) %\> | \<%= link\_to 'Destroy', product, :confirm =\> 'Are you sure?', :method =\> :delete %\> |

  

\<%= link\_to ‘New product’, new\_product\_path %\>

what is this function edit\_product\_path and why the result is  
/1/edit and not /edit/1

And why both work?

???

---

<div class="post-metadata">

**Author:** ![jujuju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jujuju](https://www.ruby-forum.com/u/jujuju)\
**Post date:** [November 25, 2008, 11:30am UTC](https://www.ruby-forum.com/t/strange-for-me-scaffold-source-code-generated/154097/3 "2008-11-25T11:30:16Z")

</div>

On 25 Nov 2008, at 10:11, Ju Lian wrote:

> \<%= link\_to ‘New product’, new\_product\_path %\>
> 
> what is this function edit\_product\_path and why the result is  
> /1/edit and not /edit/1

Short answer: restful routing. When you declare a route for products  
in routes.rb this add a bunch of named routes, so new\_product\_path is  
the url which gives you the path where the new product form is,  
edit\_product\_path(some\_product) is the url for editing some\_product  
etc…

> And why both work?

because you still have the old controller/action/id routes in your  
routes.rb file

---

<div class="post-metadata">

**Author:** ![jujuju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jujuju](https://www.ruby-forum.com/u/jujuju)\
**Post date:** [November 25, 2008, 11:47am UTC](https://www.ruby-forum.com/t/strange-for-me-scaffold-source-code-generated/154097/4 "2008-11-25T11:47:52Z")

</div>

Frederick C. wrote:

> On 25 Nov 2008, at 10:11, Ju Lian wrote:
> 
> > \<%= link\_to ‘New product’, new\_product\_path %\>
> > 
> > what is this function edit\_product\_path and why the result is  
> > /1/edit and not /edit/1
> 
> Short answer: restful routing. When you declare a route for products  
> in routes.rb this add a bunch of named routes, so new\_product\_path is  
> the url which gives you the path where the new product form is,  
> edit\_product\_path(some\_product) is the url for editing some\_product  
> etc…
> 
> > And why both work?
> 
> because you still have the old controller/action/id routes in your  
> routes.rb file

Thank you very much 🙂

Now I understand that I have to learn this:  
[http://guides.rubyonrails.org/routing\_outside\_in.html](http://guides.rubyonrails.org/routing_outside_in.html)

---

<div class="post-metadata">

**Author:** ![jujuju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jujuju](https://www.ruby-forum.com/u/jujuju)\
**Post date:** [November 25, 2008, 2:41pm UTC](https://www.ruby-forum.com/t/strange-for-me-scaffold-source-code-generated/154097/5 "2008-11-25T14:41:10Z")

</div>

May be this is the right way (it works)

# ============= === routes.rb

map.resources :products, :as =\> ‘admin’

---

<div class="post-metadata">

**Author:** ![jujuju](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jujuju](https://www.ruby-forum.com/u/jujuju)\
**Post date:** [November 25, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/strange-for-me-scaffold-source-code-generated/154097/6 "2008-11-25T14:27:08Z")

</div>

Ju Lian wrote:

> Frederick C. wrote:
> 
> > On 25 Nov 2008, at 10:11, Ju Lian wrote:
> > 
> > > \<%= link\_to ‘New product’, new\_product\_path %\>
> > > 
> > > what is this function edit\_product\_path and why the result is  
> > > /1/edit and not /edit/1
> > 
> > Short answer: restful routing. When you declare a route for products  
> > in routes.rb this add a bunch of named routes, so new\_product\_path is  
> > the url which gives you the path where the new product form is,  
> > edit\_product\_path(some\_product) is the url for editing some\_product  
> > etc…
> > 
> > > And why both work?
> > 
> > because you still have the old controller/action/id routes in your  
> > routes.rb file
> 
> Thank you very much 🙂
> 
> Now I understand that I have to learn this:  
> [http://guides.rubyonrails.org/routing\_outside\_in.html](http://guides.rubyonrails.org/routing_outside_in.html)

I still can’t change controller name from products to admin

# ============= === routes.rb

ActionController::Routing::Routes.draw do |map|  
map.resources :products  
map.resources :admin

# map.connect ‘:controller/:action/:id’

# map.connect ‘:controller/:action/:id.:format’

end

May be controler name must be ‘admins’ ?

Some of helpers functions doesn’t work for me ☹

# ================================= === /view/products/index.hmtl.erb

```
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', 

```

:method =\> :delete %\>

\<%= link\_to ‘New product’, new\_product\_path %\>

# become:

# === /view/admin/index.hmtl.erb

```
<td><%= link_to 'Show', admin_path(:id => product.id) %></td>
<td><%= link_to 'Edit', edit_admin_path(:id => product.id) %></td>
<td><%= link_to 'Destroy', admin_path(:id => product.id), :confirm 

```

=\> ‘Are you sure?’, :method =\> :delete %\>

\<%= link\_to ‘New product’, new\_admin\_path %\>

# ================================= === /view/products/new.hmtl.erb

# New product

\<% form\_for(@product) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :title %\>  
 \<%= f.text\_field :title %\>

\<%= f.label :description %\>  
 \<%= f.text\_area :description %\>

\<%= f.label :image\_url %\>  
 \<%= f.text\_field :image\_url %\>

\<%= f.submit "Create" %\>

\<% end %\>

\<%= link\_to ‘Back’, products\_path %\>

# ================================= === /view/admin/new.hmtl.erb

# New product

\<% form\_for(@product) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :title %\>  
 \<%= f.text\_field :title %\>

\<%= f.label :description %\>  
 \<%= f.text\_area :description %\>

\<%= f.label :image\_url %\>  
 \<%= f.text\_field :image\_url %\>

\<%= f.submit "Create" %\>

\<% end %\>

\<%= link\_to ‘Back’, admin\_path %\>

1-st The form submits to product controller instead of new admin  
controller  
2-nd admin\_path or admins\_path in the last row give me an error

🙂 What I am doing wrong?

Is there some reason not to change controller name from products to  
admin?
