My rails version is 2.3.5(2.3+)
How can I visit urls with .html suffix?
Just like localhost:3000/welcome.html (welcome is a controller).
I got routing errors when I visit urls above.But it works if the url with format param like this:
localhost:3000/welcome?format=html
In routes.rb:
ActionController::Routing::Routes.draw do |map|
map.root :controller => "welcome"
map.resources :users
map.resource :session
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
But but but I found localhost:3000/users.html works.
Use this route to connect to an controller:
map.connect "welcome.html", :controller => :welcome, :action => :index
Whether there is .html does not matter for routing purposes, it is just like any other path connecting to any other controller. So no need to modify your controller for this.
Using the format=html results in a parameter, so a controller can return the specific type of result, which is not what you want according to your question.
According to your information this (allowing .html in your paths) is automatically implemented when creating routes with the map.resources method. Since it is working for users in your example.
You could try this:
map.connect ':controller.:format', :action => :index
Related
I have some problems using will_paginate and named routes.
Here is some code (my site is in Spanish language):
routes.rb
map.animals '/animales/:scope/:id', :controller => :categories, :action => :show
with these routes I generate URLs like:
www.domain.com/animales/mamiferos/perros
but, when pages links are generated I get links like:
www.domain.com/animals/perros?page=2&scope=mamiferos
Why are they like that?
NOTE: I am also using friendly_id.
You need to make sure that there is no matching route before the animals route in the routes.rb file. E.g. the default route map.connect ":controller/:action/:id" and the resource definition map.resources :animals should come after the named animals route.
I've put this line in my routes.db file:
map.mything '/mything', :controller => 'mything', :action => 'list'
But I get this error when I go to http://localhost:3000/mything, I get this error:
Unknown action
No action responded to index. Actions: list
Why is it trying to use index instead of list? I thought that by setting
:action => 'list'
it would use the list action? Thanks for reading.
You have to put named routes above the default routes.
I put named routes like these at the top of routes.rb so they always get evaluated first.
ActionController::Routing::Routes.draw do |map|
map.about 'about', :controller => 'home', :action => 'about'
map.contact 'contact', :controller => 'home', :action => 'contact'
# MORE CONFIG
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
Agreeing with Jim Schubert, put the named routes above the default routes.
Another likely problem is that you have something like:
map.resources :mything
which is setting an index action on the controller as a result of you scaffolding a model
Sorry for asking a potentially obvious question, but have you tried restarting the app? Certain routes will not register until you restart the application (RESTful resources never need an application restart, but others often do).
Currently we are using method_missing to catch for calls to SEO friendly actions in our controllers rather than creating actions for every conceivable value for a variable. What we want are URLS like this:
/students/BobSmith
and NOT /students/show/342
IS there a cleaner solution than method_missing?
Thank you!
You can define a route for that particular format fairly easily.
map.connect "/students/:name", :controller => :students, :action => :show, :requirements => {:name => /[A-Z][A-Z]+/}
Then in your show action you can find by name using params[:name].
You can create a catch-all route. Put this at the bottom of config/routes.rb with whatever controller and action you want:
map.connect '*path', :controller => '...', :action => '...'
The segments of the route will be available to your controller in the params[:path] array.
I'm new to Ruby on Rails, and I'm sure I'm just missing something simple and stupid, but I can't figure out how to get my 'account/signup' action working.
This is what I have in my routs file:
map.connect ':controller/:action'
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
map.root :controller => "home"
I've added this to my accounts controller:
def signup
#account = Account.new
respond_to do |format|
format.html # signup.html.erb
format.xml { #account }
end
end
And I've added signup.html.erb to the accounts views folder.
Yet when I go to it in the browser I get this error:
ActiveRecord::RecordNotFound in AccountsController#show
Couldn't find Account with ID=signup
What am I doing wrong?
Add the following code right on top of your routes.rb file
ActionController::Routing::Routes.draw do |map|
map.connect 'account/signup', :controller => 'account', :action => 'signup'
...
...
...
end
Also I think you mean Account and not Accounts.
Here's a tip:
If you run rake routes it'll show you all the possible routes for your app.
It should then be obvious depending on what URL you are entering whether it'll be correctly resolved or not.
For a good overview of routes read this guide. There's really a lot of stuff you can do with routes so it's worth taking some time to read it.
If you want to follow the REST model, you controller should be called sessions and your signup action should be new, so in your routes you could do :
map.resources :sessions
This website is highly recommended to all newcomers to Rails :
http://guides.rubyonrails.org/
The following will do as well when added to the do |map| section of routes.rb
map.resource :account, :member => {:signup => :get}
Will create the standard routes for your accounts controller, as well as add the new route account/signup. It also provides the usual url helpers in addition to signup_account_url and signup_account_path
I am working on a project in ruby on rails and I am having a very difficult time with a basic problem. I am trying to call a custom action in one of my controllers, but the request is somehow getting redirected to the default 'show' action and I cannot figure out why.
link in edit.html.erb:
<%= link_to 'Mass Text Entry', :action=>"create_or_add_food_item_from_text" %>
Error from development.log:
ActiveRecord::RecordNotFound (Couldn't find Menu with ID=create_or_add_food_item_from_text): app/controllers/menus_controller.rb:20:in `show'
routes.rb file:
ActionController::Routing::Routes.draw do |map|
map.resources :nutrition_objects
map.resources :preference_objects
map.resources :institutions
map.resources :locations
map.resources :menus
map.resources :food_items
map.resources :napkins
map.resources :users
map.resource :session, :controller => 'session'
map.root :controller=>'pages', :action=>'index'
map.about '/about', :controller=>'pages', :action=>'about'
map.contact '/contact', :controller=>'pages', :action=>'contact'
map.home '/home', :controller=>'pages', :action=>'index'
map.user_home '/user/home', :controller=>'rater', :action=>'index'
map.user_napkins '/user/napkins', :controller=>'rater', :action=>'view_napkins'
map.user_preferences '/user/preferences',:controller=>'rater', :action=>'preferences'
map.blog '/blog', :controller=>'pages', :action=>'blog'
map.signup '/signup', :controller=>'users', :action=>'new'
map.login '/login', :controller=>'session', :action=>'new'
map.logout '/logout', :controller=>'session', :action=>'destroy'
# Install the default routes as the lowest priority.
map.connect ':controller/:action'
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
Menus_controller.rb:
class MenusController < ApplicationController
...
def create_or_add_food_item_from_text
end
...
end
create_or_add_food_item_from_text.html.erb simply has a div to show a form with a text box in it. I have the rest of my app working fine, but this is stumping me.
Any help is appreciated.
Try adding the route to your file explicitly, before the :menus resources:
map.connect "/menus/create_or_add_food_item_from_text",
:controller => "menus", :action => "create_or_add_food_item_from_text"
map.resources ...
Routes declared earlier have higher priority, and the problem here is that map.resources actually prevents certain paths from being routed.
Even regardless of this issue, it's good practice to map all paths explicitly, either through resources or named/unnamed routes, and ultimately eliminate the generic :controller/:action and :controller/:action/:id routes from your app.
link_to expects the path to your action as the second parameter - it looks like you are passing link_to the wrong path value. Check the development log to see what path rails thinks you are looking for.