0

I have to get the row id after submission of a form.

My model is Boxelement.rb

    def create
        if @boxelement.save
                puts params[:boxelement]
        end
    end

Output is:

    {"name"=>"fwqfew", "project_id"=>"1", "author_id"=>"1", "private_flag"=>"0"} 

But I need to know the id of the newly created row. How can I get it?

Reza
  • 2,896
  • 4
  • 26
  • 37

1 Answers1

3

The params hash won't have the id since that's being assigned when you call save. So even though you're accessing the params after you perform the save, Rails doesn't update the params hash. You need to just access id directly on @boxelment:

def create
  @boxelment = Boxelement.new(params[:boxelement])
  if @boxelement.save
    puts @boxelement.id
  end
end
Helios de Guerra
  • 3,445
  • 18
  • 23