在routes.rb中引用了一个嵌套的资源编辑路径:生成多个模型
resources :cars do
resources :reviews
end
resources :motorcycles do
resources :reviews
end
在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
生成的“秀”链接,评论简直是(本作品):
= link_to "Show", [@parent, @review]
同样我想引用审查的通用编辑路径,像(这是行不通的):
= link_to "Edit", [@parent, @review], :action => 'edit'
有没有人知道这是可能的,或者如果不是,这可能如何实现?
事实证明,我正在寻找能与URL帮手“edit_polymorphic_path”中找到了答案(见:HTTP ://rubydoc.info/docs/rails/3.0.0/ActionDispatch/Routing/PolymorphicRoutes)。为了得到我在上面尝试的链接,我可以用下面的代码完成这个工作:edit_polymorphic_path([@ parent,@review]) – 2011-05-31 00:56:41