Dominic Bou-Samra
Dominic Bou-Samra

Reputation: 15416

Basic rails - How link_to works and replacing it with a render

I am super fresh to Rails, but came across a problem I can't wrap my head around.

I have a link using the link_to helper method:

<%= link_to("Link",  {:controller => 'gitrevision_download', :project_id => @project.id, :rev => @rev}) %>

That link then takes me to a new page and invokes the gitrevision_download controllers index method. All I want to do is render that index template within the template I'm already in, rather then as a link.

Edit: Just realised this function isn't working how I thought it did It's displaying the data after the link is called, and the routes handler must be redirecting me to the correct controller.

So what I need is to render the index view from that controller, from another controller. Is that bad practice?

Upvotes: 1

Views: 676

Answers (1)

Veraticus
Veraticus

Reputation: 16064

The best place to go for explanations of Rails functionality is the documentation; it's really surprisingly well done!

To answer your question more directly:

  1. The first parameter, as you've likely deduced, is the name of the link.
  2. The second parameter is passed to Rails' URL generators to create the destination of the link. By passing a hash, you're instructing Rails to generate a URL for a particular controller and action inside that controller. You could also pass a named URL helper (like submissions_path) instead.
  3. The third parameter is HTML options and is added directly to the element -- things like assigning classes and titles, and will show up as <a class="specified-classes" title="specified-title" ...> and so on.

Upvotes: 5

Related Questions