rails showing . instead of / in url - ruby-on-rails

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)

Related

How to make quora.com-like URL in Ruby on Rails?

For example,
http://www.quora.com/Is-getting-ripped-worth-it
As you can see, the URL for a question does not have a controller's name. It is associated with the root URL. How can I do this?
In routes.rb, I tried the following:
match '/:id' => "questions#show"
That works, but new_question or edit_question paths still generate /questions/new(.:format) and /questions/:id/edit(.:format) as URLs.
Are there any elegant solutions for this problem?
There is nothing that prevents you from doing this:
match '/new' => "questions#new", :as => 'new_question'
match '/:id/edit' => "questions#edit", :as => 'edit_question'
but you'll have to remove :resources questions
One elegant solution is to use the friendly_id gem: https://github.com/norman/friendly_id
You can find similar ones at slugs">http://www.ruby-toolbox.com/categories/rails_permalinks_slugs

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

Rails routes match starting with a numeric character

I have a an url like "http://domain.com/1and2" that I wanted to set up in config/routes.rb like this:
match "1and2" => "frontpage#oneandtwo"
(with controllers and views in place).
Running 'rake routes' outputs 'Invalid route name: '1and2''. This error is apparently triggered when you start a match with a numeric character.
Is there a workaround, or am I doing it wrong?
match '/:id' => "frontpage#oneandtwo", :constraints => {:id => /1and2/}
The root of the problem is that methods in Ruby cannot start with a number. Since Rails routing will generate an accessor method for each route, you'll get an error.
You can pass by the issue by naming your route differently with the :as parameter.
I had an issue where I wanted to redirect from a URI /2012 -- which resulted in an error. I corrected it by adding :as => current_year to the routing:
match "/#{Time.now.year}" => redirect("..."), :as => :current_year
Further information:
https://github.com/rails/rails/issues/3224

Rails hide controller name

i have match ":id" => "people#show" in my routes.rb
now i can access http://localhost:3000/1
but,in views <%= link_to 'Show', people %> it will generate http://localhost:3000/people/1 ,
i want to it to be http://localhost:3000/1
You could do something like this to ensure that only numeric ids are matched:
match '/:id' => 'people#show', :constraints => {:id => /\d+/}
A good alternative might be to use some kind of identifier, even if it's not the controller name: http://localhost:3000/p/1. This will at least ensure that if you add other controllers and actions you don't end up having to change your routing structure.
You could write a custom route to match that in config/routes.rb. At the bottom of your routes.rb file you will have a route like match ':controller(/:action(/:id(.:format)))'
or something like resources :people. You might have to write a route that matches the route type you want.
You have to create a named route.
match ':id' => 'people#show', :as => :person
And fix your views to use your new method person_path(user_id).

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