0

我正在尝试更新我使用以下代码传递的 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

我不断收到此错误:

更新放置请求错误

4

1 回答 1

2

你还没有告诉它要更新什么。您是否需要将账单标记为“已付款”?也许令人困惑的是,这种情况下的 params 散列既需要:id由您的对象提供的 ,又需要包含要更新的数据bill的键下的嵌套散列。:bill如果您想PUT通过链接而不是表单来获取数据,您可以将一组参数作为第二个参数传递给bill_path

link_to "Pay", bill_path(bill, { bill: { paid: true }), { method: :put, ... #etc
于 2013-09-23T01:53:08.950 回答