Matching and Routes in Rails

前端 未结 3 2090
南笙
南笙 2021-02-07 18:08

I generated a controller and changed the routes but opening the links yields errors on my local server.

Generating controller and routes



        
3条回答
  •  天命终不由人
    2021-02-07 18:19

    First, you must specify the HTTP method by adding via: :get at the end of match 'st' => 'controller#action

    And it's better to use get '/home', to: 'static_pages#home'

    But, there is a problem, that your code doesn't follow RESTful, that only support 7 actions: index, new, edit, create, update, show and destroy.

    These are 2 solutions:

    SOL 1: Put them in different controller (homes, abouts..) and all of these controllers have action index.

    SOL 2: If it's too much work, we can match them to show action. We use static_pages controller, and each page (home, about) will be a item.

    The routes will look likes /static_pages/home /static_pages/about

    I know it isn't good because of the prefix static_pages.

    We can easily get rid of this by adding a custom routes at the end of routes file:

    get '/:id', to: 'static_pages#show'

    That's it. And if you think it's too much work (I think so too), check out this gem High Voltage. Have fun.

提交回复
热议问题