I've just upgraded my rails app from rails 3.2.21 to 4.2.0,
After fixed some error I have somes duplicates routes problems.
`add_route': Invalid route name, already in use: 'contact'
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....
but ... yes, I have some route pointing on the same controller action, but this is the way I want it. How can I tell rails 4 to no take care of this, or theres is a way to not touch my routes?
thanls !
The router in rails 4.0 detects whether a route name has been used before whereas previously it would've just overwritten them. This is even listed on github.
Fix:
Just make sure you are not using same helper method name for two routes in as: option
Related
I've been trying to set up Forem (a Rails 4 forum engine) using a guide and original docs.
Most things work, but I'm getting route errors. In my application.erb I have this route in a link_to:
topic_path(u)
The guide recommends that I preface this with my application name so my routes won't conflict with Forem's routes, so I did that as so:
H2le.topic_path(u)
(H2le is the application name set in application.rb)
However, this errors out:
"undefined method `topic_path' for H2le:Module"
Am I not setting the application name properly?
The problem was my being a Ruby newb and the guide I was following perhaps not being super explicit. It recommended to namespace the links like:
main_app.path
And I interpreted main_app to be a placeholder for my app name. Well, wrong. main_app is a built-in helper function, so it should literally just say main_app. I fixed this, and everything worked.
Just wanted to know, what does this line mean in the routes.rb file:
AppName::Application.routes.draw do
Please explain. I am new to Rails.
Have a read through this page.
Basically, within the block passed to Application.routes.draw (which is just a call to a method defined in ActionDispatch::Routing module within the Rails core framework), you define all the URLs/Paths that you want your Rails application to respond to.
You can see all these route definitions, by running:
rake routes
in your terminal.
It is the main routes file which defines the root and other paths for the link.
It is used as suppose you want to change your index page from default ruby on rails to your index page you make changes to file and add
root to: "controllername#index"
This file is also used to add the model to the application
resources: "model_name"
Apart from this you can also define links in your rails application
get 'courses/index'
So going from courses controller to view of the index.
I'm having trouble creating routes for my Rails app and google is returning mostly results for rails 2-3 from 5 or more years ago.
I'm trying to create a "show" route without using resources, as I need multiple show views.
If I do the following :
get "/parameters/:id" => "parameters#show"
Then if I directly type in the path, like "parameters/12" it will work, but when I run rake routes it doesn't show a path next to the route. And I can't seem to get a path to work for it.
I'm sure I'm forgetting something really obvious here, but I've been at this problem for a few hours and haven't found a solution so I would really appreciate some help.
Because you are defining a custom route, Rails doesn't know how to generate the _url and _path named routes for you automatically.
In order to get the named routes generated, you need to help Rails by specifying the as argument
get '/parameters/:id', to: 'parameters#show', as: 'parameters_show'
Why in Rails 3 do you have to uncomment match ':controller(/:action(/:id(.:format)))' (as seen in this Hello World article) to make the index method of the hello controller be called when you go to http://localhost:3000/hello? Can somebody please explain why we have to do this in Rails 3 but not Rails 2, and is this a normal thing for Rails 3 or is it some kind of hack?
That particular match is sort of a catch-all for any requests that haven't already been defined.
Ideally you should be using Resource Routing, but that matcher still exists as legacy support.
It's commented out by default because Rails assumes that if a user attempts to access a route that you didn't explicitly define, it should cause a 404 error instead of a 500 error, which is what would happen if I tried to access http://localhost:3000/hello with that matcher enabled, because there's no 'hello' controller.
I know this is a minor issue, but why, if you use scaffolding in RoR, can you use lines like 'new_model name here_path' in link tags, but without using scaffolding, I get a NameError? For example, I have a simple address book app that uses basic CRUD operations. I am a RoR beginner but wanted to build an app without scaffolding and these kind of things don't seem to work. I compared my config/routes.rb and app/helpers/* with those in a scaffolded app and they are no different. What am I missing?
One way to check your routes and paths is to run:
rake routes
It outputs all your routes and paths.
Scaffolding sets up resource routes in the routes.rb file. The resource routes are what give you the path and url helpers. When you don't use scaffolding the routes aren't added, you must do it by hand.
Resource Routes can be added like so:
map.resources :models
where :models is the plural name of one of your models.