[英]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 对于Rails 3项目,我正在尝试创建seo url,例如
/recipes/123/lasagna
My recipe model has a name property, and in the routes.rb I have specified the following: 我的食谱模型具有name属性,并且在route.rb中,我指定了以下内容:
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 休息-它似乎不知道用什么来: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 那不是很漂亮-这意味着我必须修改所有视图
Because you have added recipes/:id/:name to your routes file you can leave out the :id=> and :name=> . 因为您已经在路径文件中添加了配方/:id /:name ,所以可以省略:id =>和:name => 。 You can just do this: recipe_path(recipe.id, recipe.name)
您可以执行以下操作:recipe_path(recipe.id,recipe.name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.