fphilipe
fphilipe

Reputation: 10054

Rails complex routing, easier named routes helpers

I have a named route like the following:

map.with_options :path_prefix => '/:username', :controller => 'questions' do |q|
    q.question '/:id', :action => 'show', :conditions => { :method => :get }
end

Now to generate a URL to a specific question I have to write

question_path( :id => @question.id, :username => @question.user.username )

Which is pretty cumbersome. I would like to be able to write

question_path(@question)
# or even
link_to 'Question', @question

and get the desired result.

How is this possible? I assume I have to overwrite the default helper to achieve this.

Upvotes: 0

Views: 935

Answers (2)

Tatjana N.
Tatjana N.

Reputation: 6225

You can use question_path(@question.user.username, @question)

Or you can write helper method:

  def user_question_path(question)
    question_path(question.user.username, question)
  end

And use user_question_path(@question)

Upvotes: 1

robertokl
robertokl

Reputation: 1909

It seams like you want a nested route for users/question.

map.resource :users do |user|
  map.resources :question, :shallow => true
end

This way, you have access to the user questions with /users/1/questions, but you can still access /questions/1 to a specific question.

Upvotes: 1

Related Questions