操作控制器异常,用于构建“使用Rails中的Paperclip使用嵌套资源上载多个文件”

时间:2015-07-03 05:01:43

标签: ruby-on-rails ruby-on-rails-4 paperclip

我正在尝试为找到here的多个图片实施此rails cook解决方案,并且遇到操作控制器异常问题。我确定这是我看过的东西。

以下是我的.rb文件:

  class PicturesController < ApplicationController
  before_action :set_picture, only: [:show, :edit, :update, :destroy]

  # GET /pictures
  # GET /pictures.json
  def index
    @pictures = Picture.all
  end

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

  # GET /pictures/new
  def new
    @picture = Picture.new
  end

  # GET /pictures/1/edit
  def edit
  end

  # POST /pictures
  # POST /pictures.json
  def create
    @picture = Picture.new(picture_params)
     @document = Document.new(:name) # :name is a symbol, not

    respond_to do |format|
      if @picture.save
        format.html { redirect_to @picture, notice: 'Picture was successfully created.' }
        format.json { render :show, status: :created, location: @picture }
      else
        format.html { render :new }
        format.json { render json: @picture.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /pictures/1
  # PATCH/PUT /pictures/1.json
  def update
    respond_to do |format|
      if @picture.update(picture_params)
        format.html { redirect_to @picture, notice: 'Picture was successfully updated.' }
        format.json { render :show, status: :ok, location: @picture }
      else
        format.html { render :edit }
        format.json { render json: @picture.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /pictures/1
  # DELETE /pictures/1.json
  def destroy
    @picture.destroy
    respond_to do |format|
      format.html { redirect_to pictures_url, notice: 'Picture was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def picture_params
      params[:picture]
    end
end

我的照片控制器

    <!-- app/views/galleries/_form.html.erb -->
<%= form_for @gallery, :html => { :class => 'form-horizontal', multipart: true } do |f| %>
  <div class="control-group">
    <%= f.label :name, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :name, :class => 'text_field' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :description, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :description, :class => 'text_field' %>
    </div>
  </div>

  <div class="control-group">
    <%= f.label :pictures, :class => 'control-label' %>
    <div class="controls">
      <!-- The magic is coming ...look at my eyes....shazammmm -->
      <!-- Use HTML5 multiple attribute to enable multiple selection
           and pass back to controller all files as an array, ready
           for paperclip!!
           file_field_tag, since images is not a gallery attribute
       -->
      <%= file_field_tag "images[]", type: :file, multiple: true %>
    </div>
  </div>

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

这是我的表格

 class GalleriesController < ApplicationController
  before_action :set_gallery, only: [:show, :edit, :update, :destroy]

  # GET /galleries
  # GET /galleries.json
  def index
    @galleries = Gallery.all
  end

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

  # GET /galleries/new
  def new
    @gallery = Gallery.new
  end

  # GET /galleries/1/edit
  def edit
  end

  # POST /galleries
  # POST /galleries.json
  def create
  @gallery = Gallery.new(gallery_params)

  respond_to do |format|
    if @gallery.save

      if params[:images]
        #===== The magic is here ;)
        params[:images].each { |image|
          @gallery.pictures.create(image: image)
        }
      end

      format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' }
      format.json { render json: @gallery, status: :created, location: @gallery }
    else
      format.html { render action: "new" }
      format.json { render json: @gallery.errors, status: :unprocessable_entity }
    end
  end
end

  # PATCH/PUT /galleries/1
  # PATCH/PUT /galleries/1.json
  def update
    respond_to do |format|
      if @gallery.update(gallery_params)
        format.html { redirect_to @gallery, notice: 'Gallery was successfully updated.' }
        format.json { render :show, status: :ok, location: @gallery }
      else
        format.html { render :edit }
        format.json { render json: @gallery.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /galleries/1
  # DELETE /galleries/1.json
  def destroy
    @gallery.destroy
    respond_to do |format|
      format.html { redirect_to galleries_url, notice: 'Gallery was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def gallery_params
      params.require(:gallery).permit(:gallery_id, :picture)
    end
end

这是我的画廊控制器

public class MyHostApduService extends HostApduService {
    @Override
    public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
        // TODO: process the incoming APDU command(s) and give reasonable responses
    }

    @Override
    public void onDeactivated(int reason) {
    }
}

这是我的错误 enter image description here

1 个答案:

答案 0 :(得分:0)

您的picture_params方法可能会导致问题。尝试修改它以使用以下格式:

def picture_params
  params.require(:picture).permit(:whitelisted, :attributes, :placed, :here)
end

您可以使用您所关注的教程中提供的this repo作为参考。

strong parameters上的其他参考资料。

希望它有所帮助!