Mah*_*yed 0 ruby routes ruby-on-rails
我试图了解rails如何知道两条路线之间的区别
GET /users/:id
和
GET /users/new
当我们打字
resources :users
我试图跟踪并理解rails源代码中的资源方法,但我完全不了解它.
def resources(*resources, &block)
options = resources.extract_options!.dup
if apply_common_behavior_for(:resources, resources, options, &block)
return self
end
with_scope_level(:resources) do
options = apply_action_options options
resource_scope(Resource.new(resources.pop, api_only?, @scope[:shallow], options)) do
yield if block_given?
concerns(options[:concerns]) if options[:concerns]
collection do
get :index if parent_resource.actions.include?(:index)
post :create if parent_resource.actions.include?(:create)
end
new do
get :new
end if parent_resource.actions.include?(:new)
set_member_mappings_for_resource
end
end
self
end
Run Code Online (Sandbox Code Playgroud)
下面的代码是那样做的吗?
new do
get :new
end if parent_resource.actions.include?(:new)
Run Code Online (Sandbox Code Playgroud)
如果有,你能解释一下吗?
另外,如果我试着写用相同的另一条路线GET users/new
重定向到的格式GET users/:id
,所以我怎么能写这样另一条路线GET users/whatever
,不考虑任何为:ID?
以下是routes.rb的示例
例1:
get '/feedbacks/:id' => 'feedbacks#show'
get '/feedbacks/count' => 'feedbacks#count'
Run Code Online (Sandbox Code Playgroud)
feedbacks/count
重定向到 /feedbacks/:id
例2:
resources :feedbacks
get '/feedbacks/count' => 'feedbacks#count'
Run Code Online (Sandbox Code Playgroud)
feedbacks/count
重定向到 /feedbacks/:id
Rails没有区分,它只是搜索适合条件的第一条记录
这就是为什么new比id更早生成的原因
例1:
get '/feedbacks/count' => 'feedbacks#count'
get '/feedbacks/:id' => 'feedbacks#show'
Run Code Online (Sandbox Code Playgroud)
例2:
resources :feedbacks do
member do
get '/feedbacks/count' => 'feedbacks#count'
end
end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
69 次 |
最近记录: |