开发者

Rails3 routing: how to direct two requests to a single entry point

开发者 https://www.devze.com 2023-02-05 22:00 出处:网络
In my controller I would like to use the same code for the new and edit requests. Like this: def edit @tag= Tag.fin开发者_开发知识库d(params[:id]) || Tag.new

In my controller I would like to use the same code for the new and edit requests. Like this:

def edit
  @tag= Tag.fin开发者_开发知识库d(params[:id]) || Tag.new
end

My question is: how do I indicate this in routes.rb (Rails3)?


Assume you are using resources routes just like this one:

# routes.rb
resources :tags

This will create new and create for you.

Suppose you only want new maps to edit, but remaining create unchanged, use the following:

get "/tags/new" => "tags#edit", :as => :new_tag_path
resources :tags

The order is important. The upper one will be matched first. And so if the path is /tags/new, it will be routed to edit action. And because it is matched already, it won't go down and so although resources :tags defines also the /tags/new to new action, no routing will be successfully matched.

So adding the only one line is ok.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号