我有 2 个模型(书籍和图像)
class Book < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images
end
class Image < ActiveRecord::Base
belongs_to :book
has_attached_file :data, :path => ":rails_root/public/system/datas/:book_id/:style/:basename.:extension",
:url => "/system/datas/:book_id/:style/:basename.:extension",
:styles => {
:thumb => ["150x172#",:jpg],
:large => ["100%", :jpg]
}
validates_attachment_presence :data
validates_attachment_content_type :data,
:content_type => ['image/jpeg', 'image/pjpeg',
'image/jpg', 'image/png', 'image/tiff', 'image/gif'], :message => "has to be in a proper format"
end
我想修改我的应用程序,以便当用户创建新书并上传图片时,他们会被重定向到特定页面,否则他们会被定向到“显示”页面
我在 Book Controller 中修改了我的“创建”操作,如下所示:
def create
@book = Book.new(params[:book])
if @book.save
flash[:notice] = 'Book was successfully created'
if params[:book][:image][:data].blank? # === this line is giving me errors
redirect_to @specific_page
else
redirect_to show_book_path(@book.id)
end
else
render :action => 'new'
end
end
我想测试如果上传了一张或多张图片,用户应该被引导到不同的页面
以下 if 条件错误:
if params[:book][:image][:data].blank? # === this line is giving me errors
如果有人能建议我如何检查图像是否已附加到新书中,我将不胜感激。
请注意,我只想检查新上传的图片,而不是与图书相关的所有图片。
谢谢