0

I have routes defined like

scope ':cityname' do
  resources comments
end

Note cityname = comment.user.cityname so url helpers such as comment_path(@comment) can generate links like

/newyork/comments/1
/boston/comments/2
/miami/comments/3

How to set this :cityname url option based on the model attribute?

I found a related question here : default_url_options and rails 3

Thanks!

Community
  • 1
  • 1
QWJ QWJ
  • 317
  • 1
  • 5
  • 14

1 Answers1

1

You can try something like this

class ApplicationController < ActionController::Base

  def url_options
    { :cityname => @comment.user.cityname }.merge(super)
  end

end

class YourController < ...

def calledaction
  @comment = Comment.find(1)
end
end
techvineet
  • 5,041
  • 2
  • 30
  • 28
  • thanks but how'd it work on an index page with 100 comment links with all different citynames? i.e., is there a way to bypass ApplicationController at all? – QWJ QWJ Sep 12 '13 at 07:18
  • It has to be overridden in controllers, so you can put this function inside your specific controllers, in case you dont want it everywhere. – techvineet Sep 12 '13 at 07:20
  • still, how'd this work on the index page with 100 comment links, with all **DIFFERENT** citynames? Here you don't have a single @comment, but an array of comments, and how would a controller help here? – QWJ QWJ Sep 12 '13 at 07:40
  • In that case you can try to pass on the cityname like this some_url(:cityname => 'cityname') – techvineet Sep 12 '13 at 08:39