Currently, I have:
map.resources :users do |user|
user.resources :blogs
end
How do I turn this into a match or resources in rails 3+?
My attempt would be:
resources :users do
user.resources :blogs
end
I got most of my information on route changes from here.
resources :users do
resources :blogs
end
Since you do not have |user| in the Rails 3 block, you cannot refer to it. In addition, you shouldn't need to:
resources :users do
resources :blogs
end
Rails routing guide explains this. You should be able to use
resources :users do
resources :blogs
end
Related
I'm a beginner in Ruby on Rails and I have a problem. I'm trying to add comments to my app. Everything is working, but when I added this code to routes.rb
resources :galleries do
resources :comments, module: :galleries
end
resources :articles do
resources :comments, module: :articles
end
I can't update any gallery or article. My whole routes.rb:
Rails.application.routes.draw do
devise_for :users
resources :galleries do
resources :comments, module: :galleries
end
resources :articles do
resources :comments, module: :articles
end
match ':controller(/:action(/:id))(.:format)', via: [:post, :get]
root 'public#index'
end
It looks like the request (articles/12?page_id=4) is falling through to the match statement and not getting picked up by your resources. Your match statement:
match ':controller(/:action(/:id))(.:format)', via: [:post, :get]
is matching "articles" as the controller, and looking for "12" as the action and it clearly can't find it.
I would scrap the match statement and go strictly with named routes or resourceful routing permanently, or at least while debugging. Next, run
rake routes
and check the results. From your routes.rb I am guessing you will have a line like this:
article GET /articles/:id(.:format) articles#show
If that is indeed the case, make sure you have the show() method defined in the articles controller, and verify that the logs are showing a GET request for it.
routes.rb
Rails.application.routes.draw do
root to: 'visitors#index'
resources :states do
resources :cities do
get 'listings'
end
end
end
I am looking to have my GET URL set up like:
../state.id/city.id/listings.id
I am using friendly_id so the urls will read like:
../OR/Portland/2011-ford-truck
Listing is it's own model (resource) too in this case. You will also need a resources for listing. If it only has a show action, you can limit it like this:
resources :states do
resources :cities do
resources :listings, only: [:show]
end
end
I've been deep nesting my resources and now discover that this should be avoided. Shallow nesting seems to be the answer but I'm struggling on how to properly correct the second level deep nesting. Let me show you my routes file as I think that will make it clearer(I'm using Rails 4):
OLD:
resources :members do
resources :emails
resources :events do
resources :items
end
end
NEW so far:
resources :members, shallow: true do
resources :emails
resources :events
end
My issue is I don't understand where to put the :items now? I thought it should be nested under the :events for the :index, :new, and :create actions?
What am I missing, I'm sure its something silly that I'm just not getting yet, please excuse my ignorance and I think you in advance for any help!!!
Mark
All you need to do is, update the routes as below:
resources :members, shallow: true do
resources :emails
resources :events, shallow: true do
resources :items
end
end
This will create shallow routes for items. Only for :index, :new, and :create actions you would get nested routes prefixed with /events/:event_id.
I have 3 levels of nesting.
routes.rb looks like this
resources :clients do
resources :departments do
resources :tasks
end
end
I would like to create a custom path that looks like this
/clients/:client_id/departments/:department_id/tasks/data
I have tried adding the following
resources :clients do
resources :departments do
resources :tasks
member do
get "data"
end
end
end
This creates the route
/clients/:client_id/departments/:department_id/tasks/:task_id/data
How would I remove the :task_id part the path?
A member route acts on a member, that's why it requires an id. A collection acts on a collection and so doesn't require an id.
resources :clients do
resources :departments do
resources :tasks do
collection do
get "data"
end
end
end
end
You should use
resources :clients do
resources :departments do
resources :tasks
get "data", :on => :collection
end
end
I have several namespaced routes in an app. Here's a sample
namespace "battles" do
resources :teams do
resources :comments, :module => "comments", :controller=>'comments'
end
end
My problem is that all my resources with comments route to the comments/comments controller, but because :teams is in the battles namespace, then the app tries to route to battles/comments/comments
Is there a way to specify that the nested comment resource should route to the comments/comments controller, not the battle/comments/comments controller.
Try
scope :module => "battles" do
resources :teams do
resources :comments, :module => "comments", :controller=>'comments'
end
end
I gave up and just created a Battle::Comments controller. It results in code duplication so it's not ideal.