Routing resources/paths with :path_prefix and :name_prefix - ruby-on-rails

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.

Related

Rails2 to Rails3 routing conversion

Routing makes zero sense to me, is there an easy way to convert this:
#map.resources :bicycles, :controller => 'store/bicycles', :path_prefix => 'store'
thanks in advance
The controller option is also available in Rails 3 and the path_prefix can be achieved with scope. The below is the conversion of the route to Rails 3
scope 'store' do
resources :bicycles, :controller => 'store/bicycles'
end

What is the error while migration in 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

How can I rename a Rails controller with a route?

I have a controller in a Rails 3 app named "my_store." I would like to be able to use this controller as is, except replacing "my_store" in all the URL's with another name. I do not want to rename the controller file, and all the references to it. Is there a clean way to do this with just a routing statement?
If you use RESTful routes:
resources :another_name, :controller => "my_store"
Otherwise:
match "another_name" => "my_store"
If your routes are RESTful, this is pretty easy.
resources :photos, :controller => "images"
You can see how to do this and other helpful Rails routing information in the Rails routing guide.
Update, the other guys are correct, to replace all references you would change the resources name and corresponding controller in routes.rb! My answer is only good to set a specific route.
Yup, you would do this in your routes.rb using the :as option to specify
example:
match 'exit' => 'sessions#destroy', :as => :logout
source

Getting no route matches on the show action

I'm trying to use the path from the following route, here is what it's like in rake routes
chapter GET /chapters/:id(.:format) {:action=>"show", :controller=>"chapters"}
chapter_path creates a link to /chapters/x which is correct but I get the routing error when trying to access it.
No route matches {:controller=>"chapters"}`
this is my routes (I am using shallow routing to create a books_chapters and book_chapters_new paths.
resources :books do
resources :chapters, :shallow => true
end
when I test the route with rake routes, I get books_chapters, books_chapters_new, chapters and books, so I don't know what's wrong.
when i remove :shallow => true, i can access /books/1/chapters/6 but I just want it to be /chapters/6
this is what my terminal looks like
so /chapters/id and /chapters/id/edit should be working fine.
I've restarted the server with touch tmp/restart.txt and ran rails s to see if the routes worked there too and rake routes is giving me acceptable routes, but they don't work for me.
Are you supplying the parameter for the path helper, something like
chapter_path(#chapter)
I couldn't figure out how to get :shallow routes to work, and there isn't an example on how to use :shallow in the rails guide so, instead I have to just use nested routes like so
resources :books do
resources :chapters
end
now this means the something like chapters_url or chapters_path won't work.
So I have to do something like this everywhere
book_chapter_url(#chapter.book, #chapter)
or
edit_book_chapter_path(#chapter.book, #chapter)
It works but there's a bit of code smell because I use #chapter twice and the whole url should be able to resolve just through the chapter id instead.

Routing: prefix item name before controller resources

I am trying to get the following routes to work in my Rails 3 app.
Scenario:
I have the following controllers in my app:
Practices
Doctors
Patients
Prescriptions
Putting up :resources for each of them in routes.rb gives me routes from
example.com/practices
example.com/doctors/1/edit etc
What I'd like to have however is the following resourceful routes, for example:
example.com/james_practice/docs that translates to the doctors controller
example.com/james_practice/awesome_prescriptions that routes to the prescriptions controller etc. etc.
both of these giving me access to :practice name and furthermore route the correct controller with all the helpers like edit_docs_path(doctor) etc.
How would I go about this? I've used
resources :prescriptions, :path => "/:practice/awesome_prescriptions"
but although it showed the correct routes in "rake routes", it still wouldn't work as expected.
I think this is the route you're looking for:
scope :path => ":practice" do
resources :docs, :controller => "doctors"
resources :awesome_prescriptions, :controller => "prescriptions"
end
By the way, you didn't give me the example of Patients, so I didn't put it there.
map.resources is just a pattern, more like a shortcut. If you want to have URLs like in your examples you should use "named routes". A good screencast for Rails 2.x: http://railscasts.com/episodes/34-named-routes
Edit: Read section 3.2 in Rails Tutorial: http://guides.rubyonrails.org/routing.html
p.s. Also you'll face a case where people use "." in their names and it'll cause problems.
Here's my route for "tags"
map.resources :tags, :requirements => { :id => %r([^/;,?]+) }

Resources