【发布时间】:2015-07-15 14:34:39
【问题描述】:
我正在用 ruby on rails 开发一个网站。我想在使用回形针创建帖子时上传多张图片。我找不到任何上传到公用文件夹的图像,因此在我创建新帖子时没有显示图像。我为图像创建了一个名为 Postimage.rb 的模型,并引用了已经存在的模型“post”。我使用回形针在其他模型中上传单个图像,它工作正常。
rails g model postimage post:references caption:string
rails g paperclip post_images photo
这是 Postimage.rb
class Postimage < ActiveRecord::Base
belongs_to :post
has_attached_file :photo,
:styles => {:small => "100x100>", :medium => "640x480>"}
validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/
end
在 Post 模型中,
class Post < ActiveRecord::Base
has_many :postimages
accepts_nested_attributes_for :postimages, :allow_destroy => true, :reject_if => lambda { |t| t[:postimage].nil? }
这是后期控制器中的代码
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
@posts = Post.all.order('created_at DESC').paginate(page: params[:page],:per_page =>7)
@users = User.all
respond_with(@posts)
end
def show
respond_with(@post)
end
def new
@post = Post.new
5.times {@post.postimages.build} # added this
respond_with(@post)
end
def edit
5.times{@post.postimages.build} # ... and this
end
def create
@post = current_user.posts.build(post_params)
@post.user_id = current_user.id
if @post.save
redirect_to @post, :notice =>"Post created successfully!!"
else
render "new"
end
end
def update
@post.update(post_params)
respond_with(@post)
end
def destroy
@post.destroy
redirect_to preschools_Home_path
#respond_with(@post)
end
private
def set_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :content, postimages_attributes: [:caption, :photo])
end
这是帖子中 _form.html 中的代码
<%= form_for(@post, :html =>{:multipart => true }) do |f| %>
<%=f.fields_for :postimages do |builder| %>
<%if builder.object.new_record? %>
<p>
<%= builder.label :caption, "Image Caption" %>
<%= builder.text_field :caption %>
</p>
<p>
<%= builder.label :photo, "Image File" %>
<%= builder.file_field :photo %>
</p>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这是帖子中 show.html.erb 中的代码
<div>
<% @post.postimages.each do |postimage|%>
<%= image_tag (postimage.photo.url(:medium))%>
<% end %>
</div>
你能帮帮我吗?谢谢。
我已添加图片栏来发布模型。并且该图像正在显示。但是在多次上传的情况下,就不行了。
下面的日志代码可能会有所帮助。
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PHcPmsB+a0qy6tn4U/nRyycjGFYHiFuum7LNqO8EX24+mGq+Yu7Ct0Ls/odNyAjUtVkT1cMImYRYwnpVajQRXA==", "post"=>{"title"=>"1st post", "content"=>"Added image", "postimages_attributes"=>{"0"=>{"caption"=>"1st image", "photo"=>#<ActionDispatch::Http::UploadedFile:0x007f4a351b0ba8 @tempfile=#<Tempfile:/home/ubuntu/workspace/website/RackMultipart20150715-1790-7hfszv.jpg>, @original_filename="Murali.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[postimages_attributes][0][photo]\"; filename=\"Murali.jpg\"\r\nContent-Type: image/jpeg\r\n">}
【问题讨论】:
-
我猜你必须将这些
:path => ":rails_root/public/system/:attachment/:id/:basename_:style.:extension"、:url => "/system/:attachment/:id/:basename_:style.:extension"添加到你的Postimage模型中。 -
是的。我已经添加了这些行,问题仍然存在。我在公共/系统文件夹中看不到“postimages”文件夹。我想知道为什么没有创建文件夹。
-
这种带有载波的解决方案是否适用于回形针? stackoverflow.com/a/21412174/2111040