我对Ruby / Rails比较陌生,搞砸了case/when
条款的结构,或者我不理解路线......
浏览器出错只是:No route matches {:action=>"show", :controller=>"masters"}
但很清楚地看rake routes
我看到了:
master GET /masters/:id(.:format) {:action=>"show", :controller=>"masters"}
breeder GET /breeders/:id(.:format) {:action=>"show", :controller=>"breeders"}
并且两个(masters
& breeders
)控制器都有show
个方法......
哪里是我的傻瓜?
#app/controllers/dogs_controller.rb
class DogsController < ApplicationController
def create
@parent = parent_object
@dog = @parent.dogs.create(params[:dog])
puts parent_path
redirect_to parent_path(@parent)
end
def destroy
@parent = parent_object
@dog = @parent.dogs.find(params[:id])
@dog.destroy
redirect_to parent_path(@parent)
end
private
def parent_object
case
when params[:master_id] then Master.find(params[:master_id]) && parent_path = master_path
when params[:breeder_id] then Breeder.find(params[:breeder_id]) && parent_path = breeder_path
end
end
end
修改:添加了MastersController
#app/controllers/masters_controller.rb
class MastersController < ApplicationController
respond_to :html, :json
def index
respond_with(@masters = Master.all)
end
def show
respond_with(@master = Master.find(params[:id]))
end
def new
respond_with(@master = Master.new)
end
def edit
respond_with(@master = Master.find(params[:id]))
end
def create
@master = Master.new(params[:master])
flash[:notice] = 'Master was successfully created.' if @master.save
respond_with(@master)
end
def update
@master = Master.find(params[:id])
flash[:notice] = 'Master has been updated.' if @master.update_attributes(params[:master])
respond_with(@master)
end
def destroy
@master = Master.find(params[:id])
flash[:notice] = 'Successfully deleted master.' if @master.destroy
respond_with(@master)
end
end
答案 0 :(得分:1)
哈!找到了!我的重定向错误......应该是redirect_to @parent
,而不是像我一样构建一些parent_path(@parent)
...