Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters in URL in Rails

I am starting to learn Rails and I am facing an issue which I suppose is very simple but I have not been able to find an answer.

I have a catalog of movies and the index page displays it showing several columns and I need to make two of them a link. This link will re-display the index page again but sorted by the selected criteria.

So far I have only added one link as follows:

%th= link_to 'Movie Title', root_path

But I am not able to send a URL parameter so I can get it in the controller and perform the sort. The actual controller is as follows:

def index
  @movies = Movie.all
end

I guess that, if I can get a parameter in the controller then using an "if" statement would do the job. I imagine an URL like this, where "1" means to sort by the first criteria:

http://www.mysite/1

I saw a solution like this but I guess it is not a good choice. Please advice.

<%= link_to "Link Title", something__path(:param1 => "value1", :param2 => "value2") %>

I will very much appreciate any suggestions on how to modify the view and the controller. I have googled several solutions but I am having a hard time understanding the solutions. Maybe there is a better approach to achieve this. If so, then I am ready to listen.

Respectfully,
Jorge Maldonado

like image 380
user3059248 Avatar asked Feb 11 '17 19:02

user3059248


1 Answers

Main question

This comes down to RESTful path design. It really depends on the kind of field that you're trying to pass to the controller and represent in the URL.

It's important to think of paths as access routes to resources.

Generally speaking, there are two ways to represent the data that you want:

  • As path variables: The form of these is /path/to/resource/:resourceId. Use this format to represent properties of the resource itself.
    • e.g. The ID of a post on a blog is a property of the blog resource.
  • As query parameters: The form of these is /path/to/resource?queryKey=queryVal. Use this format for metadata and operations on data.
    • e.g. The way that you sort blog posts is not a property of the blog posts themselves, it's just an operation you're performing on them.

Mechanics

Your initial guess is actually pretty close. What you'll want is something like this instead:

In your controller:

movie = Movie.find(params[:id])

In your view:

<%= link_to 'View Movie', movie %>

Under the hood, Rails is performing some magic that turns this into something that looks fairly similar to your code in the question. It's best to let Rails perform this magic itself though, as it makes changing things later easier.

Also, just to be clear, using path variables (instead of query params) is the best approach for your situation. You can read more on routing in the docs here: http://guides.rubyonrails.org/routing.html

Follow-up question

Answering this (I don't have enough rep to comment yet):

I appreciate your response. It works fine. I still have one question. What does it mean a link like this? What is the last parameter (:id) used for? link_to "Movie Title", movies_path(:sort => "title"), :id => "title_header"

On link_to tags, :id adds an id field to the generated HTML tag.

This: link_to "Articles", articles_path, id: "news", class: "article"

Generates: <a href="/articles" class="article" id="news">Articles</a>

See the Rails docs here: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
(Search for "Classes and ids for CSS are easy to produce:")

like image 146
drs Avatar answered Sep 28 '22 02:09

drs