Keeps getting Routing Error.. what am I doing wrong? - ruby-on-rails

I am doing the very intro course in rails, and so far have completed the following
Made a new rails app: rails new shovell ("shovell" is the name of the app)
Installed all the gems and what not: bundle install
Generated a model rails generate model Story name link
Generated a controller rails generate controller Stories index
And now when I point to http://localhost:3000/stories, I get a error that says "Routing Error
No route matches [GET] "/stories" "
And the following is my routes.rb:
Shovell::Application.routes.draw do
get "stories/index"
# a bunch of comments
end
So I don't know what Im doing wrong, and why its not showing up the default welcome message, but rather giving me an error. Thanks for your help!

However, if you do:
http://localhost:3000/stories/index
you may get the page, although that's not the rails way.
First thing, read and understand the rails routing guide
Then in order to fix your code, on the routing you can write
Shovell::Aplication.routes.draw do
resources :stories
end
Or if you want custom routes instead of a rest resource
Shovel::Application.routes.draw do
match "stores", to: "my_controller#my_action"
end
And you can also name the custom route
Shovel::Application.routes.draw do
match "stores", to: "my_controller#my_action", as: :store_index
end
So with a name you can use the route name on your rails app
link_to("Store Index", store_index_path)

You've defined a route to /stories/index, but you've not defined one to /stories, which is why this is failing.
You should define this route like this:
get '/stories', :to => "stories#index"
For more information, see The Routing Guide

Related

path helper generating exeception on console while migrating from Rails-2 to Rails-3

I am trying to modify my script, i am migrating from rails 2.3 to rails 3.1 but i am facing a strange issue. I see that when i use a path helper like this
in rails 3.1 i am getting an exception but rails 2.3 it used to work, by work i mean when to the path helper i pass values and order.customer_id is nil, it generates a path to create new customer however in rails 3.1 i see it generates a exception, below is the description of the error i see in rails 3.1
helper.link_to customer_email, app.store_customer_path(store,order.customer_id) , when order.customer_id is blank i get a exception on console as below
Below is the error i observe in rails 2.3
ActionController::RoutingError: store_customer_url failed to generate from {:controller=>"customers", :action=>"show", :store_id=#<object>}
. but when i load a web page i see that i get a path generated to create a new customer.
here is my relevant routes.rb code
resources :stores do
resources :customers do
collection do
get :get_customers, :download, :csv_template
end
match :upload, :import, :map, :on => :collection
member do
get :more
end
resources :dropship_profiles
resources :address
end
end
But in rails 3.1
on console to i see exception and when loaded from browser also i see exception
I am not able to understand this and its confusing me, can anyone please help, Thanks.
After going through your routes.rb,i dont see any relevant path that routes to store_customer_path...kindly use the one present or create new one...verify by running rake routes > path.txt and then check your routes easily in path.txt
=============UPDATED ANSWER==============
here you are passing two values,you just need to pass one
Instead of helper.link_to customer_email, app.store_customer_path(store,order.customer_id)
try
helper.link_to customer_email, app.store_customer_path(order.customer_id)
or
helper.link_to customer_email, app.store_customer_path(store.id)
As your path says /stores/:store_id/customers/new(.:format),you just need to pass store_id for it to work.
for understanding major changes in Routes from Rails 2 to Rails 3...you must have a look at this page

Migrating Routes to Rails 4

I am about to migrate my rails 3 application to rails 4.
There are some additional routes on my ressources that make some problems.
I get an error message for this lines in my route file:
resources :teams do
...
get 'apply' => 'teams#apply_membership', as: :apply_membership
post 'apply' => 'teams#apply_membership_save', as: :apply_membership
...
This is the generated error message
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
In rails3 it was possible to define a get and a post route using the same alias and routing them to different controler methods.
Can I do this in rails4, too?
And if yes, how does it have to look like in my route file?
You can not take two route name with same name. but you have done it. so please change,
get 'apply' => 'teams#apply_membership', as: :apply_membership
post 'apply' => 'teams#apply_membership_save', as: :update_membership
Take a look here for rails routings. http://guides.rubyonrails.org/routing.html

scope in routes.rb not properly working in connection with get

In my rails 3.2.2 app I have the following in my routes.rb:
scope "abc" do
get "hello/index"
end
Which should link "/abc/hello/index" to my index-action in my hello-controller, right?
Instead, I get the error "uninitialized constant Abc"
If I change it to the following
scope "abc" do
match "hello/index", to: "hello#index", via: :get
end
it works just fine.
From my understanding of the routing engine, the two should be the same, shouldn't they?
(See e.g.: http://guides.rubyonrails.org/routing.html#http-verb-constraints )
Also, if you do a "rails g controller hello index" a route named
get "hello/index"
is autocreated suggesting that this is the standard way of doing a non-restful get route.
So why can't I scope such a route? Any ideas?
The examples are using the notation scope "/abc", maybe the initial / is required.

"No route matches" - Ruby on Rails

I am new to Ruby and Rails. This is my second application where I'm trying to implement login authentication.
Getting this error
No route matches "/user/process_login"
Here is my routes.rb
Myapp::Application.routes.draw do
get "user/login"
get "user/process_login"
end
On submit i am getting above error on login page. I think something has gone wrong in routes.rb or somewhere else.
looks like you're trying to post to that route,
try changing your routes file to this:
post "user/process_login"
You need to do this in your routes.rb
get "user/login" => "users#login"
get "user/process_login" => "users#process_login"
on a side note, I highly recommend that you use post instead of get to process the login.

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.

Resources