私はレールが初めてで、独自の簡単な投票アプリケーションを作成しようとしています。私は2つのモデルを持っています:
class Product < ActiveRecord::Base
attr_accessible :description, :title, :photo
has_many :votes
has_attached_file :photo, :styles => { :medium => "300x300" }
before_save { |product| product.title = title.titlecase }
validates :title, presence: true, uniqueness: { case_sensitive: false }
validates :photo, :attachment_presence => true
end
class Vote < ActiveRecord::Base
belongs_to :product
attr_accessible :user_id
end
ここに製品コントローラがあります
class ProductsController < ApplicationController
http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index]
def index
@products = Product.all
end
def indexprv
@products = Product.all
end
def show
@product = Product.find(params[:id])
end
def edit
@product = Product.find(params[:id])
end
def new
@product = Product.new
end
def create
@product = Product.new(params[:product])
if @product.save
redirect_to @product
else
render 'new'
end
end
def update
@product = Product.find(params[:id])
if @product.update_attributes(params[:product])
flash[:success] = "Producto Actualizado"
redirect_to root_path
else
render 'edit'
end
end
def destroy
Product.find(params[:id]).destroy
flash[:success] = "Producto Eliminado."
redirect_to root_path
end
end
たくさんの質問があります。
製品のインデックス ページに製品ごとの総投票数を表示するにはどうすればよいですか?
インデックス製品ページにボタンを作成して、製品への投票を追加するにはどうすればよいですか?
これを行う方法がわからず、同様の例を含むチュートリアルまたはブログが見つかりませんでした。
ご協力いただきありがとうございます。