开发者

Error in my Ruby code...or Rails route?

开发者 https://www.devze.com 2023-01-29 06:54 出处:网络
I\'m relatively new to Ruby/Rails and have screwed up the structure of a case/when clause or perhaps I\'m not understanding the routes...

I'm relatively new to Ruby/Rails and have screwed up the structure of a case/when clause or perhaps I'm not understanding the routes...

Error in browser simply: No route matches {:action=>"show", :controller=>"masters"}

but clearly looking at rake routes I see:

 master GET   /masters/:id(.:format)    {:action=>"show", :controller=>"masters"}
 breeder GET  /breeders/:id(.:format)   {:action=>"show", :controller=>"breeders"}

and both (masters & breeders) controllers have working show methods...

where's my goof?

#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

Edit: Added 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开发者_StackOverflow社区(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


Ha! Found it! My redirects were wrong... should have been redirect_to @parent instead of trying to construct some parent_path(@parent) as I was...

0

精彩评论

暂无评论...
验证码 换一张
取 消