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.
精彩评论