2014-12-06 51 views
1

当我尝试自定义我的bid_params以向数据库添加参数时,我的强参数因某种原因而不起作用。我需要能够在创建出价时将current_user传递到数据库中。此对象嵌套在与拍卖有关的has_many belongs_to关系中。这里是我的控制器:ActionController :: ParameterMissing(参数丢失或值为空:出价)

class BidsController < ApplicationController 
    def index 
    @auction = Auction.find(params[:auction_id]) 
    @bids = @auction.bids 
    end 

    def new 
    @auction = Auction.find(params[:auction_id]) 
    @bid = @auction.bids.build 
    end 

    def create 
    @auction = Auction.find(params[:auction_id]) 
    @bid = @auction.bids.create(bid_params) 
    if @bid.save 
     flash[:success] = "Bid has been successfully placed." 
     redirect_to @auction 
    else 
     flash[:error] = @bid.errors.full_messages.join('. ') 
     render 'new' 
    end 
    end 

    def destroy 
    @auction = Auction.find(params[:auction_id]) 
    @bid = @auction.bids.find 
    @bid.destroy 
    flash[:notice] = "Successfully destroyed Bid." 
    redirect_to auction_url(@bid.article_id) 
    end 

    private 

    def bid_params 
    params.require(:bid).permit(:auction_id).merge(bidder: current_user) 
    end 

end 

和堆栈跟踪:

Started POST "/auctions/2/bids" for 127.0.0.1 at 2014-12-06 08:54:35 -0600 
Processing by BidsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"6x4hV8y323a10kaJN5Rubj1z3uhUrSDQrD6aoaWCUhk=", "commit"=>"Create Bid", "auction_id"=>"2"} 
    Auction Load (0.1ms) SELECT "auctions".* FROM "auctions" WHERE "auctions"."id" = ? LIMIT 1 [["id", 2]] 
Completed 400 Bad Request in 2ms 

ActionController::ParameterMissing (param is missing or the value is empty: bid) 

新形式:

<h1>Create a New Bid</h1> 
<%= form_for ([@auction, @bid]) do |f|%> 
<p> 
<%= f.submit %> 
</p> 
<%end%> 

谢谢!

+0

秀新形式.. – Nithin 2014-12-06 15:05:56

回答

1

看参数接收控制器:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"6x4hV8y323a10kaJN5Rubj1z3uhUrSDQrD6aoaWCUhk=", "commit"=>"Create Bid", "auction_id"=>"2"} 

然后尝试允许这些PARAMS:

def bid_params 
    params.require(:bid).permit(:auction_id).merge(bidder: current_user) 
end 

和错误被扔在这个操作:params.require(:bid)因为该方法假设您的PARAMS容貌如:

{ ..., "bid" => { "auction_id" => "2" } } 

因此,您可能会更改您的视图/ js发送params中,改变def bid_params实施:

def bid_params 
    params.permit(:auction_id).merge(bidder: current_user) 
end 
+0

啊!我懂了。我会检查,看看它是否在几个小时内工作。感谢您的回应。 – 2014-12-06 15:47:02

+0

它的工作!很好地抓住亚历山大本周末休息好 – 2014-12-06 18:37:06

相关问题