2011-01-16 13 views
-3

私は、ショーアクションが呼び出されたときにユーザーが自由にショップを作成し、関連するショップアイテムが表示されるアプリケーションを作成しようとしていますが、何か間違っているようです。ここでの助けに感謝します。私は以下のコードを添付しています。RORのネストされた属性

class ShopItem < ActiveRecord::Base 

    belongs_to :shop 

     def self.find_shop_items_for_sale 
     find(:all, :order => "title", :conditions => ["shop_id = ?", @shop.id]) 
     end 

    end 

    class Shop < ActiveRecord::Base 
     has_many :shop_items 
     end 


#Controllers 

     class ShopsController < ApplicationController 

     def new 
     @shop = Shop.new 
     end 

     def create 
     @shop = Shop.new(params[:shop]) 
     @shop.user_id = current_user.id 
     respond_to do |format| 
     if @shop.save 
      flash[:notice] = "Successfully created shop." 
      format.html {redirect_to(all_shops_shops_url)} 
      format.xml {render :xml => @shop, :status => :created, :location => @shop } 
     else 
      format.html {render :action => 'new'} 
      format.xml { render :xml => @shop.errors, :status => :unprocessable_entity } 
     end 
     end 
    end 



    def show 
      @shop = Shop.find(params[:id]) 
      @shop_items = ShopItem.find_shop_items_for_sale 
      @shop_cart = find_shop_cart 
      end 




     class ShopItemsController < ApplicationController 

    def user 
     @per_page ||= 5 
     @user = User.find(params[:id]) 
     @shop_items = ShopItem.find(:all, :conditions=>["user_id = ?", @user.id], :order=>"id desc") 

     end 

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

    # GET /shop_items/new 
    # GET /shop_items/new.xml 
    def new 
    @shop_item = ShopItem.new 

    @shop = Shop.find(params[:id]) 
    #@shop_items = ShopItem.paginate(:all, :condition=>["shop_id] = ?", @shop.id], :order=> "id desc", :page => params[:page],:per_page => @per_page) 
    @shop_items = ShopItem.find(:all, :conditions=>["shop_id = ?", @shop.id], :order=> "id desc") 
    @shop_item.shop_id = params[:id] 

    respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @shop_item } 
    end 
    end 

    # GET /shop_items/1/edit 
    def edit 
    @shop_item = ShopItem.find(params[:id]) 
    end 

    # POST /shop_items 
    # POST /shop_items.xml 
    def create 
    @shop_item = ShopItem.new(params[:shop_item]) 
    @shop_item.user_id = current_user.id 

    respond_to do |format| 
     if @shop_item.save 
     flash[:notice] = 'Shop item was successfully created.' 
     format.html { redirect_to(@shop_item) } 
     format.xml { render :xml => @shop_item, :status => :created, :location => @shop_item } 
     else 

     @shop = Shop.find(@shop_item.shop_id) 
     #@shop_items = ShopItem.paginate(:all, :condition =>["shop_id = ?", @shop.id], :order=> "id desc" , :page => params[:page], :per_page => @per_page) 
@shop_items = ShopItem.find(:all, :conditions =>["shop_id = ?", @shop.id], :order=> "id desc") 

     format.html { render :action => "new" } 
     format.xml { render :xml => @shop_item.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 
+2

コードブロックを意味のある塊に分けてください。読むことがかなりあります。 –

+2

あなたが「間違っている」と思われる理由についても解説してください。間違いがありますか?何か具体的なことがあなたの期待通りに起こっていないのですか? –

答えて

0

あなたがその店:has_many :shop_itemsを指定する場合は、find_shop_items_for_saleのようなアクションを指定する必要はありません。ただ、呼び出し:

@shop = Shop.find(params[:id]) 
@shop_items = @shop.shop_items 

はまた、ユーザ(アクションuser)のために、すべてのショップアイテムをretriveしようとすることは悪い考えです。代わりにwill_paginate gemをご覧ください。

あなたの質問は全く問題ではなく、特定の質問をするか、あなたの問題の説明だけを試みる方が簡単です。

+0

巨大なコードについては申し訳ありません。私の悪い! – user487429

+0

@ Ryan ..巨大なコードについては申し訳ありません。私の悪い!私の問題は、特定のショップで商品のカタログを表示しようとするときに、未知の動作エラーが発生することです。私は当初はそれが私のルートだと思っていましたが、確かではありません@klew ...私はあなたの提案を試してみます。 – user487429

+0

このようなエラーが発生した場合は、ルートに問題があります。あなたの経路を質問してください。 – klew

関連する問題