john doe
john doe

Reputation: 35

Rails Resources are giving me headache

I am a .NET developer moving to Ruby on Rails. I have been programming in ASP.NET MVC and now trying to apply the same concepts to Rails. I created index action and now when I say home/index it automatically redirects me the "show" action which does not exists. My routes.rb file has this particular line:

resources :home

home is the home_controller.

What am I doing wrong?

class HomeController < ApplicationController

  def show

  end

  # show all the articles 
  def index

    @articles = Array.new[Article.new,Article.new,Article.new]

    respond_to do |format|
      format.html 
      format.xml { render :xml => @articles }
    end

  end

  def new 
    @article = Article.new

    respond_to do |format|

      format.html 
      format.xml { render :xml => @article }

    end

  end



  def create

    @article = Article.new(params[:post]);

    if @article.save 

      format.html { redirect_to(@post,
              :notice => 'Post was successfully created.') }

    end


  end

  def confirm 
  end

end

Upvotes: 1

Views: 106

Answers (1)

iain
iain

Reputation: 16274

You can run "rake routes" to check out what rails thinks about your routes and which urls will be dispatched to which controllers.

In your case, I get:

home_index GET    /home(.:format)           {:action=>"index", :controller=>"home"}
home_index POST   /home(.:format)           {:action=>"create", :controller=>"home"}
  new_home GET    /home/new(.:format)       {:action=>"new", :controller=>"home"}
 edit_home GET    /home/:id/edit(.:format)  {:action=>"edit", :controller=>"home"}
      home GET    /home/:id(.:format)       {:action=>"show", :controller=>"home"}
      home PUT    /home/:id(.:format)       {:action=>"update", :controller=>"home"}
      home DELETE /home/:id(.:format)       {:action=>"destroy", :controller=>"home"}

So to get to the index action, you need to go to "/home". If you go to "/home/index", it will think "index" is the ID of the resource, thus dispatching to the show action.

However, in Rails, it's custom to use plural names for controllers, and naming them after the resource they represent (this is usually a model, but it doesn't have to be). So in your case the name of the controller should be "ArticlesController" and your routes.rb should contain "resources :articles". Rails is very anal about plural and singular names.

The big advantage of using the plural name of the resource you're accessing, is that you can now use short notations, like "redirect_to @article", "form_for @article do |f|", etc.

So, resources in Rails are supposed to be telling about what you want your actually getting. This helps maintenance too, since other developers have to guess less. If you find yourself needing more than one ArticlesController, consider using namespaces, or try to figure out if one of those controllers are actually another resource (even though they store their data in the same database table).

More information about the routers can be found in the Rails Guide: http://guides.rubyonrails.org/routing.html

Upvotes: 3

Related Questions