I?m having a strange issue with nested routes though, and was wondering if anyone could shed some light on this? If I have: map.resources :users do |users| users.resources :items end When I try to call <%= link_to_remote ''Delete'', :url => item_url(@item), :with => "''_method=delete''", :confirm => ''Are you sure?'' %>>From inside the user urlhttp://localhost:3001/users/1/items Or edit_item_url(@item.id) it doesn?t always render the correct URL, and sometimes I get a nil error trying to render the url. Not sure what I?m missing here, but any help is greatly appreciated. Thanks. I''ve freezed Edge Rails btw. -- Posted via http://www.ruby-forum.com/.
Still getting to grips with this myself, but I *think* you still need to explicitly declare the items routes as well, so: map.resources :items Rick''s routing_navigator helps with showing what routes are available and that''s help me some, although as I said, still getting to grips with it myself... HTH Guest wrote:> I?m having a strange issue with nested routes though, and was wondering > if anyone could shed some light on this? > > If I have: > > map.resources :users do |users| > users.resources :items > end > > When I try to call > > <%= link_to_remote ''Delete'', :url => item_url(@item), :with => > "''_method=delete''", :confirm => ''Are you sure?'' %> > > >From inside the user url > > http://localhost:3001/users/1/items > > Or edit_item_url(@item.id) it doesn?t always render the correct URL, and > sometimes I get a nil error trying to render the url. > > Not sure what I?m missing here, but any help is greatly appreciated. > Thanks. > > I''ve freezed Edge Rails btw. > >
Guest wrote:> I?m having a strange issue with nested routes though, and was wondering > if anyone could shed some light on this? > > If I have: > > map.resources :users do |users| > users.resources :items > end > > When I try to call > > <%= link_to_remote ''Delete'', :url => item_url(@item), :with => > "''_method=delete''", :confirm => ''Are you sure?'' %> > > From inside the user url > > http://localhost:3001/users/1/items > > Or edit_item_url(@item.id) it doesn?t always render the correct URL, and > sometimes I get a nil error trying to render the url.When you use the x_url or x_path helpers you can pass either a hash of options or positional params. If you go with positional params, they have to correspond to the order of params in the route. In your example, the route URL would look like /users/:user_id/items/:id Therefore you need to say item_url(@user, @item) or items_url(@user) That should fix it. You might also consider using item_path instead of item_url. It''s the same without the protocol, host and port bits of the URL, which are generally redundant for that usage. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
try <%= link_to_remote ''Delete'', :url => item_url(@item.user, @item), :with => "''_method=delete''", :confirm => ''Are you sure?'' %> (asuming Item has a user association) nesting broke my neck yesterday, too, because i didn''t think of the :with option and tried <%= link_to_remote ''delete'', :url => item_url(@item.user, @item, :_method => ''delete'') %> but, as you can think that doesnt work at all 2006/8/5, Guest <guest@guest.com>:> > I''m having a strange issue with nested routes though, and was wondering > if anyone could shed some light on this? > > If I have: > > map.resources :users do |users| > users.resources :items > end > > When I try to call > > <%= link_to_remote ''Delete'', :url => item_url(@item), :with => > "''_method=delete''", :confirm => ''Are you sure?'' %> > > >From inside the user url > > http://localhost:3001/users/1/items > > Or edit_item_url(@item.id) it doesn''t always render the correct URL, and > sometimes I get a nil error trying to render the url. > > Not sure what I''m missing here, but any help is greatly appreciated. > Thanks. > > I''ve freezed Edge Rails btw. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Michael Siebert <info@siebert-wd.de> www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060805/3b68f092/attachment.html
This is all pretty helpful. Thanks guys. Another thing, is there a way to get the user from the url? I''m going to complicate this even more now.. Items can belong to users, and categories. So... I have this in my routes file: map.resources :categories do |categories| categories.resources :items end map.resources :users do |users| users.resources :items end Now, if I''m at users/1/items I need a way of being able to have that user id. However if I''m in categories/1/items I need the category ID. Do I have to do an explicit find for each in the index action for items like: @user = User.find(params[:user_id]) and @categroy = Category.find(prams[:category_id]) ? There has to be a better way than this. Thanks again. Michael Siebert wrote:> try > > <%= link_to_remote ''Delete'', :url => item_url(@item.user, @item), :with > => > "''_method=delete''", :confirm => ''Are you sure?'' %> > > (asuming Item has a user association) > nesting broke my neck yesterday, too, because i didn''t think of the > :with > option and tried > <%= link_to_remote ''delete'', :url => item_url(@item.user, @item, > :_method => > ''delete'') %> > but, as you can think that doesnt work at all > > 2006/8/5, Guest <guest@guest.com>: >> When I try to call >> >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > > -- > Michael Siebert <info@siebert-wd.de> > > www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium-- Posted via http://www.ruby-forum.com/.
On Aug 6, 2006, at 10:47 AM, Guest wrote:> This is all pretty helpful. Thanks guys. Another thing, is there a way > to get the user from the url? > > I''m going to complicate this even more now.. > > Items can belong to users, and categories. So... > > I have this in my routes file: > > map.resources :categories do |categories| > categories.resources :items > end > > map.resources :users do |users| > users.resources :items > end > > Now, if I''m at users/1/items I need a way of being able to have that > user id. However if I''m in categories/1/items I need the category > ID. Do > I have to do an explicit find for each in the index action for items > like: > > @user = User.find(params[:user_id]) > > and > > @categroy = Category.find(prams[:category_id]) > > ? > > There has to be a better way than this.I think this is pretty nice: before_filter :find_category before_filter :find_item, :except => [:index, :new, :create] private def find_category @category = Category.find(params[:category_id]) end def find_item @item = @category.items.find(params[:id]) end Is it too much code? or too repetitive? Could dry it out for resources which follow the /categories/1/items/2 convention - # in a base class before_filter :find_resources # imagine resource_chain is extracted from the route as [[''categories'', 1], [''items'', 2]] # set @category = Category.find(1), @item = @category.items.find (2), etc. def find_resources name, id = resource_chain.shift root = name.classify.constantize.find(id) instance_variable_set "@#{name.singularize}", root resource_chain.inject(root) do |last, (associate, id)| instance_variable_set "@#{associate.singularize}", last.send (associate).find(id) end end Then your resource controllers can drop the find_* before_filters. jeremy