¿Cómo enumerar TODO en el índice incluso con recursos anidados en rieles 3.1.0?

There are rfqs and quotes controllers in our rails 3.1.0 app. Rfq has many quotes and quote belongs_to a rfq. In routes.rb, it is:

resources :rfq do
  resources :quotes
end

However we want to list all quotes in index. But the predefined route only allows to display all quotes for a specific rfq, as displayed for rake routes:

 rfq_quotes GET    /rfqs/:rfq_id/quotes(.:format)                                   {:action=>"index", :controller=>"quotes"}

What's a simple and clean way to add an route so we can list ALL quotes in index, with show and edit for each of quote listed? Thanks so much.

preguntado el 31 de enero de 12 a las 16:01

2 Respuestas

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

y entonces

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

Respondido el 31 de enero de 12 a las 21:01

it looks very clean. In index, we need to find the rfq_id for each show and edit (nested resources). What's the purpose for before filter? Thanks. - user938363

One question, Will the variable @rfq returned from load_rfq be available for access by quote index view and all other actions (such as new, update etc.) in quote controller? - user938363

Then, there is no need to have @rfq = Rfq.find(params[:rfq_id]) in other nested actions in quotes controller. thanks. - user938363

This is just a suggestion, not even sure if it will work. Perhaps if you make another index in the rfq thing and set @rfqs = @Rfqs.find(:all) and then in the view you could loop through each rfq in rfqs and each quote associated with each rfq. (@rfqs.each.quotes.find(:all).each do |quote|)

I'm not really sure if that makes sense.

Respondido el 31 de enero de 12 a las 20:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.