Mikerich94
Mikerich94

Reputation: 23

How do I create a new path/route in Ruby and then link to it?

I have done it before but am having trouble adding a new page and a new path to my rails server.

Pretty much, I want to add a new page and then link to that page in a drop down menu... but I am having trouble getting the changes to take effect and for the new path/route to show up when I do "rails routes".

I have done it before for an "offerings" page at pages#offerings but can't seem to figure out how to repeat the same process

I started off going to the pages controller and adding a "def public_speaking" and "end":

Pages Controller

# GET request for / which is our home page
   def home 
      @basic_plan = Plan.find(1)
      @pro_plan = Plan.find(2)
   end 

      def about
      end 

     def offerings
     end 

      def public_speaking
      end
  end

Routes.rb

Then in Routes.rb I tried using the same process (Adding get 'public_speaking', to : 'pages#public_speaking')

  root to: "pages#home"
  devise_for :users, controllers: { registrations: 'users/registrations' }
  resources :users do 
     resource :profile
  end 

  get 'about', to: 'pages#about'
  resources :contacts, only: [:create]
  get 'contact-us', to: 'contacts#new', as: 'new_contact' 
  get 'offerings', to: 'pages#offerings'
  get 'public_speaking', 'pages#public_speaking'
end

View file

I also created a file "public_speaking.html.erb" in the views folder with the same name.

What am I doing wrong/missing to create this new path? Is there some command to execute this linkage or something?

I expected there to be a new route created (since it worked for "offerings"), however it has not worked and I'm not sure why. I will be repeating this process for 5-6 pages, so I want to be sure I can do it right

Upvotes: 0

Views: 1122

Answers (2)

Roman Alekseiev
Roman Alekseiev

Reputation: 1920

Khan Pham has given a correct answer. Seems you are messing with link.

Accordingly to Ruby on rails guide the proper route would be:

get 'public_speaking', to: 'pages#public_speaking'

where to: expects controller#action format.

And then you can check your routes by executing command rake routes and if your part presents there you can use it in your views like: link_to('Public Speaking', public_speaking_path) you can read more about url here. Good luck!

Upvotes: 0

Khanh Pham
Khanh Pham

Reputation: 2973

i see in your routes, it seems your code is not correct.

you should change:

from get 'public_speaking', 'pages#public_speaking'

to get 'public_speaking', to: 'pages#public_speaking'

Upvotes: 2

Related Questions