次のコードを使用して、渡した 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
ID の正しい URL が作成されるため、bill_path が「bill」オブジェクトを渡していることは確かです。
BillsController の update アクション:
# 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
このエラーが発生し続けます: