2017-03-21 78 views
1

我認爲必須有一些簡單的東西,我沒有正確設置,但我無法弄清楚這種行爲。用戶提交併編輯表單,rails日誌顯示提交的更改,但是當我再次加載記錄時,它仍然是相同的。Rails提交從窗體更改,但沒有寫入數據庫

這裏的表單控件

def edit 
    @deal = Deal.find(params[:id]) 
    end 

    def update 
    @deal = Deal.find(params[:id]) 
    if @deal.save 
     flash[:success] = 'Your deal was updated' 
     redirect_to root_path 
    else 
     render 'edit' 
    end 
    end 

<%= form_for(@deal) do |f| %> 
    <div class="deal-<%= @deal.id %>"> 
    <div class="field"> 
     <%= f.label :headline %><br /> 
     <%= f.text_field :headline, required: true %> 
    </div> 
    <div class="field"> 
     <%= f.label :matter %> 
     <%= f.text_field :matter %> 
    </div> 
    <div class="field"> 
     <%= f.label :summary %> 
     <%= f.text_area :summary %> 
    </div> 
    <%= f.submit "Update" %> 
    </div> 
<% end %> 

和相關部分,而在輸出提交

Started PATCH "/deals/1" for ::1 at 2017-03-21 13:15:17 +0100 
Processing by DealsController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"Rctg6qcVMfOutyXjZihiR5+zhagr+cduNfA6TSYqOHmjvh+dcQl9TZ/MxZbY+IMyt0LU2PQrsyCjcUJczGcTKg==", "deal"=>{"headline"=>"testheadline", "matter"=>"matter", "summary"=>"First test summary"}, "commit"=>"Update", "id"=>"1"} 
    Deal Load (0.1ms) SELECT "deals".* FROM "deals" WHERE "deals"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 
    (0.0ms) begin transaction 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 6], ["LIMIT", 1]] 
    (0.1ms) commit transaction 
Redirected to http://localhost:3000/ 
Completed 302 Found in 5ms (ActiveRecord: 0.4ms) 

回答

1
def update 
    @deal = Deal.find(params[:id]) 
    if @deal.update(deal_params) 
    flash[:success] = 'Your deal was updated' 
    redirect_to root_path 
    else 
    render 'edit' 
    end 
end 

private 
def deal_params 
    params.require(:deal).permit(:headline, :matter, :summary) 
end 
+0

這給我一個'加載ActiveModel :: ForbiddenAttributesError在DealsController#update'錯誤提交後 – oneWorkingHeadphone

+0

@oneWorkingHeadphone我已更新答案 請閱讀導軌部分中的強參數以便更好地理解 – Dias

+0

更新已爲我解決,我在頂部的.require()中有錯誤的參數。非常感謝! – oneWorkingHeadphone