簡體   English   中英

關於回形針多次上傳的兩個問題

[英]Two questions on paperclip with multiple uploads

我正在做一份工作。 用戶可以在其中創建海報的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. 當我創建海報時,一切都會變得很好。 但是,當我嘗試更新海報並按下“編輯”時,我的表單即將進入,並且我看到了創建海報時所擁有的所有字段。 但是,如果在創建海報時我添加了圖片,則當我看到“編輯”表單時,我已經打開了添加圖​​片的鏈接。 如果我使用它,也可以,但是,例如,當我只是編輯我的姓名並保存而沒有添加新圖片時,我的“ show.html.erb”將顯示我之前的圖片和一個“損壞”的圖像它。 如果我有兩個圖像,則在更新海報時,Rails會嘗試使我再添加兩個圖像,或者自己添加兩個損壞的圖像標志。 有什么想法嗎?(如果您了解上面的內容)

  2. 正在檢查我的應用程序的人說,我忘記在“ poster_params”控制器中的“ poster_images_attributes :: image”中添加一些鍵。 我在SOF中發現這里已經足夠了,但是他說創建海報已經足夠了,但是對於編輯和刪除操作來說已經足夠了。 有什么想法我還要在poster_params中寫些什么? 謝謝

帶回形針的滑軌會忽略空附件

這就是我兩個問題的解決方案!

poster_images_attributes: [:image, :id]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM