小编kai*_*gth的帖子

rails 3与回形针和多个模型的多态关联

我想与paperclip建立多态关联,并允许我的用户拥有一个头像和多个图像.

附件模型:

class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
end

class Avatar < Attachment
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end

class Image < Attachment
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
Run Code Online (Sandbox Code Playgroud)

用户模型:

has_one :avatar, :as => :attachable, :class_name => 'Attachment', :conditions => {:type => 'avatar'}
accepts_nested_attributes_for :avatar
Run Code Online (Sandbox Code Playgroud)

用户控制器:

def edit
   @user.build_avatar
end
Run Code Online (Sandbox Code Playgroud)

用户视图表单:

<%= form_for @user, :html => { :multipart => true } do …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails paperclip polymorphic-associations ruby-on-rails-3

7
推荐指数
1
解决办法
4703
查看次数

rails 3根据布尔值验证状态

我有一个名为boolean的模型 draft

我只想验证字段的存在draft == false.

我的模特

if self.draft == false
  validates :name, :presence => true, :length => { :maximum => 45 }
  validates :description, :presence => true
  validates :blurb, :presence => true, :length => { :maximum => 175 }
  validates :category_id, :presence => true
  validates :location_id, :presence => true
  validates :goal, :presence => true
else
end
Run Code Online (Sandbox Code Playgroud)

在我的控制器中

def new
  @item.new(:draft => false) || @item.new(:draft => true)

def create
  if params[:commit] == "Create Item"
    @cause = Item.new(params[:item], :draft => …
Run Code Online (Sandbox Code Playgroud)

validation if-statement model ruby-on-rails-3

3
推荐指数
1
解决办法
2385
查看次数