Home>

Currently, I am also creating a web service that I wanted to create.
Until now, when making an application with rails in the tutorial, it was almost created with a single page (one html.erb file). However, most web services (web pages) do not consist of a single page, but rather consist of multiple pages.

When creating a web page in a local environment
Enter the routing in routes.rb, define a function with the same name as the action name described here in the controller file, create an html.erb file with the same name, and display the page.
Now, for example, if you simply want to create two pages, create two files, index.html.erb file and index2.html.erb file, and define two controller names like this and access localhost3000 The page name can be changed by changing the controller/path path name.

class ServiceController<ApplicationController
  def index
    end
  def index2
  end
end
Rails.application.routes.draw do
get 'service/index' =>'service # index'
get 'service/index2' =>'sevice # index2'
end


However, since there are still two pages, there are aspects that can be handled like this, but when this number increases to 10, 30, 50, it is very inefficient to define the path name and controller each time I thought so.
I tried various things on my own, but when I wanted to simply increase the page, I thought that using scaffold was different, and I was in trouble because I couldn't find the answer.
When developing an application with rails, how to write multiple pages?
Or do we have to do feats like this?
Thanks for your understanding.

  • Answer # 1

    Ruby on Rails Tutorial Learn Rails with examples
    We recommend that you read the URL and try your hand at making your site.

    I think you can learn the basics of rails.

  • Answer # 2

    Ruby on Rails has a policy of "If you follow the convention, you don't have to write one by one."
    As long as you follow the proper naming convention,automaticallywill do the right thing.

    Rails will automatically route when the controller follows the naming convention, even if nothing is listed.
    From/{controller}/{method}, the method method of the Controller class is called (and the corresponding view erb is automatically output).

    Try to remove the description about index and index2 from routes.rb. Still (if proper naming) it should work as intended.