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.
Related
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}/ }
I'm trying to use the high_voltage gem to serve static pages in my Rails app. What I want is for individual sections to get their own folder, but can't quite get it to work & can't find a solution around the web.
What I want:
RAILS_ROOT/app/views/pages/(page) to be routed as '/(page)'
While RAILS_ROOT/app/views/pages/(directory)/(page) => '/(directory)/(page)'
Here's my attempt:
routes.rb:
Cam4::Application.routes.draw do
root :to => 'high_voltage/pages#show', :id => 'index'
match '/:id' => 'high_voltage/pages#show', :as => :static, :via => :get
scope "ruby" do
match '/ruby/:id' => 'high_voltage/pages/ruby#show', :as => :static, :via => :get
end
end
Thanks a lot,
Cameron
Actually ended up solving the problem on my own using route globbing.
Given a Rails 3.2.5 app running high_voltage, with view paths:
RAILS_ROOT/app/views/pages/id [=> '/pages/id' or just '/id']
RAILS_ROOT/app/views/pages/ruby/id [=> 'pages/ruby/id' or 'ruby/id']
Routes.rb:
Cam4::Application.routes.draw do
root :to => 'high_voltage/pages#show', :id => 'index'
match '/*id' => 'high_voltage/pages#show', :as => :static, :via => :get
end
Using Rails 3.1 (not sure if this is applicable to 3.0, etc.)
In routes.rb what's the difference between:
match "team" => "users#index"
and
match "team" => "users#index", :as => :team
I ask because docs say:
3.6 Naming Routes
You can specify a name for any route using the :as option.
match 'exit' => 'sessions#destroy', :as => :logout
This will create logout_path and logout_url as named helpers in your application. Calling logout_path will return /exit
But, in first example above I have access to team_path & team_url in my views?!? So what's the :as => :team do exactly? I must be overlooking something as I've seen example code written like:
match "logout" => "sessions#destroy", :as => :logout
match "login" => "sessions#new", :as => :login
match "signup" => "users#new", :as => :signup
though from my limited testing the :as => :something seems redundant?!?
It seems redundant but it's not... when the name of your route differ from the name you want to give.
The ActionDispatcher does a lot of things by default. You should try to trigger rake routes in your console to test this behavior.
Another example is the shortcut:
match "account/profile"
# same as
match "account/profile", :to => "account#profile"
which will create the named route: account_profile
I have a working application with proper routes.
The application then became shared with another application with a homepage below the two sites.
-- Root Homepage --
/ \
| |
Website 1 Website 2
Now when I go to my page, the site loads. But the functioning does not.
My Routes
match '/generate_csv/(:id)', :to => "main#generate_csv", :via => :post, :as => "generate_csv"
resources :main
root :to => 'main#new'
Currently, my application is prepended by rails/website_1
How can I get my routes to line up again?
UPDATE
So this resolves the #create method, but I am still trying to get that first match route working..
scope "rails" do
resources :main
match 'main/generate_csv/(:id)', :to => "main#generate_csv", :via => :post, :as => "generate_csv"
end
The problem is solved by writing the routes twice. This can't be right:
scope "rails" do
resources :main
match 'main/generate_csv/(:id)', :to => "main#generate_csv", :via => :post, :as => "generate_csv"
end
resources :main
match 'main/generate_csv/(:id)', :to => "main#generate_csv", :via => :post, :as => "generate_csv"
root :to => 'main#new'
It might be easier to use a subdomain as an alternative when merging two separate sites by doing something like:
constraints(:subdomain => /website1/) do
match '/' => 'website2/dashboard#index'
end
Otherwise, maybe try a scope:
scope module => "website1" do
resources :main
end
I am new to Rails. I found it very strange when I use the resources in routes.rb, after I redirect the page to controller/index, it render the controller/show .
I know GET controller/action is same as match "controller/action", :to => "controller/action"
I think the strange thing happens to me about the redirect, is similar to the GET and Match.
so I wonder what exactly the resources mean, can I use some simple match do the same thing?
resources is a shortcut for generating seven routes needed for a REST interface.
resources :widgets is equivalent to writing
get "widgets" => "widgets#index", :as => 'widgets'
get "widgets/:id" => "widgets#show", :as => 'widget'
get "widgets/new" => "widgets#new", :as => 'new_widget'
post "widgets" => "widgets#create", :as => 'widgets'
get "widgets/:id/edit" => "widgets#edit", :as => 'edit_widget'
patch "widgets/:id" => "widgets#update", :as => 'widget'
put "widgets/:id" => "widgets#update", :as => 'widget'
delete "widgets/:id" => "widgets#destroy", :as => 'widget'
it just saves you the trouble.
By the way, get is not exactly the same as match. get, post, put and delete are shortcuts for limiting the route to a single HTTP verb. The two route definitions below are equivalent.
match 'foo' => 'controller#action', :method => :get
get 'foo' => 'controller#action'