What is the error while migration in rails? - ruby-on-rails

I tried to execute the following migration command:
rails g migration add_user_to_posts user_id:integer
but i got this error:
/mapper.rb:233:in `default_controller_and_action': missing :action (ArgumentError)
/mapper.rb:116:in `normalize_options!'
how to rectify these errors?

This is caused by incorrect routes - Please check your config/routes.rb check if any routes is defined incorrectly

The problem that you are getting is related to routes.
Check your routes.rb and there is a mapping of controller with its action. So if it's not proper,
the error will be thrown as:
`default_controller_and_action': missing :action (ArgumentError)
For example:
Common mistake-
root :to => "home/index"
Correct Way-
root :to => "home#index"
Check out the post: Default Controller and Actions.
As indicated,
The error says that the default controller is missing an action.

I think in your routes having problem.
make sure the
root_path
like,
root :to => 'home#index'

EDIT: The #Ved Prakash answer is correct, after more research I've found that my answer was not good enough. I'm leaving here just a relevant note for Rails 4.x
When you starting using Rails 4.x you have a new method to add references:
rails generate migration AddUserRefToProducts user:references

Related

Changing the route from posts/article to blog/article in rails

I'm in trouble with routes in Rails. I've been trying to get a path like: home/blog/title-article but the closes I get is: home/blog/posts/title-article
I've tried with namespace, scope and one by one with get 'blog' => 'posts#blablabla', but I get errors like UrlGenerationError or NameError every time I change the paths. I've read the official guide and some answers in this forum, but I'm getting more confused hehe
In my last try I generated a controller Blog and a scaffold Post. The output of rake routes is: http://i.stack.imgur.com/gdfPc.png
routes.rb
Rails.application.routes.draw do
scope :blog do
resources :posts
end
get 'blog' => 'blog#index'
root 'pages#index'
...
Thank you!
Now my routes are like: http://i.stack.imgur.com/cKsFG.png
Thank you!
Not sure its what you expect but try:
resources :posts, path: 'blog'
Feels weird though.
Btw, I guess you have the errors due to url helpers.
use rake routes to check the existing routes and the related url helpers.
Doc lives here

Getting error while using RefineryCMS Blog route

I instructed config/routes.rb to use Refinery Blog as a root directory:
root :to => "refinery/blog/posts#index"
mount Refinery::Core::Engine, :at => '/'
In a app/view/layouts/_header.html.slim I'm trying to use blog_root route. For example:
= link_to (image_tag "/logo.gif"), blog_root, class: "brand"
The route is listed when I issue rake routes:
blog_root /blog(.:format) refinery/blog/posts#index
But nothing shows up, the system gives an error:
undefined local variable or method `blog_root' for
#<#<Class:0x00000005e62f80>:0x007fd7241d94c8>
Also, I tryed blog_root_path, but it didn't work either.
Anything I can do in this situation? Thanks a lot!
This question/answer pair was helpful.
I looked inside config/routes.rb of main app, and in comments it was writted that 'We ask that you don't use the :as option here, as Refinery relies on it being the default of "refinery"'.
So, the working route is refinery.blog_root_path.

Rails 3.2 Routing Error

In my first approach with Rails I have simply create a void SayController and static hello.rhtml view but when the page http://localhost:3000/say/hello started return me a Routing Error like this:
No route matches [GET] "/say/hello"
Try running rake routes for more information on available routes.
Rails version: 3.2.6
Seems like you didn't add a route for hello to your config/routes.rb file.
YourApp::Application.routes.draw do
match 'say/hello' => 'say#hello', :as => :hello
end
This will match route say/hello to controller say (the part before #) and action hello (the part after #).
:as => :hello makes it a named route so you can refer to it as hello_path from within your app.
The error message tells you to run rake routes (from the console) which will show you the existing routes in your app.
You should have something in your config/routes.rb to define that route. Try:
match 'say/hello' => 'say#hello', :as => 'say_hello'
The go to localhost:3000/say/hello
Also check out this documentation:
http://guides.rubyonrails.org/routing.html
I assume, controller: say and action: hello
Add following to config/route.rb
get 'say/hello' => 'Say#hello'

Routing questions with rails

I am just starting with rails and I have a very simple case. I have a "home" controller. There is one action 'index' defined in this controller. When I go to ~/home I get an error msg saying:
uninitialized constant HomesController (I noticed the singular/plural thing).
That's the first thing I don't get (I thought it would automatically go to ~/home/index).
Second thing, if I go to ~/home/edit (note that this action doesn't exist yet), I get also:
uninitialized constant HomesController
But if I go to ~/home/show (show doesn't exist as well) I get a different error message:
No route matches "/home/show"
How can I get 2 differents errors for the same reason (an inexistant action). And What's the deal with this constant?
Thank you
Edit
I'm running rails 3.0
Here is my routes.rb file
Topnotch::Application.routes.draw do
resources :subscriptions
resource :home
get "home/index"
get "subscriptions/index"
root :to => "home#index"
end
You must add the resource "home" to the route.rb.
The controllers are considered to be plural.
If you are new to rails, I suggest you to start using generators - just open a terminal in the project's folder and type in "script/generate scaffold home" (in rails3 it would be "rails g home")
Changes the root route as below:-
root :to => "homes#index".
You must use the plural form in the routes.
Turns out, the routes were correct I was just not using them correctly !
rake routes helped.

Routing resources/paths with :path_prefix and :name_prefix

I have the following route definied:
map.resources :addresses, :path_prefix => ':site', :name_prefix => 's_'
I've had no problem correcting my scaffolding links for "Show" and "New". But I am getting a failure to generate error when attempting to use:
edit_s_address_path(address) or edit_s_address
rake routes shows that this is the proper path. I'm perplexed. Thanks in advance.
Shouldn't you use s_edit_address_path(address)? According to the Rails Guide on routing, the name_prefix comes at the start of the route name.

Resources