pavlo
pavlo

Reputation: 11

ActiveRecord::RecordNotFound in MembersController#show

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...

Upvotes: 1

Views: 2072

Answers (3)

Ricardo Castañeda
Ricardo Castañeda

Reputation: 5812

I had the very same problem when creating a new html.erb.
Even my routes.rb match 'controller/action' => 'controller#action' were correct.
Later I found that the problem was that the resources :controller were above the match.

This is the correct order that worked for me:

match 'controller/action' => 'controller#action'
resources :controller

Thanks to pavlo for asking this question, and to maz because his answer gave me the hint that the resources were involved in the error.

Upvotes: 0

maz
maz

Reputation: 2476

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.

Upvotes: 5

Anubhaw
Anubhaw

Reputation: 6068

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

Upvotes: 0

Related Questions