Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: How to link/route from one view/page to another view/page with a different controller

I have a view template within the following file in my Rails application:

app/views/static_pages/mission.html.erb

I want to link to that page from a view template in a different folder (corresponding to a different page in the app):

app/views/home/home.html.erb

I don't know what to write in my mission method in the StaticPagesController. I'm also a newbie at Rails. I also don't know how to write the route for it and I suspect I may need to write a get 'something' in my routes.rb file.

Can anybody help with this?

like image 595
Catherine Austria Avatar asked Jan 09 '23 08:01

Catherine Austria


1 Answers

What you are asking can be accomplished in this way:

In your routes.rb:

get "/path/to/your/mission/page", to: "static_pages#mission", as: "mission"

Then in your static_pages_controller.rb:

class StaticPagesController < ApplicationController

  # You can leave the mission method blank, it will render
  # the corresponding static_pages/mission.html.erb by default

  def mission
  end

end

This should set up the static page, so that when you visit: localhost:3000/path/to/your/mission/page, you should be able to see the mission.html.erb page rendered.

To link to the mission page from any other template, you can simply point a link to it, like so:

<%= link_to "Mission Page", mission_path %>

Or, since it's a static page, you can just hardcode the path into the markup:

<a href="/path/to/your/mission/page">Mission Page</a>

Since you're new to Rails (welcome to the world of Rails btw :D), I hope you find these really great resources useful:

  • Official Rails Guides
  • Rails For Zombies
  • Railscasts by Ryan Bates

Hope this was helpful!

like image 71
Zoran Avatar answered Jan 27 '23 06:01

Zoran