dimanche 22 mai 2016

rails3 update param in controller

I want to update 2 column values in a Rails3 controller if a certain submit button is used on the form.

Submit button on form:

  <%= simple_form_for @costproject, :html => {:class => 'form-horizontal'}, :validate => true do |f| %>
...

  <%= f.submit 'Submit to Division', :class => 'btn btn-warning', :name => "submit1" %>

Update logic in Controller:

class CostprojectsController < ApplicationController
...
  def update
    ...
    params[:coststatus_id] = 2 if params[:submit1]
    params[:submit_date] = Date.today if params[:submit1]
    respond_to do |format|
      if @costproject.update_attributes(params[:costproject])
        flash[:success] = "Project Submitted"  if    @costproject.previous_changes.include?(:submit_date)
        format.html { redirect_to nextpath }
        format.json { render json: @costproject }
      else
        format.html { render action: "edit" }
        format.json { render json: @costproject.errors, status: :unprocessable_entity }
      end
    end
   end

If I stop execution with an alert, it looks like the params have been set. But, the values don't get saved to the db.

Aucun commentaire:

Enregistrer un commentaire