导轨,向新路线添加参数 [英] rails, adding a parameter to new route

查看:76
本文介绍了导轨,向新路线添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

new 操作通常不需要参数,因为它从头开始创建新资源。
在我的应用程序中,每当我创建某种类型的资源时,说一本书 ,我就需要提供一个模板,即另一个的I​​D书。因此,我的 new 路由始终具有一个参数。
我不知道如何将此事实表示为 routes.rb 文件。

The new action normally doesn't require parameters, since it creates a new resource from scratch. In my application whenever i create a certain type of resource say a book i need to provide a template, that is the id of another book. So my new route always has a parameter. I don't know how to represent this fact into routes.rb file.

由于我什至不知道它是否可行,因此我将创建一个new_wp,参数操作。

我试图将其添加到我的

Since i don't even know whether it is feasible, just in the case it isn't, then i will create a new_wp, a "new with parameter" action.
I tried to add it to my

resources :books, :only => [:edit, :update, :show, :new] do

  member do
    get 'new_wp/:template_id', :action => 'new_wp'
  end

end

但耙路线说那不是我想要的:

but rake routes say that it isn't quite what i want:

GET        /books/:id/new_wp/:template_id(.:format)   books#new_wp

也就是说,它有两个参数。

that is, it has two params.

推荐答案

尝试:

resource ...
  get "new/:template_id", :to => "Books#new_wp", :on => :collection
end

#  GET  /books/new/:template_id(.:format)  Books#new_wp 

这篇关于导轨,向新路线添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆