0

I use this path: new_inspiration_path(inspiration_id: inspiration.id) to take info from an existing inspiration and pre-populate a new form so a user can duplicate the inspiration just by clicking the path route and then submit.

But my problem is that once the form is submitted the new inspiration isn't created.

  def new
    if params[:inspiration_id]
      @inspiration = Inspiration.find(params[:inspiration_id])
    end
    @inspiration = current_user.inspirations.build
  end

  def create
    @inspiration = current_user.inspirations.build(inspiration_params)
    @inspiration.save
  end

This is because @inspiration is stuck trying to do two different things: .find and .built. How can we do both when params[:inspiration_id]?

Question inspired by: How to prepopulate form with data passed from @object?

Community
  • 1
  • 1
AnthonyGalli.com
  • 2,796
  • 5
  • 31
  • 80

1 Answers1

0

Your code, as shown, is not using @inspirational. You need to pass @inspirational.attributes.slice(Inspiration.attribute_names) Inspiration.new to copy all pertinent attributes to the new object:

if params[:inspiration_id]
  @inspirational = Inspiration.find(params[:inspiration_id])
  inspirational_attributes = @inspirational.attributes.slice(Inspiration.attribute_names)
end

@inspiration = current_user.inspirations.new(inspirational_attributes)

Of course, make sure inspirational_attributes is set to {} when params[:inspiration_id] is not set.

Leonel Galán
  • 6,993
  • 2
  • 41
  • 60
  • That was a typo. I meant to say just `inspiration` for all, but maybe it would work if I made a distinction using `inspiration` and `inspirational`. I plugged in your code as is and by taking off the ending `al` from `inspirational`, but either way the code doesn't populate the _form. Maybe it is because I'm misunderstanding your "of course" paragraph. Sorry can you please elaborate Leito? Thanks so much for the help man! – AnthonyGalli.com Jan 19 '16 at 21:31
  • You say the form doesn't populate, but does inspirational_attributes have the values you want? We need to make sure that is working before worrying about the form. I just meant that that `inspirational_attributes = {}` should be before the `if`, so you don't get a "NameError: undefined local variable or method `inspirational_attributes'" – Leonel Galán Jan 19 '16 at 21:38
  • Yea I'm just putting `Inspiration.name` for `attribute_names` and when I click on the path I am brought to the new _form where it remains unpopulated. No errors come up tho. And thanks for the clarification Leito :) – AnthonyGalli.com Jan 19 '16 at 21:59
  • Ah you have `@inspirational.attributes.slice` etc. Is `attributes` there just a placeholder since I don't define it anywhere else? – AnthonyGalli.com Jan 19 '16 at 22:01
  • @AnthonyGalli.com, attributes comes from the the class your model is inheriting: `ActiveRecord::Base`. Let's not turn this into a chat. Search about debugging, hint: search for `byebug` or simply learn how to troubleshoot using `puts var_name` (outputting to the rails server output), so on top of errors you can see what values variables hold before the form is rendered. – Leonel Galán Jan 20 '16 at 17:57