Nachshon Schwartz
Nachshon Schwartz

Reputation: 15795

Rails routing not giving wanted result

I have the following route:

#routes.rb    
get "(/questions_groups/:group_id)/questions/new" => "questions#new", as: "new_question"
resources :questions

Id like calling the new_question_path(@question_group) where @question_group.id = 1 to return the path:

/questions_group/1/questions/new

Yet it returns:

/questions/new?group_id=1

When I remove the resources :questions I get the correct path but lose all my routes, how can I solve this problem?

Upvotes: 0

Views: 40

Answers (2)

Mike Campbell
Mike Campbell

Reputation: 7978

Just call it something different. When you define the resource it comes loaded with a whole bunch of url helpers, in your case one of them is new_question, which is the same name as your custom route. If you're trying to replace the route for the new question then tell the resource not to define its own with:

resource :questions, except: :new

Upvotes: 1

usha
usha

Reputation: 29369

Try this

match "/questions_groups/:group_id/questions/new" => "questions#new", as: "new_question"

Upvotes: 0

Related Questions