Rails 3.1 Has_One Nested Resource: Routing Not Generating "All" Paths

Rails 3.1 has_one nested resource: routing not generating all paths

I dont really know why it's not displayed when you do rake routes but did you try in your code to do supplier_presentation_path(@supplier)? It should work based on your routes.

Rails 4 - routes for nested resource

I'd suggest only nesting some profile actions, like this:

resources :users do
resources :profiles, only: [:new, :create]
end

and then using top-level routes for the other actions:

resources :profiles, only: [:show, :edit, :update, :destroy]

If you have the current_user, you can pass just that user's profile to those actions instead of both. Of course, your path names will change and you'll have to change those in your views. (Something like Hi <%= link_to(current_user.first_name.titlecase, profile_path(@profile)) %>).

I'm curious, though, why have what appears to be a link to the user lead to the user's profile? Why not link to Users#show, using user_path(@current_user) or whatever your equivalent route is, and then just rendering the profile on the Users#show view?

Rails nested routes not filtering

I guess you should simply edit your controller's action:

@products = ProductType.find(params[:product_type_id]).products

As for your second question, simply add the mere:

resources :products

To your routes.rb. In routing the rule is first match first served. But since the nested and unnested url won't match each other, you're fine.

Rails 3 - Nested resources and polymorphic paths: OK to two levels, but break at three

Your code example that limited your nesting to 2 levels is quite near the answer. To avoid duplicate routes for fams->kids and kids, you can use the :only option with a blank array so that the 1st-level kids will not generate routes except in the context of kids->pictures, like so:

resources :posts do
resources :pictures
end

resources :fams do
resources :pictures
resources :kids
end

resources :kids, only: [] do # this will not generate kids routes
resources :pictures
end

For the above code, you can use the following to construct your polymorphic edit url:

polymorphic_url([fam, picture], action: :edit) # using Ruby 1.9 hash syntax
polymorphic_url([kid, picture], action: :edit)

Rails nested resources and routing - how to break up controllers?

All you are doing with nested resources is changing the routing URL. Only thing you would have to do is make sure you are passing the proper id (in your case post)to the tag controller. Most common error is the Can't Find *** ID.

If you don't nest a profile route into a user route it would look like this

domain.com/user/1

domain.com/profile/2

When you nest the routes it would be

domain.com/user/1/profile/2

That is all that it is doing and nothing else. You don't need additional controllers. Doing nested routing is just for looks. allowing your user to follow the association. The most important thing about nesting routes is that you make sure you make the link_to's to the right path.

When not nested: it would be

 user_path

and

 profile_path

when it is nested you would need to use

user_profile_path

rake routes is your friend to find out how the routes have changed.

Hope it helps.

Ruby on Rails nested resource routing error

I will give it a try, after reading Fernandez: The rails 3 way about redirect_to.

When you look at the output from rake routes, you have the output:

user_routes GET /users/:user_id/routes(.:format) {:action =>"index", :controller=>"routes"}

The methods you may use to that route are:

  • user_routes_url: Full URL (with protocol and everything)
  • user_routes_path: Relative URL to the host

But your user_routes tells you another thing: the URL has to contain a user_id, and this user_id has to come from somewhere. So to call the different url and path methods, you have to look at the arguments:

  • users_path: no argument, shows all users
  • user_path(@user): one argument, because the information about the user is needed. Could be the user, or the user_id
  • `user_routes_path(@user): needs the user, so that all routes (index view) for one user could be shown.

So include in you source code in the controller:

...
else
sign_in user
redirect_to user_routes_path(user)
end
...

I don't understand the error message you have appended, but I think you should first correct the path call.

How to list ALL in index even with nested resources in rails 3.1.0?

You have to change the routes to:

resources :quotes, only: [:index]
resources :rfq do
resources :quotes
end

And handle the fact you won't have a :rfq_id in that situation. You can use a before filter:

before_filter :load_rfq

def load_rfq
@rfq = Rfq.find(params[:rfq_id]) if params[:rfq_id].present?
end

and then

def index
@quotes = @rfq.present? ? @rfq.quotes : Quote.all
end


Related Topics



Leave a reply



Submit