oneWorkingHeadphone
oneWorkingHeadphone

Reputation: 899

Rails commits change from form, but nothing written to DB

I think there must be something simple that I'm not setting up correctly, but I cannot figure out this behavior. The user submits and edit form, rails log shows the change is committed, but when I load the record again, it's still the same.

Here's the form

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

and relevant parts of controller

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

And the output on submit

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)

Upvotes: 1

Views: 47

Answers (1)

Dias
Dias

Reputation: 882

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

Upvotes: 1

Related Questions