1
votes

I have 2 questions:

I have a controller called homepage. I have a view called samplegraph in my homepage's view directory. I want to get the routing working correctly such that www.homepage.com/samplegraph takes me to the samplegraph page.

As far as I can tell, the route for it in routes.rb should be something like this:

GET 'homepage/samplegraph' => 'homepage#showgraph1'

If I'm understanding rails routing correctly, this statement routes GET requests to homepage/samplegraph to the homepage controller's showgraph1 action. At this point I'm not particularly sure what the showgraph1 action should be in order to render the view page(samplegraph). At the moment the action is simply empty. I don't really know what to put here.

Second question:

Also, while I was researching rails routing, I was looking into resource based routing. For my purposes, I don't need most of the stuff generated by that. One thing I am interested in is that invoking resource based routing automatically generates Paths for you via helpers(I think?).

How would I generate a Path for my route, such that I'd be able to use a link_to method to link various parts of the application together? Any help/comments would be greatly appreciated.

2

2 Answers

1
votes

Firstly, if you want to get 'samplegraph' page rendered by hitting 'www.homepage.com/samplegraph', you will need to update your route.

Replace

get 'homepage/samplegraph' => 'homepage#showgraph1'

with

get '/samplegraph' => 'homepage#showgraph1'

Now in showgraph1 action of your homepage controller, you will need to render samplegraph view page at last line of the action.

render 'samplegraph'

As of you second question, just hit rake routes on your terminal from your app directory. It will show all routes with helpers which you can use with link_to. You will need to append _path to those routes while using with link_to

0
votes

Like @RAJ said first of all you need to change your route to

get '/samplegraph' => 'homepage#showgraph1'

At this point I'm not particularly sure what the showgraph1 action should be in order to render the view page(samplegraph)

Rails doesn't care if your action is empty or not, it'll still render your actions view even if it's empty. Since your action is named showgraph1 so it'll make rails look for showgraph1.html.erb with path views/homepage/showgraph1.html.erb

To change this behavior you need to use render 'samplegraph' in your action

One thing I am interested in is that invoking resource based routing automatically generates Paths for you via helpers(I think?)

Rails generate path and url helpers for each route and it doesn't depend on how your routes are defined but you can customize your helper methods by specifying as: option

get 'homepage/samplegraph' => 'homepage#showgraph1', as: 'showgraph'

This will make your helper methods showgraph_path and showgraph_url