如何使用重复图像预填充_form?

时间:2016-02-27 02:28:46

标签: javascript ruby-on-rails ruby

用户可以点击featured_inspiration以使用featured_inspiration中的数据预填充新的_form。数据为:text和/或:image。这样,用户就可以将预先存在的Inspiration保存到他自己的Inspirations列表中。

:text正确填写表单,但出于某种原因:image仍为nil

控制器

  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

控制台

[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.

更新

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 %>

1 个答案:

答案 0 :(得分:0)

我想知道你需要做的是这样的事情:

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

离开dup并执行&#34;深度&#34;手工复制。

看看What is the easiest way to duplicate an activerecord record?。它有点陈旧,但是一个启发性的讨论,以及指向一些宝石的指针,这些宝石会让人感到很深沉。副本。