user2498890
user2498890

Reputation: 1566

Link path to a specific page - Ruby on Rails

I'm a newbie and I'm having a bit of trouble understanding link paths on Ruby on Rails. Say I have say a page in app/views/jobs/info.html.erb and I want to make a link to that page from my index.html.erb which is in the same folder, what would the link path be?

I have another link path inside my index.html.erb to new.html.erb which is as follows:

<%= link_to "Add a Position", new_job_path %>

but I if I wanted to do a link path to info.html.erb I don't understand what I put instead of 'new_job_path'?

If anyone could help me I'd really appreciate it.

Upvotes: 0

Views: 6997

Answers (2)

scottb
scottb

Reputation: 1135

You can't actually "do a link path to info.html.erb".

Links refer to abstract things called "resources". In Rails, a resource is, ultimately, a specific action on a given controller. The routes specified in config/routes.rb translate URL paths into actions on controllers.

If you run rake routes, you'll see output that looks something like this:

  Prefix Verb   URI Pattern              Controller#Action
    jobs GET    /jobs(.:format)          jobs#index
         POST   /jobs(.:format)          jobs#create
 new_job GET    /jobs/new(.:format)      jobs#new
edit_job GET    /jobs/:id/edit(.:format) jobs#edit
     job GET    /jobs/:id(.:format)      jobs#show
         PATCH  /jobs/:id(.:format)      jobs#update
         PUT    /jobs/:id(.:format)      jobs#update
         DELETE /jobs/:id(.:format)      jobs#destroy

This lists all of the routes defined for your application. The right-most column is the action that gets run. The left-most column is what you usually use in link_to calls in views.

So, to answer your question, your info.html.erb file is a view available to the "posts" controller.

If the view doesn't need any information from the database or other "work" done, then by default, invoking the action "info" on the controller will just render that view -- you don't need to modify the controller. (It will do this for any action, not just "info". If the view isn't defined, it generates a 404 error.)

So, you need to add a route that maps to your action. If you look at config/routes.rb, you should see a line that says resources :jobs. If you change that to:

resources :jobs do
  collection do
    get :info
  end
end

You can re-run the rake routes command and you'll see a new line added to the output:

   Prefix Verb   URI Pattern              Controller#Action
info_jobs GET    /jobs/info(.:format)     posts#info

That's a route pointing to the right action, and you can link to it in a view by writing <%= link_to 'Info', info_jobs_path %>. I got that value by taking the "prefix" column and adding _path, which tells it to generate a relative URI for that resource. If I had needed an absolute URI (typically needed in a mailer), then I'd have used info_jobs_url.

There are other ways you can specify the route, depending on other information you might need -- look through the comments in the Rails-generated config/routes.rb for details.

Upvotes: 3

usha
usha

Reputation: 29369

One way would be to add an action in your controller and add a custom route in your config/routes.rb

In JobsController

def info
  #initialize instance variables that you would use in jobs/info.html.erb
end

config/routes.rb

get 'jobs/info', :to => "jobs/info", :as => job_info

Now in your index.html.erb

<%= link_to "Job Info", job_info_path%>

Upvotes: 2

Related Questions