当我尝试自定义我的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%>
谢谢!
秀新形式.. – Nithin 2014-12-06 15:05:56