私は、ショーアクションが呼び出されたときにユーザーが自由にショップを作成し、関連するショップアイテムが表示されるアプリケーションを作成しようとしていますが、何か間違っているようです。ここでの助けに感謝します。私は以下のコードを添付しています。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
コードブロックを意味のある塊に分けてください。読むことがかなりあります。 –
あなたが「間違っている」と思われる理由についても解説してください。間違いがありますか?何か具体的なことがあなたの期待通りに起こっていないのですか? –