我正在尝试更新我使用以下代码传递的 Bill 对象的属性:
<%= link_to "Pay", bill_path(bill), { method: :put, :class => 'btn btn-mini btn-primary', id: 'payBills' } %>
这只是完整的部分:
<table class="table table-striped" id="bills">
<thead>
<tr>
<th><%= "Name" %></th>
<th><%= "Amount" %></th>
<th><%= "Due by" %></th>
<th><%= "Status" %></th>
<th><%=t '.actions', :default => t("helpers.actions") %></th>
</tr>
</thead>
<tbody>
<% bill_filter.keys.sort.each do |month| %>
<tr>
<td><h2><%= month.strftime('%B') %></h2></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<% for bill in bill_filter[month].sort { |a, b| ((a.due_by <=> b.due_by) == 0) ? (a.amount <=> b.amount) : (a.due_by <=> b.due_by) } %>
<tr>
<td><%= link_to bill.name, edit_bill_path(bill) %></td>
<td><%= number_to_currency bill.amount %></td>
<td><%= bill.due_by.strftime("%B %e") %></td>
<td><%= paid_status(bill) %></td>
<td>
<%= link_to "Pay", bill_path(bill), { method: :put, :class => 'btn btn-mini btn-primary', id: 'payBills' }%>
<%= link_to "Delete",
bill_path(bill),
:method => :delete,
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
:class => 'btn btn-mini btn-danger' %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Total</td>
<td><%= number_to_currency(bill_total bill_filter) %></td>
</tr>
</tbody>
</table>
# PATCH/PUT /bills/1
# PATCH/PUT /bills/1.json
def update
respond_to do |format|
if @bill.update(bill_params)
format.html { redirect_to bills_path, notice: 'Bill was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @bill.errors, status: :unprocessable_entity }
end
end
end
我确定 bill_path 正在传递“bill”对象,因为它为 id 创建了正确的 URL。
BillsController 中的更新操作:
# PATCH/PUT /bills/1
# PATCH/PUT /bills/1.json
def update
respond_to do |format|
if @bill.update(bill_params)
format.html { redirect_to bills_path, notice: 'Bill was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @bill.errors, status: :unprocessable_entity }
end
end
end
和完整的控制器:
class BillsController < ApplicationController
before_action :set_bill, only: [:show, :edit, :update, :destroy]
# GET /bills
# GET /bills.json
def index
@bills = Bill.find(:all)
@all_bills_by_months = @bills.group_by { |b| b.due_by.beginning_of_month }
@bills_for_this_month = @all_bills_by_months.select {|m, b| m.strftime('%B') == Date.today.strftime('%B')}
@bills_for_next_month = @all_bills_by_months.select {|m, b| m.strftime('%B') == Date.today.next_month.strftime('%B')}
@bills_for_last_month = @all_bills_by_months.select {|m, b| m.strftime('%B') == Date.today.last_month.strftime('%B')}
end
# GET /bills/1
# GET /bills/1.json
def show
end
# GET /bills/new
def new
@bill = Bill.new
end
# GET /bills/1/edit
def edit
@put = true
end
# POST /bills
# POST /bills.json
def create
@bill = Bill.new(bill_params)
respond_to do |format|
if @bill.save
format.html { redirect_to bills_path, notice: 'Bill was successfully created.' }
format.json { render action: 'show', status: :created, location: @bill }
else
format.html { render action: 'new' }
format.json { render json: @bill.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /bills/1
# PATCH/PUT /bills/1.json
def update
respond_to do |format|
if @bill.update(bill_params)
format.html { redirect_to bills_path, notice: 'Bill was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @bill.errors, status: :unprocessable_entity }
end
end
end
# DELETE /bills/1
# DELETE /bills/1.json
def destroy
@bill.destroy
respond_to do |format|
format.html { redirect_to bills_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_bill
@bill = Bill.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def bill_params
params.require(:bill).permit(:name, :amount, :due_by, :paid)
end
end
我不断收到此错误: