简体   繁体   中英

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?

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:

Hope this was helpful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM