Correct route file - ruby-on-rails

Can someone help me to correct that?
match 'graphs/(:id(/:action))' => 'graphs#(:action)', :via => [:get, :post]
This is working as expected if parameter :action is specified but if it is missing I get :action not found. Is it possible to have a default controller if :action is missing or :id and :action together?
Thanks

you don't have to use 'controller#action'. You can also only specify the controller there:
match 'graphs/(:id(/:action))' => 'graphs', :via => [:get, :post]

Related

How can I redirect to a specific controller/action with similar routes?

My routes are redirecting to the same controller even when I specified different properties inside my routes.rb file.
These are my routes.
match ':clube_id' => 'clubes#show', :as => 'clean_cluble', via: [:get]
match ':project_id' => 'projects#show', :as => 'clean_project',via: [:get]
These are the links that I am using.
=link_to 'Project', :controller => "projects", :action => "show", :project_id=>'xxxxx'
=link_to 'Clube', :controller => "clubes", :action => "show", :id=>'cccc'
The link for projects works well, but the linl for clubes is redirecting to projects controller. that is the problem.
The URLs that I spect are:
http://host_name/project_name
http://host_name/clube_name
You didn't specify different properties, both routes look's identical for Rails. The match method expect any string(or id) in the ':clube_id' or ':project_id', for example:
host_name/soho_project or host_name/1
How is Rails can recognize for a which model it's related? It can be a Project or Club. I suggest add something like the anchor to a match method.
match 'club/:clube_id' => 'clubes#show', :as => 'clean_cluble', via: [:get]
match 'project/:project_id' => 'projects#show', :as => 'clean_project',via: [:get]
and helpers:
= link_to 'Project', clean_project_path(:project_id=>'xxxxx')
= link_to 'Clube', clean_cluble_path(:clube_id=>'cccc')
Read more about routes from the Rails guides.

rails translated paths example

routes.rb
match "about/how_it_works" => "about#how_it_works", :as => "about_how_it_works", :via => :get
match "about/we_are" => "about#we_are", :as => "about_we_are", :via => :get
match "about/what_is" => "about#what_is", :as => "about_what_is", :via => :get
I read this rails guide and changed my code.
new routes.rb
scope(path_names: { about_we_are: 'translated-about-we-are', about_what_is: 'translated-about-what-is' }) do
resources :about, path: 'translated-about'
end
But when I enter localhost:3000/about/translated-about-we-are, I encounter no route matches error.Do you know how can handle with this problem?
Since you've specified path for resources about your path becomes translated-about/.... So you need to use:
http://localhost:3000/translated-about/translated-about-we-are
then you should not get the error.
You can check all the routes generated by issuing rake routes from within your application directory.

how do I ignore a term in a route?

I'm creating a route that should have an optional/ignored last term.
Like so:
/product/12345/Dark-Knight-Rises # last term is just there for a nice URL
I was thinking, from reading the docs, that I'd just be able to wildcard the last term:
match 'product/:uid/*full_name' => 'product#view', :via => [:get]
That didn't work. I did get this to work:
match 'product/:uid/:full_name' => 'product#view', :via => [:get]
match 'product/:uid' => 'product#view', :via => [:get]
But, well, it seems like this should be doable in one line. Yes?
match 'product/:uid(/:full_name)' => 'product#view', :via => [:get] is what you are looking for
The parenthesis make the full_name an optional parameter which you can just ignore since all you want is a pretty URL.
Below single line should work
match 'product/:uid/:full_name' => 'product#view', :via => [:get]

Rails routing, matching short urls?

I'm having an issue where I've created a route I'm using to match short token like urls, like this:
myapp.com/a2c3b
I'm doing that by using a route like this:
match '/:id' => 'items#show', :as => "show_item", :via => :get, :constraints => { :id => /[a-z0-9]{5}/ }
But the issue is that now my other routes like /admin don't work because that also has 5 characters, how can I work around this, and have both kinds of routes work?
Put all of your routes that would match before this route in the file... that is...
match '/admin'....
match '/login'....
match '/:id' => 'items#show', :as => "show_item", :via => :get, :constraints => { :id => /[a-z0-9]{5}/ }

How do you create a route to a specific show item?

I would like 'about' to route to 'abouts/1'
I tried this:
match 'about' => 'abouts#show/1', :via => get
and it doesn't work. Any ideas?
How about:
match 'about' => 'abouts#show', :via => :get, :defaults => {:id => 1}
What about just removing the 1 from the route and retrieve the record you want directly in the controller method?
# routes.rb
match 'about' => 'abouts#show', :via => get
# abouts_controller.rb
def show
#about = About.find(1)
end

Resources