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
Related
I have two routes (defined in my config/routes.rb)
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report
I would like to know how I can tell my controller to render the view based on my route, without adding a parameter on my URL.
Based on this Question&Answer I Managed to get the result I want with
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare, :defaults => { :render => 'compare' }
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report, :defaults => { :render => 'full_report' }
And in my controller I defined my action as:
def search_by_id
render :action => (params[:render] || "full_report")
end
But is this a Good Practice or is there a better way to do it?
Instead of creating different routes for each category you are making for simplifying you can write it like:
# config/routes.rb
get ":category", to: "front_office#search_by_id", as: 'front_office', constraints: {category: /(compare|full_report)/}
the above routes looks for /compare and /full_report and this will call search_by_id action in front_office controller.
then inside the controller do as follows:
def search_by_id
render params[:category]
end
params[:category] will hold the slug values which we passed through the URL
I am getting below error when I am trying to route the about page which I did with rails g scaffold about.
undefined local variable or method `map' for main:Object Did you mean? tap
This is what I have in the routes.rb file.
map.about '/about', :controller => 'abouts', :action => 'about'
Use
get '/about', :controller => 'abouts', :action => 'about'
or
post '/about', :controller => 'abouts', :action => 'about'
or
match '/about', :controller => 'abouts', :action => 'about', via: [:get, :post]
Run rails g scaffold about this command will generate multiple files, include model, views, and will write resource :abouts to the routes.rb file.
The routes syntax is not right.
map.about '/about', :controller => 'abouts', :action => 'about'
if you just want to create a about page, url set as /about
create a controller named abouts_controller.rb, in the /app/controllers folder
create a action def index; end in abouts_controller.rb
create a view file named abouts.html.erb in the folder app/views/abouts.(you will creat the folder)
write routes to routes.rb
get :about, :controller => :abouts, :action => :index
or
get '/about' to: "abouts#index"
or
match '/about', :controller => 'abouts', :action => 'about'
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}/ }
This is the route the Vanity gem generates:
controller :vanities do
match ':vname' => :show, :via => :get, :constraints => {:vname => /[A-Za-z0-9\-\+]+/}
end
This is the rake routes:
GET /:vname(.:format) {:vname=>/[A-Za-z0-9\-\+]+/, :controller=>"vanities", :action=>"show"}
How do I use the Rails link helper to link directly to URL mydomain.com/vname?
From the top of my head (sorry, I don't really have the time to test it right now):
controller :vanities do
match ':vname' => :show, :via => :get, :constraints => {:vname => /[A-Za-z0-9\-\+]+/}, :as => :vanity
end
which you would use like this:
vanity_path(:vname => "marcamillion")
This is a pretty silly question but I'm having some real trouble figuring out. I want to convert the following routes to make it compliant for Rails 3 (from 2.8.x):
map.with_options :controller => 'static_pages', :action => 'show' do |static_page|
static_page.faq 'faq', :id => 'faq'
static_page.about 'about', :id => 'about'
static_page.blog 'blog', :id => 'blog'
static_page.support 'support', :id => 'support'
static_page.privacy 'privacy', :id => 'privacy'
static_page.howitworks 'howitworks', :id => 'howitworks'
static_page.contact 'contact', :id => 'contact'
static_page.terms_and_conditions 'terms_and_conditions', :id => 'terms_and_conditions'
end
Any help would be much appreciated!
I think I would do it like this:
scope '/static_pages', :name_prefix => 'static_page', :to => 'static_pages#show' do
for page in %w{ faq about blog support privacy howitworks contact terms_and_conditions }
match page, :id => page
end
end
This is awesome, I just wrote an article about this a couple weeks ago:
Routing in Ruby on Rails 3
It goes over most aspects of the conversion, with a downloadable sample app. While I didn't cover the with_options conversion specifically, I can do a little of that here. Here's a short way:
scope :static_pages, :name_prefix => "static_page" do
match "/:action", :as => "action"
end
This matches all the routes you have above, and your named routes would look like this:
static_page_path(:faq)
static_page_path(:about)
...and so on. If you want your named routes to still look like static_page_faq_path then you can specify them one at at time, like so:
scope '/static_pages', :name_prefix => 'static_page' do
match '/faq', :to => 'static_pages#faq'
match '/about', :to => 'static_pages#about'
# fill in all the rest here
end
I hope this helps!