views:

55

answers:

2

Hi, I am sorry for my bad english first. I just installed ruby and rails few hours ago (you wouldn't believe it took me 3 days to install ruby,rvm,rails and etc, on this ubuntu 10.04 machine) and I am trying to implement basic Member scaffold. My version of rails is 3.0.0 and my ruby is 1.9.2.

When I #rails generate scaffold Member email:string password:string it created various files. I also did #rake db:migrate to implement database in mysql. So within member controller, I saw that I have to go through 127.0.0.1:3000/members/ to get to the basic scaffold setup.

I just changed

def new
  @member = Member.new

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @member }
  end
end

above statements in member controller into

def register
  @member = Member.new

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @member }
  end
end

U see, I just changed the new into register, and now, when I try to get into 127.0.0.1:3000/members/register The ActiveRecord::RecordNotFound error shows up. How can I resolve this problem? I just want to make 127.0.0.1:3000/members/register to be a page where user can register..

btw, this RoR seems to be very complicated, and api documents seems to be too broad to be understood for beginners. I ordered a RoR book last week, so I will see how it goes...

A: 

Hi,

You need to add 'register' method to routes, like:-

map.connect '/members/register', :controller => 'members', :action => 'register'.

After adding the above to routes.rb restart the server.

Thanks, Anubhaw

Anubhaw
You mean just put -> map.connect '/members/register', :controller => 'members', :action => 'register' <- in a route.rb file? I did so, but I am getting a undefined local variable or method `map' for #<ActionDispatch::Routing::Mapper:0x000000030c64a0> (NameError)
pavlo
You need to put the above line in like ActionController::Routing::Routes.draw do |map| map.connect'/members/register', :controller => 'members', :action => 'register' end
Anubhaw
I'm not sure that's true for rails 3. The routing has changed quite considerably iirc.
Shadwell
To do that in Rails3 you have to do the following: match 'members/register' => 'members#register'
maz
ok I am not up to date with rails3 routes.......... Sorry for that...
Anubhaw
+4  A: 

By using the scaffold generator members gets mapped as a resource. Look in the config/routes.rb

resources :members

When entities are mapped as resources they get a set of default routes. You can see all your mapped routes by doing rake routes

members GET     /members(.:format)          {:action=>"index", :controller=>" members"}
members POST    /members(.:format)          {:action=>"create", :controller=> "members"}
new_member GET  /members/new(.:format)      {:action=>"new", :controller=>"members"}
edit_member GET /members/:id/edit(.:format) {:action=>"edit", :controller=>"members"}
member GET      /members/:id(.:format)      {:action=>"show", :controller=>"members"}
member PUT      /members/:id(.:format)      {:action=>"update", :controller=>"members"}
member DELETE   /members/:id(.:format)      {:action=>"destroy", :controller=>"members"}

When you rename the new action to register there no longer is a valid route for that mapping.

What you could do is to leave the action as new and just add the following route in your routes.rb

match 'members/register' => 'members#new'

This way you do not break other things in the scaffold. If you really want to rename the action to register I would suggest not using scaffolds.

maz
Hi, thanks for nice answer. Now I get how rails 3 get done in mapping. :)
pavlo
Hi again. If I want to set something like
pavlo
edit_password_member GET /members/:id/edit_password(.:format) {:action=>"edit_password", :controller=>"members"}Where can I do so? in a 'routes' file??
pavlo
Yes! in routes.rb. If you create a new question I will be happy to provide you with a more detailed answer.
maz