Make routes upperscore instead of underscore? - ruby-on-rails

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

Related

rails showing . instead of / in url

I have a static_controller with index action where an id is needed
My routes
get 'faqs' => 'static#main'
get 'faqs/:id' => 'static#index'
but if I use this path faqs_path(faq_id) I get
http://localhost:5000/faq.1 instead of http://localhost:5000/faq/1
Could someone tell me how to fix this. Thank you..
You may need to name them properly:
get 'faqs' => 'static#main', as: 'faqs'
get 'faqs/:id' => 'static#index', as: 'faq'
Normally you do this with resources where you try to adhere to REST conventions, but in your case if you really need them this way you'll have to coach the router.
Check with rake routes that the names are correct. You may have been calling faqs_path with the id going in as the optional :format specifier.
I think your problem here is same routes name, you can change your name as:
get 'faqs/:id' => 'static#index'
get 'faqs' => 'static#main'
to
get 'faq/:id' => 'static#index'
get 'faqs' => 'static#main'
And your path is: faq_path(faq_id)
Tell me if it didn't work.
Can you try. faqs_path(:id => faq_id)

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

Route in rails with simple regex doesn't match

I looked on the web for a while but I can't get this to work. Our application has to work with urls like ourapp.com/meandyou, where the common element is the "and" in the parameter.
I saw that it's possible to constrain urls parameters using regex, so I added the rule to routes.rb, but without success. If I try to match the same expression using the terminal, it works. Here's the complete route file:
Railroot::Application.routes.draw do
resources :couples
get "home/index"
root :to => 'home#index'
match ':url' => 'couples#show_url', :url => /and/
end
I read that Rails nests the expression within a bigger one when matching the route, so maybe I'm doing something slightly wrong even for such a simple expression.
I'm running on Ubuntu 10.04, Ruby 1.9.3, Rails 3.2.3, Passenger 3.0.13, Nginx 1.2.1.
Thanks in advance for your help!
This should be your starting point:
Railroot::Application.routes.draw do
root :to => 'home#index'
resources :couples
match ':url' => 'couples#show_url', :constraints => { :url => /and/ }
end
This may be your answer, from the rails routing docs:
:constraints takes regular expressions with the restriction that regexp anchors can’t be used. [...]
However, note that you don’t need to use anchors because all routes are anchored at the start.
So I think what you are actually matching against is not /and/ but /^and/, which would explain why it's not working.
Try being more explicit, like this:
match ':url' => 'couples#show_url', :url => /.*and/

How can I rename a Rails controller with a route?

I have a controller in a Rails 3 app named "my_store." I would like to be able to use this controller as is, except replacing "my_store" in all the URL's with another name. I do not want to rename the controller file, and all the references to it. Is there a clean way to do this with just a routing statement?
If you use RESTful routes:
resources :another_name, :controller => "my_store"
Otherwise:
match "another_name" => "my_store"
If your routes are RESTful, this is pretty easy.
resources :photos, :controller => "images"
You can see how to do this and other helpful Rails routing information in the Rails routing guide.
Update, the other guys are correct, to replace all references you would change the resources name and corresponding controller in routes.rb! My answer is only good to set a specific route.
Yup, you would do this in your routes.rb using the :as option to specify
example:
match 'exit' => 'sessions#destroy', :as => :logout
source

problem in routes

i want to change the default route in RoR to what i want:
consider the following example...
:controller/:action/:id
which will give you the route in the browser as:
http://localhost:3000/controller/action/id
now i want to change it to...
http://localhost:3000/this-is-what-i-want/id
we can get an alias for the controller and the action as well like...
resources :controller, :as => "my-custom-name"
and if you want to have the alias for the action, then
resources :controller, :path_names => { :action => 'my-custome-name-1', :action => 'my-custome-name-2' }
BUT i want to change the controller and the action at once... if u noticed the above http://localhost:3000/this-is-what-i-want/id path in the question...
need help...
thanks in advance...
You need a named route.
In Rails2:
map.a_name 'this-is-what-i-want/:id', :controller => 'controller_name', :action => 'action_name'
In Rails3:
match 'this-is-what-i-want/:id' => 'controller_name#action_name'
You want to be using Rest routes, rather than controller/action
I'm going to use "balls" instead of "this-is-what-i-want"
resources :balls
Then, when you link to a ball, do link_to(ball.name, ball).
This will give you a link of http://localhost:3000/balls/45
This rails rest cheatsheet is a good start.

Resources