Search code examples
ruby-on-railsrubyruby-on-rails-3haml

How do I use HAML in a dynamic link?


I am trying to create a link using HAML which looks like the this

=link_to("Last updated on<%=@last_data.date_from.month %>",'/member/abc/def?month={Time.now.month}&range=xyz&year={Time.now.year}')

It is not taking the Ruby code and it is displaying that as a string

Last updated on<%=@last_data.date_from.month %>

and in the URL as well it is not taking the function Time.now.month or Time.now.year .

How do I pass Ruby code in URL and in the string ?


Solution

  • You should probably use something like this:

    = link_to("Last updated on #{@last_data.date_from.month}", "/member/abc/def?month=#{Time.now.month}&range=xyz&year=#{Time.now.year}")
    

    Note that in the second string, it's necessary to change the ' to ". Also if the link text is getting long, you can use something like this:

    = link_to("/member/abc/def?month=#{Time.now.month}&range=xyz&year=#{Time.now.year}") do
      Last updated on #{@last_data.date_from.month}