我的Paperclip对象上的禁止属性错误

时间:2014-10-05 03:07:24

标签: ruby-on-rails controller paperclip

我已经在几个小时内对抗这个问题了。当我尝试创建拍卖时,我有一个禁止属性错误;我到处寻找答案,但没有一个能奏效。这是我的代码: 这是我的整个控制器...

class AuctionsController < ApplicationController
load_and_authorize_resource

def index
end

def show
end

def new
end

def edit
end

def create
@auction = Auction.new(auction_params)
respond_to do |format|
if @auction.save
  format.html { redirect_to @auction, notice: 'Auction was successfully created.' }
  format.json { render action: 'show', status: :created, location: @auction }
else
  format.html { render action: 'new' }
  format.json { render json: @auction.errors, status: :unprocessable_entity }
end
end
end

def update
respond_to do |format|
  if @auction.update(auction_params)
    format.html { redirect_to @auction, notice: 'Auction was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: 'edit' }
    format.json { render json: @auction.errors, status: :unprocessable_entity }
  end
end
end

def destroy
  @auction.destroy
    respond_to do |format|
     format.html { redirect_to auctions_url }
     format.json { head :no_content }
    end
end

private

def auction_params
  params.require(:auction).permit(:title, :price, :image)
end
end

我现在得到一个禁止属性错误;我不知道为什么会这样。这是我的迁移:

class AddAttachmentImageToAuctions < ActiveRecord::Migration
  def self.up
    change_table :auctions do |t|
  t.attachment :image
end
end

def self.down
 remove_attachment :auctions, :image
end
end

class CreateAuctions < ActiveRecord::Migration
  def change
    create_table :auctions do |t|
      t.string :title
      t.string :price

      t.timestamps
    end
  end
end

这是我的表格:

<%= form_for(@auction) do |f| %>
  <% if @auction.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@auction.errors.count, "error") %> prohibited this auction from being    saved:</h2>

      <ul>
      <% @auction.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.text_field :price %>
  </div><br />
  <div class="field">
    <%= f.file_field :image %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

由于参数丢失错误,我甚至无法获取发布请求的日志...抱歉。 以下是我得到的确切错误...编辑:由于mandeep,此错误已解决。现在它只是paperclip对象的Forbidden Attributes错误:

    ActiveModel::ForbiddenAttributesError in AuctionsController#create
ActiveModel::ForbiddenAttributesError

Rails.root: /Users/claymccullough/Desktop/cybocars

Application Trace | Framework Trace | Full Trace
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"MeQb0/xWREQ4imyYl5nDRy5KioyePs6PYe7ARXyaSTs=",
 "auction"=>{"title"=>"",
 "price"=>""},
 "commit"=>"Create Auction"}

当我尝试在表单上发布POST时,以下是完整日志:

Started POST "/auctions" for 127.0.0.1 at 2014-10-05 14:34:10 -0500
Started POST "/auctions" for 127.0.0.1 at 2014-10-05 14:34:10 -0500
Processing by AuctionsController#create as HTML
Processing by AuctionsController#create as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"MeQb0/xWREQ4imyYl5nDRy5KioyePs6PYe7ARXyaSTs=", "auction"=>{"title"=>"",     "price"=>""}, "commit"=>"Create Auction"}
  Parameters: {"utf8"=>"✓",   "authenticity_token"=>"MeQb0/xWREQ4imyYl5nDRy5KioyePs6PYe7ARXyaSTs=", "auction"=>{"title"=>"", "price"=>""}, "commit"=>"Create Auction"}
Completed 500 Internal Server Error in 2ms
Completed 500 Internal Server Error in 2ms

ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
  activemodel (4.0.8) lib/active_model/forbidden_attributes_protection.rb:21:in     `sanitize_for_mass_assignment'
  activerecord (4.0.8) lib/active_record/attribute_assignment.rb:21:in `assign_attributes'
  activerecord (4.0.8) lib/active_record/core.rb:469:in `init_attributes'
  activerecord (4.0.8) lib/active_record/core.rb:185:in `initialize'
  activerecord (4.0.8) lib/active_record/inheritance.rb:30:in `new'
  activerecord (4.0.8) lib/active_record/inheritance.rb:30:in `new'
  cancan (1.6.10) lib/cancan/controller_resource.rb:85:in `build_resource'
  cancan (1.6.10) lib/cancan/controller_resource.rb:66:in `load_resource_instance'
  cancan (1.6.10) lib/cancan/controller_resource.rb:32:in `load_resource'
  cancan (1.6.10) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
  cancan (1.6.10) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
  activesupport (4.0.8) lib/active_support/callbacks.rb:407:in     `_run__1086883359883225076__process_action__callbacks'
  activesupport (4.0.8) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.8) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (4.0.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.0.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.0.8) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.0.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.0.8) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.0.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.0.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.0.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.0.8) lib/abstract_controller/base.rb:136:in `process'
  actionpack (4.0.8) lib/abstract_controller/rendering.rb:44:in `process'
  actionpack (4.0.8) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.0.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.0.8) lib/action_controller/metal.rb:231:in `block in action'
  actionpack (4.0.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
  actionpack (4.0.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
  actionpack (4.0.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
  actionpack (4.0.8) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.0.8) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.0.8) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.0.8) lib/action_dispatch/routing/route_set.rb:676:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `catch'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.0.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.0.8) lib/action_dispatch/middleware/flash.rb:241:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.0.8) lib/action_dispatch/middleware/cookies.rb:486:in `call'
  activerecord (4.0.8) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.0.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in  `call'
  activerecord (4.0.8) lib/active_record/migration.rb:373:in `call'
  actionpack (4.0.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.8) lib/active_support/callbacks.rb:373:in `_run__4322106486789320703__call__callbacks'
  activesupport (4.0.8) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.8) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.0.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.8) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.8) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.0.8) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.0.8) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.0.8) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.0.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.8) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.8) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.0.8) lib/rails/engine.rb:511:in `call'
  railties (4.0.8) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /Users/claymccullough/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /Users/claymccullough/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /Users/claymccullough/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

1 个答案:

答案 0 :(得分:1)

我终于发现了什么是错的。我在控制器中错过了两条神奇的线:这是固定控制器。我希望这可以帮助其他有这个问题的人。

class AuctionsController < ApplicationController
  before_action :set_auction, only: [:show, :edit, :update, :destroy]

  # GET /auctions
  # GET /auctions.json
  def index
    @auctions = Auction.all
  end

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

  # GET /auctions/new
  def new
    @auction = Auction.new
  end

  # GET /auctions/1/edit
  def edit
  end

  # POST /auctions
  # POST /auctions.json
  def create
    @auction = Auction.new(auction_params)

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

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

  # DELETE /auctions/1
  # DELETE /auctions/1.json
  def destroy
    @auction.destroy
     respond_to do |format|
      format.html { redirect_to auctions_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def auction_params
      params.require(:auction).permit(:title, :price, :image)
    end
end

谢谢Mandeep帮助我。