2013-09-23 33 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

只是账单支付{:真正}工作。我早些时候尝试过,但只是“付费:真”没有散列。非常感谢!!! –