ruby on rails - How do I specify that a named route should use a model's columns its _url and _path functions? -
i have posts model id , title columns.
i've got route (rails 2.3.8) set follows:
map.post ':title/:id', :controller => 'posts', :action => 'show'
which works correctly when recognising urls , when generating them explicitly, in
post_url(:title => 'foo', :id => 123)
which comes out nicely /foo/123. i'd able call
p = post.create!(:title => 'foo') # let's assume gets id 123 url_for(p)
and same path out. error:
post_url failed generate {:action=>"show", :controller=>"posts", :title=>#<post id: 123 title: "foo" created_at: ...
how specify named route should use model's columns _url , _path functions?
when declare route, way call requires number of parameters, , must specified in correct order or things can confused.
here typical routes:
map.none '/', :controller => 'none', :action => 'index' map.one '/:one_id', :controller => 'one', :action => 'show' map.two '/:one_id/:two_id', :controller => 'two', :action => 'show' map.three '/:one_id/:two_id/:three_id', :controller => 'three', :action => 'show'
when want call them, need specify parameters you've put in route or invalid:
none_path one_path(one) two_path(one, two) three_path(one, two, three)
you can include optional parameters @ end. it's bad idea mix , match automatic routing , manual routing methods:
# using named routes one_path(one) # /one/1 one_path(one, :two_id => two) # /one/1?two_id=2 one_path(:one_id => one) # awkward format same # using automatic routing url_for(:controller => 'one', :action => 'show', :one_id => one) # /one/1
path parameters in brackets (:format)
optional these best avoided except when there safe defaults.
you're tripping url_for
method including 2 parameters in route instead of :id
.
Comments
Post a Comment