views:

104

answers:

2

Hey guys,

I'm pretty new to rails, so I apologize if this question has an obvious answer.

I have multiple controllers with records stored based on relations

for example, /books/1/titles/4

meaning the the title with ID 4, is only related to book with ID 1.

in an action such as destroy, how can I use redirect_to to go to /books/1/titles?

format.html { redirect_to :controller => 'books/:id/titles' , :action => 'show', :id => @book}

didn't work

Thanks in advance!

A: 

you'll want to use restful routes from here on out, and you are describing nested resources. topic is big. However, i suggest you see what happens when you

script/generate scaffold Book
script/generate scaffold Title

In there you will see how they destroy and redirect. You will learn a bunch just from that one command. then you can learn about associations between Title and Book (as in a title belongs to book and books has_many titles) and then finally adding your nested_route by doing:

map.resources :books, :has_many => :titles

oh and the stuff you got there won't work, btw.

pjammer
Hi pjammer, thats actually how those were created. and in routes I've already set that up. currently it works. The issue is making it redirect to the URL in the respond, during an action such as destroy
Elliot
A: 

Run

rake routes

to check your routes.

I think you'll want something like

book_titles_url(params[:book_id])
Andy Gaskell
Hi pubb! Thanks - that helped a lot, what should I use to put in the book ID?
Elliot
It's hard to say without seeing your controller code. You might be able to put params[:book_id] - I'm going to edit my answer to explain a different way of doing it.
Andy Gaskell
nevermind, I figured it out! :)
Elliot
Great - here's a great guide to rails routing if you're interested. http://guides.rubyonrails.org/routing.html
Andy Gaskell