2014-10-05 2 views
1

나는 몇 시간 동안이 문제에 대해 내 머리를 두드렸다. 나는 경매를 만들려고 할 때 금지 된 속성 오류가 있습니다. Ive는 도처에 대답을 보았지만 그들 중 누구도 작동하지 않았습니다. ... 나는 지금 금지 얻고금지 된 Attibutes 오류 내 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 %> 

그리고 난 때문에 매개 변수가 누락 오류 POST 요청에 대한 로그를 얻을 수 없다 ... 죄송합니다. 정확한 오류는 다음과 같습니다. edit :이 오류는 mandeep 덕분에 해결되었습니다. 이제 클립 객체에 대한 바로 금지 속성 오류 : I 양식에 POST 할 때 여기

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"} 

전체 로그는 다음과 같습니다 마침내 무엇이 잘못된 것인지 알게

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' 
+0

어떤 오류가 발생합니까? 새로운 행동으로 경매를 만들고 있습니까? 양식을 제출할 때 로그를 게시 할 수 있습니까? – Mandeep

+0

나는 그것을 개정했다 : 나는 2 개의 에러를 얻는다. 금지 된 속성 에러와 파라미터 Missing Error. –

+0

# POST 할 때 서버의 로그는 무엇입니까? 귀하의 양식은 어떻게 생겼습니까? – user2936314

답변

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에게 감사드립니다.

+0

무엇이 누락 되었습니까? – Mandeep

+0

이전 조치 필터 및 설정된 경매 변수입니다. –

+0

Cancancan이 당신을 위해 그렇게하지 않았습니까? – Mandeep