I'm trying to add a specific attribute of a record in Rails to the URL from something like:
domain.com/share/5
(where 5
is the record ID) to something like:
domain.com/share/johnsmith/5
where johnsmith
is stored in record 5
. I'm alternating between these two routes to no success:
get "/share/:name/:id" => "share#show"
resources :share, :only => [:show]
And between these two methods:
share_path(doc.user.name, doc)
share_path(doc)
The show method in the ShareController
is pretty standard.
The problem:
Using share_path(doc.user.name, doc)
in the view generates a link to /share/johnsmith.5
, instead of /share/johnsmith/5
.
get "/share/:name/:id" => "share#show"
should do the job. But you may have to look at the order of routes in routes.rb
, maybe Rails took the wrong route?
Best tip to look at what's happening:
Call the URL in your browser (or using curl
or whatever) and then look into your console where your started rails s
(or rails server
).
There you should see something like this:
Processing by ShareController#show
Parameters: {"id"=>"5", "name"=>"johnsmith"}
Concerning the path
methods:
Simply use rake routes
, it will tell you which path methods are available.
No idea what happened but it resolved itself with this:
get "/share/:name/:id" => "share#show", :as => :share
share_path(doc.user.name, doc)
I do not get the .
and /
issue at all. I restarted everything and it was gone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.