Working with nested models in ROR

Posted by user487429 on Stack Overflow See other posts from Stack Overflow or by user487429
Published on 2011-01-16T21:49:54Z Indexed on 2011/01/16 21:53 UTC
Read the original article Hit count: 195

Filed under:

Hi there,

I'm trying to create an application where users can freely create shops and associated shop item for a specific shop is displayed when a show action is called but I seem to be doing something wrong. Any help here will be appreciated. I have attached shots of my code below.

    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

© Stack Overflow or respective owner

Related posts about ruby-on-rails