New to Ruby and Rails and confused with Routes..

Hi,

I am new with RoR. I have this HTML element <th><a href="/movies" id="title_header">Movie Title</a></th>

What i am planning to acheive with this is that when the user clicks the movie title the list sorts itself. My problem here is with the routes. I already have the Rails provided "resourceful routes" but i have no idea what should i do for the above case. I am doing this for a homework and in the hint section of the homework it says

"You may find it helpful to know that if you pass Rails provided "resourceful routes" a hash of additional parameters, those parameters will be parsed by Rails and become available in the params hash."

I have no idea how to code this in routes.rb and how it would be interepted at controllers.rb

Hi,

here you could see what ‘resource :something’ will do in config/routes.rb

for example it creates the route:

GET /photos/:id show

the “:id” part will populate the params hash in the controller with an id

so in the controller, in the show action you could have:

@photo = Photo.find(params[:id])

try to check which routes your config/routes.rb generates calling rake routes in the console

look here Rails Routing from the Outside In — Ruby on Rails Guides

I hope this could help you!

bye

Muhammad Salman wrote in post #1080505:

I am new with RoR. I have this HTML element <th><a href="/movies" id="title_header">Movie Title</a></th>

First off, don't statically define this link in HTML. Instead use the Rails link_to helper:

<th><%= link_to @movies -></th>

What i am planning to acheive with this is that when the user clicks the movie title the list sorts itself. My problem here is with the routes. I already have the Rails provided "resourceful routes" but i have no idea what should i do for the above case. I am doing this for a homework and in the hint section of the homework it says

"You may find it helpful to know that if you pass Rails provided "resourceful routes" a hash of additional parameters, those parameters will be parsed by Rails and become available in the params hash."

I have no idea how to code this in routes.rb and how it would be interepted at controllers.rb

What you're trying to describing here is not a routing issue. The route doesn't change since you'll be calling the same "index" action. The responsibility of routes.rb is for mapping URLs to controllers and action methods defined by those controllers.

For example:

If your MoviesController looks something like:

class MeetingsController < ApplicationController   def index     @movies = Movies.all     ....   end end

Then you might do something like:

class MeetingsController < ApplicationController   def index     @movies = Movies.order('title ASC')     ....   end end

And if you want to make the ordering column dynamic you might do:

class MeetingsController < ApplicationController   def index     @movies = Movies.order("#{params[column]} ASC")     ....   end end

Of course you may want to validate the column name passed into the params hash. And, I'm not saying this is the best way to accomplish your need. This is all off the top of my head.

Now take a closer look at the link_to helper and figure out how to pass the desired sorting column in the request.

Hint: http://example.com/movies?sort=title