0

The below code prepopulates a new _form, but once the user saves the form it will update the old Inspiration instead of creating a new Inspiration.

controller

  def new
    if params[:inspiration_id] 
      @inspiration = Inspiration.find(params[:inspiration_id]) 
      @inspiration = current_user.inspirations.build # Is there a way to find and then build?
    else 
      @inspiration = current_user.inspirations.build
    end
  end

path

new_inspiration_path(inspiration_id: inspiration.id)

How can we use @inspiration to first Inspiration.find(params[:inspiration_id]) then to current_user.inspirations.build?

Community
  • 1
  • 1
AnthonyGalli.com
  • 2,796
  • 5
  • 31
  • 80
  • Any reason why you are not using `edit` for updating and `new` for just creating? – tejasbubane Feb 26 '16 at 19:00
  • @tejasbubane I want to use `new` for creating. I'm using find in new so that I can find a featured inspiration and then when the user clicks the above path the idea is that the `text` of that featured inspiration will prepopulate the new form. That works. Now once the user saves that prepopulated form it doesn't create a new Inspiration. It edits the featured inspiration, which I don't want. – AnthonyGalli.com Feb 26 '16 at 19:05
  • Does the form post to the same action? I don't think so. It would be better if you edit the question and add your routes corresponding to this controller. – tejasbubane Feb 26 '16 at 19:07
  • Routes are typical `resources :inspirations` so new, edit, update, delete, etc @tejasbubane – AnthonyGalli.com Feb 26 '16 at 19:10

4 Answers4

3

You can use the dup method on the existing record if found. For example

existing_inspiration = Inspiration.find_by_id params[:inspiration_id]

if existing_inspiration
  @inspiration = existing_inspiration.dup
else
  @inspiration = current_user.inspirations.build
end
Bart Jedrocha
  • 11,450
  • 5
  • 43
  • 53
  • Thanks Bart. I'm working on your suggestion now. Question tho. For some reason only the `:name` attribute is duplicated into the _form, but not the `:image` or `:tag_list` attributes. Any idea why? Thanks good sir! – AnthonyGalli.com Feb 26 '16 at 19:28
  • `dup` will only perform a shallow copy of the existing object's attributes. If `:image` and `:tag_list` are associations, they'll need to be assigned manually after the `existing_inspiration.dup` line. – Bart Jedrocha Feb 26 '16 at 19:35
  • Thanks Bart. When you say assign manually do you mean something like `@inspiration.image = params[:image]` & `@inspiration.tag_list = params[:tag_list]`. Sorry – AnthonyGalli.com Feb 26 '16 at 19:42
  • Well if you're trying to copy it over from the `existing_inspiration` then it would be `@inspiration.image = existing_inspiration.image` etc. – Bart Jedrocha Feb 26 '16 at 20:14
  • I can see why `:tag_list` still isn't working since it's an association (it was working in the question code tho), but I can't see why `:image` doesnt work. Maybe because it is kind of redundant to say @inspiration.image = existing_inspiration.image since we made `@inspiration = existing_inspiration.dup`? – AnthonyGalli.com Feb 26 '16 at 20:46
  • It's difficult to say without knowing what `:tag_list` and `:image` actually are and how you're actually setting them in the form. I would suggest you create a new question detailing the above and go from there. Please accept the answer if it answered your original question. Thanks. – Bart Jedrocha Feb 26 '16 at 20:52
  • Good point. Thank you :) I'll continue the conversation over here: http://stackoverflow.com/questions/35664978/how-to-prepopulate-form-with-duplicated-image Hopefully you'll stop by! – AnthonyGalli.com Feb 27 '16 at 02:31
1

The reason your Inspiration is updated is because it is a persisted record. You could extract all attributes except the id and instantiate a new inspiration.

original = Inspiration.find(params[:inspiration_id])
@inspiration = Inspiration.new(original.attributes.except("id"))

I assume that you want to create a copy of an inspiration in order to have some fields prepopulated. Depending on your exact requirements it might or might not be a good idea to copy the inspiration like that (references to other objects, permissions/access, ...)

Pascal
  • 8,464
  • 1
  • 20
  • 31
  • I couldn't get the image attribute with your code, but thanks for trying :) I continued the problem over here: http://stackoverflow.com/questions/35664978/how-to-prepopulate-form-with-duplicated-image. I gave the answer to Bart even though both your answers achieved the same thing because I saw his first (I wish I had a better reason lol sorry) – AnthonyGalli.com Feb 27 '16 at 02:31
0

Im not 100% of your use case as you generally dont pass IDs into new actions. However, you might check out clone if you want a new object based on all of the attributes of the original object.

David Stump
  • 482
  • 2
  • 7
0

I suggest you to redirect to edit action instead of going to the new action to follow rails CRUD structure. Let me know if you need more.

Mohanraj
  • 4,056
  • 2
  • 22
  • 24