views:

350

answers:

3

I got a config/routes.rb file like this:

  map.resources :categories, :shallow => true do |cat|
    cat.resources :entries,  :member => {:yes => :post, :no => :post }
  end

My goal is use yes and no as buttons that modify my entries (it is like a game, and yes and no are answers to the entries).

I would like that when I click on yes or no I will go back to category/:category_id/entries , because there is where I have the pretty layout, etc.

The problem is that I got to the yes method from entries/:entry_id So I lost the category id this entry is in and I dont know how to get a link to the category.

I guess I can try without using :shallow parameter so I got the full path but I am probably going to go further in the hierarqui (entries may have comments) and I want to learn with this simple problem how to manage this situation.

A: 

In your button, you can specify the category like so:

link_to "Yes", yes_entry([@category, Entry.new]), :method => :post

I'm not sure if it works though, otherwise you could try:

link_to "Yes", yes_entry(Entry.new, :category_id => @category), :method => :post
Jaryl
A: 

Have you tried resource_controller? I didn't work with :shallow option (and this solution is probably without it), but resource_controller supports some kind of polymorphic resources (you can have url's like: /entries, /category/3/entries, /whatever/12/entries etc). Then you can generate url to entries with this:

<%= link_to 'Entries', collection_path %>

But in case of your 'yes' and 'no' methods, you only need to add:

redirect_to collection_path

in response to 'yes' and 'no' actions.

klew
+2  A: 

If your entries are subresources of your categories, then there is probably a parent-child relationship between the models, right? If so, then you can use:

redirect_to category_entries_path(@entry.category)

in your yes and no methods.

Andrew Watt
It worked perfectly.I am still overwhelmed by the amount of things rails do in programmers behalf. Didn't know that belongs_to in the model would create @entry.category Thanks a lot.
Jordi