Rails: Generate URL without parameters - ruby-on-rails

For example I have simple routes like:
get 'something/:param' => 'controller#action'
Request path in this case is /something/123 for example
Being in controller how to generate URL w/o parameter(s) like /something using Rails features?
Other examples:
get 'something/:param/action' => 'controller#action': '/something/action'
get 'something/:param/:param2' => 'controller#action': '/something'

You mean like this? You just get rid of the :param.
get 'something/' => 'controller#action'
get 'something/action' => 'controller#action'

If you are looking to protect or obscure the ids in the routes, or simply have nicer looking urls, then you might want to try the friendly_id gem.

Related

Can Ruby on Rails Routes point a nested URL at a single action?

Is it possible in Ruby on Rails (we're using v2 still) to allow the routes file to map a nested url EG
mydomain.com/controller/object/action
to a single action eg
:controller, :action
?
We currently have a url like
mydomain.com/controller/action
and i want to change it to
mydomain.com/controller/object/action
Thanks in advance
You can write singular route like as follow. You will get record id in params[:id] in controller action.
match 'controller/:id/action' => 'controller#action', via: :get

Adding a record's attribute to the URL before the ID

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.

Rails: pretty_id - hide the resource

I am trying to setup a pretty url that does not show the resource on the route.
For example, instead of:
localhost/users/newton
show
localhost/newton
Is that possible with the pretty_id gem?
Thanks!
i think you mean the friendly_id gem? Either way, you can, but in the routes file, you would just need to specify that you want to use the users controller.
get ':slug' => 'users#show', :as => 'user_show'

Custom url in ruby on rails

I know rails uses the controller action style urls like www.myapp.com/home/index for example
I would like to have a url like this on my rails app, www.myapp.com/my_page_here is this possible and if so how would I go about this?
You just use a get outside of any resources or namespace block in your routes.rb file:
get 'my_page_here ', :to => 'home#index'
Assuming you are using Rails 3+, do NOT use match. It can be dangerous, because if a page accepts data from a form, it should take POST requests. match would allow GET requests on an action with side-effects - which is NOT good.
Always use get, put, post or these variants where possible.
To get a path helper, try:
get 'my_page_here ', :to => 'home#index', :as => :my_page
That way, in your views, my_page_path will equal http://{domain}/my_page_here
you just need to make a routing rule to match that url
in this case it will be something like
match 'my_page_here' => 'your_controller#your_action'
your controller and action will specify the behavior of that page
so you could do
match 'my_page_here' => 'home#index'
or
get 'my_page_here', :to => 'home#index'
as suggested in other responses.
for index action in home controller if you have such a controller
see http://guides.rubyonrails.org/routing.html for more details
also see Ruby on Rails Routes - difference between get and match

Make routes upperscore instead of underscore?

Very basic and maybe I missed it, but instead of doing something like sign_up for my routes, how can I do sign-up? This is assuming I have these route like this:
get "/sign_up" => "devise/registrations#new"
It doesn't work for me if I change it to: get "/sign-up".
Try this instead:
match "/sign-up" => "devise/registrations#new", :as => :sign_up
Unfortunately ActionDispatch works best with underscores. So you might try compromising by keeping with convention for the sign_up_path helper and using the dash in the URL

Resources