Z
Z
Zaur Ashurbekov2014-05-01 21:07:26
PostgreSQL
Zaur Ashurbekov, 2014-05-01 21:07:26

Why does rollback always come out?

And hello again, Habr!
Straight to the point. There is an action that saves or modifies an object.
here he is actually

def edit
       navigation_params = get_navigation_params 
       puts params_id    
       if params_id.present?
    item = Navigation.find_by_id(params_id)
       if item 	   
    item.update(navigation_params)
      end
       else
      item = Navigation.new(navigation_params)
            item.save
        end
  redirect_to controller: "routes", action: "main"
    end
private
   def params_id
    params[:navigation][:id]
   end
   def get_navigation_params
    params.require(:navigation).permit(:name, :url, :sort_id, :drop_down, :parent_id)
   end 
end

Here is the diagram of the Navigation model that needs to be created/updated
create_table "navigations", force: true do |t|
    t.string   "name"
    t.string   "url"
    t.integer  "sort_id"
    t.boolean  "drop_down"
    t.integer  "parent_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

and model
class Navigation < ActiveRecord::Base
  validates :name, :sort_id, absence: true
end

Here is what the console says:
Started POST "/navigation" for 127.0.0.1 at 2014-05-01 22:05:08 +0400
Processing by NavigationController#edit as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"aRyvGIMgPYOlzSrfmFyJewWc2ggs167kElGEiE9+87A=", "navigation"=>{"id"=>"", "name"=>"new item", "drop_down"=>"1", "url"=>"/url/", "sort_id"=>"1", "parent_id"=>""}, "commit"=>"Изменить"}
Unpermitted parameters: id

   (0.2ms)  BEGIN
  (0.2ms)  ROLLBACK
Redirected to http://0.0.0.0:3000/
Completed 302 Found in 14ms (ActiveRecord: 2.9ms)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Finist, 2014-05-01
@zaurius

read about absence validator

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question