简体   繁体   中英

ruby on rails url_for breaks when placing more than one variable into a route

For a rails 3 project, I am trying to create seo urls like

/recipes/123/lasagna

My recipe model has a name property, and in the routes.rb I have specified the following:

resources :recipes, :path => "recipes", :except => [:show]
get :path => "recipes/:id/:name", :controller => :recipes, :action => :show, :as => :recipe

the controllers work as desired, but

url_for

and

recipe_path

break - it doesn't seem to know what to use for :name

I would like it to work the default way - using

recipe_path(recipe)

a workaround would be to use

recipe_path(:id => recipe.id, :name => recipe.name)

that is not really pretty - and it would mean I have to modify all of my views

  1. Is adding :name the correct approach to add the name to an url?
  2. How can I get recipe_path to work properly?

Because you have added recipes/:id/:name to your routes file you can leave out the :id=> and :name=> . You can just do this: recipe_path(recipe.id, recipe.name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM