I wanted to create a controller called Database Importer.
Here's what I ran:
rails generate controller database_importer index
Which generated:
class DatabaseImporterController < ApplicationController
def index
end
def import
# to do.
# Receive the uploaded CSV file and import to the database.
CSV.foreach("parse.csv") do |row|
end
end
end
When I visit http://0.0.0.0:3000/database_importer/index I get:
Routing Error
No route matches {:action=>"import", :controller=>"database_importer"}
Try running rake routes for more information on available routes.
Here is my relevant route:
get 'database_importer/index'
Why is the routing engine trying to push me towards the import action method?
When I try to change the route to reflect the actual controller name (it doesn't have the underscore), I get:
get 'databaseimporter/index'
Routing Error
uninitialized constant DatabaseimporterController
Try running rake routes for more information on available routes.
Do you also have a route defined like:
post 'database_importer/import'
I think the form action is what is failing when Rails is trying to generate the route for the form.
Related
I'm fairly new to rails, but I have completed a couple of projects before, including the Michael Hartl Tutorial.
I'm building a simple app that stores a virtual wardrobe.
I've got 2 tables - users and items - where a user has_many items and an item belongs_to a user.
I set up the following named route in my routes.rb file:
match "/wardrobe", to: "items#index"
However, when I try to go to /wardrobe in my browser I get a no route match error as follows:
No route matches {:action=>"show", :controller=>"items"}
I'm not sure why rails is trying to route via the show action when I've named the route through the index action.
These are the relevant actions in my ItemsController:
def show
#item = Item.find(params[:id])
end
def index
#items = Item.all
end
The redirect is called on create as follows:
def create
#item = Item.new(params[:item])
if #item.save
flash[:success] = "Item added"
redirect_to wardrobe_path
else
render 'new'
end
end
rake routes provides the following:
wardrobe /wardrobe(.:format) items#index
So, I know the route exists.
Can anyone explain what's going on here? And how I can go about fixing it?
Thanks in advance
It might be because it's rake route is called wardrobe_path rather than wardrobes_path (plural) - when it's singular Rails will default to show action I believe. That might be causing the confusion.
I'm trying to achieve full internationalization of my routes in a Rails3.1 app. I'm already using Francesc Pla's rails-translate-routes to localize route actions and resources. The last step is to be able to translate slugs for some of my models.
Route to be translated:
http://myhost.com/products/pharmacy --> http://myhost.com/productos/farmacia
I have a nested route of the form
# routes.rb
match 'products/:category_slug' => "products#index"
I have a model Category with an instance #<Category id: 1, slug: "pharmacy"> and I do find_by_slug category in my ProductsController.
Any ideas on how to do translate the :category_slug part of the route?
As far as I'm aware, you can call translation helpers directly from your controller as long as you namespace correctly with I18n.
So your ProductsController could contain something like the following:
class ProductsController < ApplicationController
def index
i18n_slug = I18n.t("locale.category.#{params[:category_slug]}")
#category = Category.find_by_slug(i18n_slug)
end
end
You should probably inform yourself as to potential security risks of passing the params directly into the translation engine, though I'm not aware of any. You might also consider
moving that into a before filter or into the application controller if it will be used in multiple controller actions.
I'm new to Rails and currently using Rails 3, so please bear with me. I have a basic app, with a basic scaffolded controller/model e.g Contacts.
Amongst the methods for Show/Edit etc.. i have added a method called newcontacts (i have also added a newcontacts.html.erb), which will eventually show the last 5 contacts imported , but at the moment i am using the same code one would find in the basic Index method of a controller (i intend to filter the data at a later point), the method in the controller is -
def newcontacts
#contacts = Contact.all
respond_to do |format|
format.html # index.html.erb
end
end
I can access localhost:3000/contacts which displays the index method action from the contact controller, but when i try and access this method (newcontacts) using localhost:3000/contacts/newcontacts it returns the error
Couldn't find Contact with id=newcontacts
I have looked at the routes.rb file as i believe this is what needs editing, and have added the following line to routes.rb
match 'newcontacts', :to => 'contacts#newcontacts'
but this only works when i call localhost:3000/newcontacts.
So my question is, how do i get the url localhost:3000/contacts/newcontacts to work?
Any help would be great.
I think what you're trying to do is add another RESTful action.
resources :contacts do
# This will map to /contacts/newcontacts
get 'newcontacts', :on => :collection # Or (not and; use only one of these)...
# This will map to /contacts/:id/newcontacts
get 'newcontacts', :on => :member # ... if you want to pass in a contact id.
end
Try this in your routes.rb file:
resources :contacts do
member do
put 'newcontacts'
end
end
That will add in a new action for the contacts controller.
I want to have a button which goes to a random user on my site. I am using the friendly_id gem so the URLs are, for example, /users/dean and I've also set it up so its /dean.
I'm guessing I would add something similar to this in my routes.rb file:
match '/users/random' => 'users#index'
And then some extra code in the user controller?
How would I go about doing this?
Many thanks.
I'd do this:
Define a class method random on User model (or in a module that's included into your model if you'd want to reuse it for other models later).
class User
def self.random
offset = rand(count)
first(:offset => offset)
end
end
Other ways of getting a random record, if performance becomes an issue.
Add a random action in your UsersController like this
def random
redirect_to User.random
end
And finally create a route
match '/users/random' => 'users#random'
I would have a specific action random in the user controller and localize the logic for choosing a user there. Return a redirect to the route to that user from that action. I would prefer this over complicating the index action with extra logic to handle a different action.
I'm trying to make use of rails 3's respond_to/respond_with mechanism for restful controllers. I'm finding that when I try and use those features within a namespaced controller the redirects fail and i have to specify the optional location: parameter on the respond_with.
So right now I have:
def Admin::FooController
respond_to :html, :xml
def create
#foo = Foo.new(params[:foo])
#foo.save
respond_with(#foo, location: admin_foo_path(#foo))
end
end
If I don't provide the location parameter it attempts to redirect to just foo_path(#foo)
Any ideas as to why this would be / if I'm doing something wrong or does rails 3's default responder just not handle namespaced URL routes?
respond_with(:admin, #foo)
This blog has more..