0

A user can click on a featured_inspiration to prepopulate a new _form with the data from that featured_inspiration. The data being :text and/or :image. This is so the user can save a preexisting Inspiration to his own list of Inspirations.

:text correctly prepopulates the form, but for some reason :image remains nil.

controller

  def new
    existing_inspiration = Inspiration.find_by_id params[:inspiration_id]
    if existing_inspiration
      @inspiration = existing_inspiration.dup
           ### Attempt at Making It Works. Had No Effect. ###    
      @inspiration.image_file_name = existing_inspiration.dup.image_file_name
      @inspiration.image_content_type = existing_inspiration.dup.image_content_type #Attempt at Making It Works. Had No Effect.    
      @inspiration.image_file_size = existing_inspiration.dup.image_file_size
      @inspiration.image_updated_at = existing_inspiration.dup.image_updated_at
                            ##############
    else
      @inspiration = current_user.inspirations.build
    end
  end

Console

[41] pry(main)> Inspiration.last
  Inspiration Load (1.7ms)  SELECT  "inspirations".* FROM "inspirations"  ORDER BY "inspirations"."id" DESC LIMIT 1
=> #<Inspiration:0x007fa6d15ec9a8
 id: 13,
 conceal: false,
 user_id: 8,
 created_at: Fri, 26 Feb 2016 15:29:52 EST -05:00,
 updated_at: Fri, 26 Feb 2016 15:29:52 EST -05:00,
 likes: nil,
 name: "",
 image_file_name: nil, #SHOULD BE: "inspiring-quotes-about-life-6gbwrvxd.jpg", 
 image_content_type: nil, #SHOULD BE: "image/jpeg",
 image_file_size: nil, #SHOULD BE: 83348
 image_updated_at: nil> #SHOULD BE: Wed, 24 Feb 2016 15:25:44 EST -05:00>

_form

<%= simple_form_for(@inspiration, html: { data: { modal: true } }) do |f| %> 
  <%= f.file_field :image %>
  <%= f.text_area :text %>
<% end %>

Question that got duplicating text to work.

UPDATE

suggested_inspirations

<% @inspirations.each do |inspiration| %>
  <%= link_to inspiration_path(inspiration) do %>
    <div class="box panel panel-default" style="width: 175px;">
      <% if inspiration.image.present? %>
        <div id="box">
          <%= link_to new_inspiration_path(inspiration_id: inspiration.id), data: { modal: true } do %>
            <div class="inspiration-image-button"><span class="glyphicon glyphicon-plus"></span></div>
          <% end %>
          <%= link_to image_tag(inspiration.image.url(:medium)), inspiration %> #This is how image is pulled
        </div>
      <% end %>
      <% if inspiration.name.present? %>
        <div class="panel-body">
          <%= link_to new_inspiration_path(inspiration_id: inspiration.id), data: { modal: true } do %>
            <div class="inspiration-button"><span class="glyphicon glyphicon-plus"></span></div>
          <% end %>
          <%= inspiration.name %>
        </div>
      <% end %>
    </div>
  <% end %>
<% end %>
Community
  • 1
  • 1
AnthonyGalli.com
  • 2,796
  • 5
  • 31
  • 80

1 Answers1

0

I wonder what you need to do is something like this:

@inspiration = existing_inspiration.dup
@inspiration.image_file_name = existing_inspiration.image_file_name

Leaving out the dupand doing the "deep" copies by hand.

Take a look at What is the easiest way to duplicate an activerecord record?. It is a bit old, but an enlightening discussion as well as pointers to some gems that make "deep" copies.

Community
  • 1
  • 1
dlu
  • 749
  • 10
  • 27
  • Thanks dlu. I took at look at the question. I tried the `clone` suggestion there too, plus your selection of leaving `dup` out, but for some reason that image won't duplicate/clone/upload into a new _form. – AnthonyGalli.com Feb 28 '16 at 20:22
  • Could you put `binding.pry` right after the two lines above and then take a look at what is actually happening when you try to create the new derivative inspiration? It really seems like it should work… – dlu Feb 28 '16 at 20:26
  • Okay I did `binding.pry`. What should I be looking for? Sorry never used this before. Do I write something in the terminal after `[1] pry(#)>`? – AnthonyGalli.com Feb 28 '16 at 20:30
  • So, now you're in the context that you're interested in and you can try out some code to see what it will do and also inspect the results that you're getting. I'd start by trying something like this: `existing_inspiration.image_file_name` and I'd expect it to return the name of the file. If it does, then the assignment should also work. Type control-d to continue and move the binding pry into the create action. There you can take a look and make sure that you've got the values that you expect in the new inspiration. – dlu Feb 28 '16 at 20:37
  • I'm suspicious that you may be getting a `nil` value from `existing_inspiration.image_file_name` and we'll need to figure out why. Can an inspiration have multiple images? – dlu Feb 28 '16 at 20:38
  • `pry(#)> existing_inspiration.image_file_name => "inspiring-quotes-about-life-6gbwrvxd.jpg"` – AnthonyGalli.com Feb 28 '16 at 20:40
  • when I did control-d though it didn't create the new record – AnthonyGalli.com Feb 28 '16 at 20:41
  • Ok. That's good, that seems to be what you'd expect, so the problem doesn't seem to be in the new action. The control-d gets you out of the pry session and lets your app continue running. So now, go look in the create action. Put a binding pry there and check that you come in with the values that you'd expect. Is it possible that in your form you clear out the defaults some how? Basically, I would proceed by checking at each step along the way that I'm seeing the values that I expect. At some point you'll discover a surprise… – dlu Feb 28 '16 at 21:00
  • My guess is that `<%= f.file_field :image %>` for some reason doesn't accept it whereas `<%= f.text_area :name %>` does accept it. `file_field` might not accept it because maybe for it to work you need to manually click the button and then choose a file and its default overrides anything that is not manually uploaded. It doesn't have to do with create because before I even click create I see that `file_field` says `No file chosen` in the _form. – AnthonyGalli.com Feb 28 '16 at 21:10
  • Idk I put it in create after save with `@inspiration.save binding.pry`. I got `pry(#)> existing_inspiration.image_file_name` to show `NameError: undefined local variable or method 'existing_inspiration' for # from (pry):3:in 'create'` – AnthonyGalli.com Feb 28 '16 at 21:15
  • Or `pry(#)> @inspiration.image_file_name => nil` – AnthonyGalli.com Feb 28 '16 at 21:15
  • I don't have time to dig, right now, but I think you may be right about the behavior of f.file_field. So, look at the docs (I like the Rails Guides) to see how you can set the default value. I'll be there is a way. Also search SO for a method. That seems likely to do it… – dlu Feb 28 '16 at 22:36