Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating the edit path for a nested resource referenced by multiple models

In routes.rb:

resources :cars do
  resources :reviews
end

resources :motorcycles do
  resources :reviews
end

In ReviewsController:

before_filter :find_parent

def show
  @review = Review.find(params[:id])
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @review }
  end
end

def edit
  @review = Review.find(params[:id])
end

# ...
def find_parent
  @parent = nil
  if params[:car_id]
    @parent = Car.find(params[:car_id])
  elsif params[:motorcycle_id]
    @parent = Motorcycle.find(params[:motorcycle_id])
  end
end

Generating the "show" link for a Review is simply (this works):

= link_to "Show", [@parent, @review]

Similarly I would like to reference a generic edit path for a Review, something like (this does not work):

= link_to "Edit", [@parent, @review], :action => 'edit'

Does anyone know if this is possible or, if not, how this might be accomplished?

like image 584
the_real_one Avatar asked May 30 '11 23:05

the_real_one


1 Answers

link_to 'Edit Review', [:edit, @parent, @review]
like image 188
Khoa Nguyen Avatar answered Oct 08 '22 00:10

Khoa Nguyen