2013-09-23 69 views
0

我試圖更新我用下面的代碼通過我的條例草案對象的屬性模型對象:更新放置請求失敗,不會讓我送

<%= 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 

我不斷收到此錯誤:

error for update put request

回答

2

你沒有告訴它要更新什麼。您是否需要將賬單標記爲「已付款」?也許令人困惑的是,在這種情況下,params散列需要由bill對象提供的:id和包含要更新的數據的:bill密鑰下的嵌套散列。如果你想通過一個鏈接,而不是形式PUT數據,你可以通過一組PARAMS作爲第二個參數來bill_path

link_to "Pay", bill_path(bill, { bill: { paid: true }), { method: :put, ... #etc 
+0

只是賬單支付{:真正}工作。我早些時候嘗試過,但只是「付費:真」沒有散列。非常感謝!!! –