关于多次上传的回形针的两个问题

时间:2013-09-25 07:31:17

标签: ruby-on-rails paperclip

我正在完成一项工作任务。 Rails 4应用程序,用户可以在其中创建海报。海报可以有多个图像上传。实际上,我没有明确的错误,只有两个问题。但在问题之前,这是我的文件。 Poster.rb:

class Poster < ActiveRecord::Base
  has_many :poster_images, dependent: :destroy
  accepts_nested_attributes_for :poster_images, allow_destroy: true
  belongs_to :type
  belongs_to :user
  default_scope -> { order('created_at DESC') }

  validates :title, :body, :publish_date, :user_id, :presence => true
end

poster_image.rb:

class PosterImage < ActiveRecord::Base
  belongs_to :poster
  has_attached_file :image, :styles => {:medium => "300x300>", :thumb => "100x100>" }
end

posters_controller.rb:

class PostersController < ApplicationController
  before_filter :set_poster, only: [:show, :edit, :update, :destroy]
  before_filter :authenticate_user!, :except => [:index, :show]
  authorize_resource
  load_resource except: :create
  # GET /posters
  # GET /posters.json
  def index
    @posters = Poster.paginate(page: params[:page], :per_page => 10)
  end

  # GET /posters/1
  # GET /posters/1.json
  def show
  end

  # GET /posters/new
  def new
  end

  # GET /posters/1/edit
  def edit

  end

  # POST /posters
  # POST /posters.json
  def create
    @poster = Poster.new(poster_params)
    @poster.user_id = current_user.id
    respond_to do |format|
      if @poster.save
        format.html { redirect_to @poster, notice: 'Poster was successfully created.' }
        format.json { render action: 'show', status: :created, location: @poster }
      else
        format.html { render action: 'new' }
        format.json { render json: @poster.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posters/1
  # PATCH/PUT /posters/1.json
  def update
    respond_to do |format|
      if @poster.update(poster_params)
        format.html { redirect_to @poster, notice: 'Poster was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @poster.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posters/1
  # DELETE /posters/1.json
  def destroy
    @poster.destroy
    respond_to do |format|
      format.html { redirect_to posters_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_poster
      @poster = Poster.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def poster_params
      params.require(:poster).permit(:title, :body, :publish_date, :type_id, :type_name,   poster_images_attributes: :image )
    end
  end

_form.html.erb:

<%= simple_nested_form_for @poster, :html =>  {:multipart => true } do |f| %>
  <%= f.input :title, :autofocus => true %>
  <%= f.input :body %>
  <%= f.input :publish_date %>
  <%= f.input :type_id, :collection => Type.all, required:     true %>


  <%= f.fields_for :poster_images do |images_f| %>
  <%= images_f.file_field :image %>
  <%= images_f.link_to_remove "X" %>

  <% end %>
  <%= f.link_to_add "Add image", :poster_images %>


  <div class="form-actions">
    <%= f.button :submit, :class => 'btn-primary' %>
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
            posters_path, :class => 'btn' %>
  </div>
<% end %>

show.html.erb:

<%- model_class = Poster -%>
<div class="page-header">
  <h1><%=t '.title', :default => model_class.model_name.human.titleize %></h1>
</div>

<dl class="dl-horizontal">
  <dd><h3><%= @poster.title %></h3></dd>

  <dd><%= @poster.body %></dd>

  <dt><strong><%= model_class.human_attribute_name(:publish_date) %>:</strong></dt>
  <dd><%= @poster.publish_date %></dd>
  <dt><strong>Author:</strong></dt>
  <dd><%= link_to @poster.user.name, @poster.user %></dd>
  <dt><strong>Type:</strong></dt>
  <dd><%= @poster.type.name %></dd>
</dl>

<dt><strong>Image(s):</strong></dt>

<% @poster.poster_images.each do |p| %>
<%= content_tag "p_#{p.id}" do %>
<%= image_tag p.image.url(:medium) %>
<% end %>
<% end %>



<div class="form-actions">
  <%= link_to t('.back', :default => t("helpers.links.back")),
          posters_path, :class => 'btn'  %>

  <% if current_user && (current_user.has_role?(:admin) || current_user.id==@poster.user_id) %>
      <% if can? :update, Poster %>
  <%= link_to t('.edit', :default => t("helpers.links.edit")),
          edit_poster_path(@poster), :class => 'btn' %>
      <% end %>
  <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
          poster_path(@poster),
          :method => 'delete',
          :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
          :class => 'btn btn-danger' %>
      <% end %>
</div>

现在有两个我的问题:

  1. 当我创作海报时,一切都很顺利。但是,当我试图更新我的海报并推送“编辑”时,我的表格即将进入,我看到了我创建海报时所拥有的所有字段。但是,如果在创建海报时我添加了一张图片,当我看到EDIT表单时,我已经打开了添加图​​片的链接。如果我使用它,它没关系,但是当我只是编辑我的名字,并保存而不添加新的图片时,我的“show.html.erb”显示我之前的图片和“破损”图片的标志它。如果我有两张图片,在更新海报时,rails试图让我再添加两张图片或者添加两张破碎图片。有什么想法吗?(如果你理解了上面的内容)

  2. 正在检查我的应用程序的人说我忘了在“poster_params”中的控制器中的“poster_images_attributes :: image”中添加一些键。我在SOF中发现它应该足够了,但是他说它足以创建海报,但不能用于编辑和删除操作。我还应该在poster_params中写些什么?感谢

1 个答案:

答案 0 :(得分:0)

Rails with Paperclip ignore empty attachments

这就是我的两个问题的解决方案!

poster_images_attributes: [:image, :id]
相关问题