創建一個新的has_many我試圖使用Rails button_to在我的連接表中創建一個新的實例。我有4個型號(製造商,批次,報價和批發商) - 製造商has_many批次,批次has_many優惠,並has_many批發商通過優惠。批發商has_many優惠和has_many批次通過優惠。通過使用button_to
我在給定的批次中設置了「新優惠」視圖,作爲所有批發商的清單,以便製造商可以點擊單個批發商旁邊的按鈕,該按鈕將創建一個新的「優惠」批次和具體的批發商。
<%= button_to '+', {:controller => "offers", :action => "create", :wholesaler_id => wholesaler.id}, :method=>:post %>
我的報價控制器創建方法:
def create
@offer = Offer.new(offer_params)
respond_to do |format|
if @offer.save
format.html { redirect_to @offer, notice: 'Offer was successfully created.' }
format.json { render :show, status: :created, location: @offer }
else
format.html { render :new }
format.json { render json: @offer.errors, status: :unprocessable_entity }
end
end
end
我offer_params方法在要約控制器:
def offer_params
params.require(:offer).permit(:batch_id, :wholesaler_id, :amount, :accepted, :ignored)
end
我得到一個錯誤,當我嘗試,然後單擊添加按鈕 -
ActionController::ParameterMissing in OffersController#create
param is missing or the value is empty: offer
指的是offer_params方法。
由於我沒有創建要約,直到我點擊按鈕,我不知道如何/我可以參考它。
感謝您的任何幫助 - 很高興發佈任何其他可能有用的回答代碼。
全 '新' 的觀點:
<div id="wrapper">
<div id="unselected">
<h2> Wholesalers</h2>
<table>
<thead>
<tr>
<th> Wholesaler </th>
<th> Add</th>
</tr>
</thead>
<tbody>
<% @unselected_wholesalers.each do |wholesaler| %>
<tr>
<td><%=wholesaler.name %></td>
<td><%= button_to '+',
{:controller => "offers", :action => "create",
:wholesaler_id => wholesaler.id},
:method=>:post %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
<%= link_to 'Back', manufacturer_batches_path(@manufacturer) %>
你能不能請附上'offer'形式,你用按鈕來發送? – idej
我不確定您的報價表單是什麼意思?我沒有使用表單部分,我剛剛在新的優惠視圖中獲得了代碼 - 我已將完整的代碼放在了我的文章中 –