Nested attributes in ROR
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.pagina开发者_JAVA百科te(: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
If you specify that shop :has_many :shop_items
, then you don't have to specify actions like find_shop_items_for_sale
. Just call:
@shop = Shop.find(params[:id])
@shop_items = @shop.shop_items
Also trying to retrive all shop items for user (action user
) is a bad idea. Instead take a look on will_paginate gem.
On the other side, your question isn't question at all and it would be much easier if you will ask a specific question, or just try to explain what problems you have.
精彩评论