krs8888 krs8888 - 2 months ago 11
Ruby Question

Ruby/Rails How to create link from one view to another view

I am new to ruby/rails and have been learning by creating a simple application. I have a simple view where I want to add a button which when clicked will take you to another view. I tried adding controller and configure the routes but still not sure how to proceed.

The project structure looks like

app
controller
health
report
view
health
index
reports
index


I created a new controller called reports and in my routes.rb I added

Rails.application.routes.draw do
get 'reports/index'
end


There are two things I want to do. Add a button in health/index file which when click will view reports/index. How to do this ?

Answer

I agree with max about reading up on MVC and doing the Rails Tutorial. But to answer your question...

In your routes.rb:

Rails.application.routes.draw do
  get 'reports#index'
  get 'health#index'
end

Then, in your health/index.html.erb file:

<%= link_to 'Reports Index', reports_path %>

This will create a link that, when clicked on, will go to the Reports Index page.