Home > other >  Create views in resource in Rails
Create views in resource in Rails

Time:11-30

I am a beginner working with Rails: I have this routes.rb:

Rails.application.routes.draw do
  resources :requirements
  root "department#index"
  get "department/about"
end

How can I create a view that has a path like requirements/major?

Thank you so much!

CodePudding user response:

You can extend resources and add custom actions, like this:

 resources :requirements do
   collection do
     get :major
   end
 end

You'll need an action in the RequirementsController that matches, e.g.

class RequirementsController < ApplicationController

  def major
    # set up whatever resource 'major' corresponds to
  end

  ...
end

That's at least one way of doing it. You could also have a controller that directly supports the nested 'major' resource, which would be similar to above - just with a controller: 'name of controller' directive inline..

It'd probably pay to get your head around the "Rails Routing from the Outside In" guide: https://guides.rubyonrails.org/routing.html

  • Related