![](/img/trans.png)
[英]Rails: ActionController::ParameterMissing param is missing or the value is empty:
[英]ActionController::ParameterMissing (param is missing or the value is empty: bid)
當我嘗試自定義我的bid_params以向數據庫添加參數時,出於某些原因,我的強參數無法正常工作。 我需要能夠在創建出價時將current_user傳遞到數據庫中。 該對象嵌套在與Auctions的has_many所屬關系中。 這是我的控制器:
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%>
謝謝!
查看控制器收到的參數:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6x4hV8y323a10kaJN5Rubj1z3uhUrSDQrD6aoaWCUhk=", "commit"=>"Create Bid", "auction_id"=>"2"}
然后,您嘗試允許這些參數:
def bid_params
params.require(:bid).permit(:auction_id).merge(bidder: current_user)
end
在此操作中引發了錯誤: params.require(:bid)
因為此方法假定您的params看起來像:
{ ..., "bid" => { "auction_id" => "2" } }
因此,您可以更改發送參數的view / js或將def bid_params
實現更改為:
def bid_params
params.permit(:auction_id).merge(bidder: current_user)
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.