好的 - 所以我有一个PINS模型,基本上允许用户上传图像(引脚)。在网站的主页上,我想查询并向用户显示已将多少总图像加载到数据库中 - 我使用回形针和设计:我需要做些什么来计算和显示针脚的总数?感谢大家花时间帮忙解决这个问题:)
这是我的图钉模型:
class Pin < ActiveRecord::Base
attr_accessible :description, :image, :tag_list, :image_remote_url
has_attached_file :image, styles: { medium: "320x300>" }
validates :description, presence: true
validates :user_id, presence: true
validates_attachment :image, presence: true,
content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', ] },
size: { less_than: 100.megabytes }
belongs_to :user
has_attached_file :image, styles: { medium: "320x300"}
def image_remote_url=(url_value)
self.image = URI.parse(url_value) unless url_value.blank?
super
end
acts_as_taggable
def next_image
self.class.where('id > ?', self.id).order('id asc').first
end
def previous
self.class.where('id < ?', self.id).order('id desc').first
end
end
和我的控制员:
class PinsController < ApplicationController
before_filter :authenticate_user!, except: [:show, :index]
# GET /pins
# GET /pins.json
def index
if params[:tag]
@pins = Pin.tagged_with(params[:tag]).order("created_at desc").paginate(:page => params[:page], :per_page => 100)
else
@pins = Pin.order("created_at desc").page(params[:page]).per_page(100)
#respond_to do |format|
#format.html
#format.atom
end
end
# @pins = Pin.all
#end
#end
# GET /pins/1
# GET /pins/1.json
def show
@pin = Pin.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @pin }
end
end
# GET /pins/new
# GET /pins/new.json
def new
@pin = current_user.pins.new
@pin_count = Pin.count
respond_to do |format|
format.html # new.html.erb
format.json { render json: @pin }
end
end
# GET /pins/1/edit
def edit
@pin = current_user.pins.find(params[:id])
end
# POST /pins
# POST /pins.json
def create
@pin = current_user.pins.new(params[:pin])
respond_to do |format|
if @pin.save
format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
format.json { render json: @pin, status: :created, location: @pin }
else
format.html { render action: "new" }
format.json { render json: @pin.errors, status: :unprocessable_entity }
end
end
end
# PUT /pins/1
# PUT /pins/1.json
def update
@pin = current_user.pins.find(params[:id])
respond_to do |format|
if @pin.update_attributes(params[:pin])
format.html { redirect_to @pin, notice: 'Pin was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @pin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pins/1
# DELETE /pins/1.json
def destroy
@pin = current_user.pins.find(params[:id])
@pin.destroy
respond_to do |format|
format.html { redirect_to pins_url }
format.json { head :no_content }
end
end
end
#def pin_params
#params.require(:pin).permit(:description, :image)
#end
答案 0 :(得分:0)
你在一个月Rails不是吗?精彩教程:)
在您的Pin Controller中包含您想要的Action中的代码(在您的示例中为show
操作):
def show
# your other code here
@pin_count = Pin.count
# @count is now available in your view for `show`
end
您可以在视图中调用=&gt; <%= @pin_count %>