Forum: Ruby on Rails Passing parameters with link_to

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Lieven D. (Guest)
on 2006-01-03 05:53
I have following code in my controller:

class AlbumsController < ApplicationController
  def list_by_year(year)
    @album_pages
    @albums = paginate :albums,
                       { :per_page => 10,
                         :conditions => ["year = ?", year] }
    render :action => 'list'
  end
end

Now I want to make a link to the list_by_year action in show.rhtml

<%= link_to @album.year,
            { :action => 'list_by_year' },
            {},
            @album.year %>

Isn't this what the *parameters_for_method_reference is for? Obviously
not because I get:

wrong number of arguments (0 for 1)

What is the correct way to pass this parameter? Haven't found anything
by googling.
Kevin O. (Guest)
on 2006-01-03 06:03
Lieven De Keyzer wrote:
> I have following code in my controller:
>
> class AlbumsController < ApplicationController
>   def list_by_year(year)
>     @album_pages
>     @albums = paginate :albums,
>                        { :per_page => 10,
>                          :conditions => ["year = ?", year] }
>     render :action => 'list'
>   end
> end
>
> Now I want to make a link to the list_by_year action in show.rhtml
>
> <%= link_to @album.year,
>             { :action => 'list_by_year' },
>             {},
>             @album.year %>
>
> Isn't this what the *parameters_for_method_reference is for? Obviously
> not because I get:
>
> wrong number of arguments (0 for 1)
>
> What is the correct way to pass this parameter? Haven't found anything
> by googling.

I don't think you can pass parameters this way.

You can submit it through the params hash like this

class AlbumsController < ApplicationController
 def list_by_year
     year = params[:year]
     @album_pages,
     @albums = paginate :albums,
                        { :per_page => 10,
                          :conditions => ["year = ?", year] }
     render :action => 'list'
   end
 end

<%= link_to @album.year,
             { :action => 'list_by_year' },{:year => @album.year} %>
This topic is locked and can not be replied to.