Ruby on rails ROR中的嵌套属性

Ruby on rails ROR中的嵌套属性,ruby-on-rails,Ruby On Rails,我正在尝试创建一个应用程序,用户可以在其中自由创建店铺,并且在调用show操作时显示特定店铺的关联店铺项目,但我似乎做错了什么。这里的任何帮助都将不胜感激。我在下面附上了我代码的截图 class ShopItem < ActiveRecord::Base belongs_to :shop def self.find_shop_items_for_sale find(:all, :order => "title", :conditions

我正在尝试创建一个应用程序,用户可以在其中自由创建店铺,并且在调用show操作时显示特定店铺的关联店铺项目,但我似乎做错了什么。这里的任何帮助都将不胜感激。我在下面附上了我代码的截图

    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
class ShopItem“title”,:conditions=>[“shop\u id=?”,@shop.id])
结束
结束
类Shop@shop,:status=>:created,:location=>@shop}
其他的
format.html{render:action=>new'}
format.xml{render:xml=>@shop.errors,:status=>:unprocessable_entity}
结束
结束
结束
def秀
@shop=shop.find(参数[:id])
@shop\u items=ShopItem.find\u shop\u items\u待售
@购物车=查找购物车
结束
类ShopItemsController[“user_id=?”,@user.id],:order=>“id desc”)
结束
def秀
@shop\u item=ShopItem.find(参数[:id])
@shop=@shop\u item.shop
回应待办事项|格式|
format.html#show.html.erb
format.xml{render:xml=>@shop_item}
结束
结束
#获取/购买商品/新
#获取/shop_items/new.xml
def新
@shop\u item=ShopItem.new
@shop=shop.find(参数[:id])
#@shop\u items=ShopItem.paginate(:all,:condition=>[“shop\u id]=?”,@shop.id],:order=>“id desc”,:page=>参数[:page],:per\u page=>@per\u page)
@shop_items=ShopItem.find(:all,:conditions=>[“shop_id=?”,@shop.id],:order=>“id desc”)
@shop_item.shop_id=参数[:id]
回应待办事项|格式|
format.html#new.html.erb
format.xml{render:xml=>@shop_item}
结束
结束
#获取/购买商品/1/编辑
定义编辑
@shop\u item=ShopItem.find(参数[:id])
结束
#邮寄/购物物品
#POST/shop_items.xml
def创建
@shop\u item=ShopItem.new(参数[:shop\u item])
@shop_item.user_id=当前_user.id
回应待办事项|格式|
如果@shop\u item.save
flash[:notice]=“店铺商品已成功创建。”
format.html{重定向到(@shop_item)}
format.xml{render:xml=>@shop\u item,:status=>:created,:location=>@shop\u item}
其他的
@shop=shop.find(@shop\u item.shop\u id)
#@shop\u items=ShopItem.paginate(:all,:condition=>[“shop\u id=?”,@shop.id],:order=>“id desc”,:page=>参数[:page],:per\u page=>@per\u 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}
结束
结束
结束

如果指定商店
:有很多:商店物品
,则不必指定像
查找商店物品以供出售之类的操作
。只要打电话:

 @shop = Shop.find(params[:id])
 @shop_items = @shop.shop_items
另外,尝试为用户检索所有商店商品(操作
user
)也是一个坏主意。相反,看看宝石吧


另一方面,你的问题根本不是问题,如果你问一个特定的问题,或者只是试着解释一下你有什么问题,那就容易多了。

请将代码块分成有意义的块。有很多东西要读。请解释一下为什么你认为自己“做错了”你有错误吗?是不是有些特定的事情没有按你预期的方式发生?@Ryan.我对大量的代码感到抱歉。我的错!我这里的问题是,当我试图显示一个特定商店的商品目录时,我得到一个未知的操作错误。我最初以为这是我的路线,但不确定@klew…我会试试你的建议。如果你遇到这样的错误,那就是路线问题。请回答您的路线问题。@klew..谢谢,问题是我的路线。将其更改为:-map.resources:shop\u items map.resources:shops,:has\u many=>[:shop\u items],并且一切似乎都正常工作。非常感谢。