Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo questions on paperclip with multiple uploads
    primarykey
    data
    text
    <p>I'm making a task for a job. Rails 4 app where users can create posters. Posters can have multiple images to upload. Actually, I have no explicit errors, just two questions. But before questions, here are my files. Poster.rb:</p> <pre><code>class Poster &lt; ActiveRecord::Base has_many :poster_images, dependent: :destroy accepts_nested_attributes_for :poster_images, allow_destroy: true belongs_to :type belongs_to :user default_scope -&gt; { order('created_at DESC') } validates :title, :body, :publish_date, :user_id, :presence =&gt; true end </code></pre> <p>poster_image.rb:</p> <pre><code>class PosterImage &lt; ActiveRecord::Base belongs_to :poster has_attached_file :image, :styles =&gt; {:medium =&gt; "300x300&gt;", :thumb =&gt; "100x100&gt;" } end </code></pre> <p>posters_controller.rb:</p> <pre><code>class PostersController &lt; ApplicationController before_filter :set_poster, only: [:show, :edit, :update, :destroy] before_filter :authenticate_user!, :except =&gt; [:index, :show] authorize_resource load_resource except: :create # GET /posters # GET /posters.json def index @posters = Poster.paginate(page: params[:page], :per_page =&gt; 10) end # GET /posters/1 # GET /posters/1.json def show end # GET /posters/new def new end # GET /posters/1/edit def edit end # POST /posters # POST /posters.json def create @poster = Poster.new(poster_params) @poster.user_id = current_user.id respond_to do |format| if @poster.save format.html { redirect_to @poster, notice: 'Poster was successfully created.' } format.json { render action: 'show', status: :created, location: @poster } else format.html { render action: 'new' } format.json { render json: @poster.errors, status: :unprocessable_entity } end end end # PATCH/PUT /posters/1 # PATCH/PUT /posters/1.json def update respond_to do |format| if @poster.update(poster_params) format.html { redirect_to @poster, notice: 'Poster was successfully updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @poster.errors, status: :unprocessable_entity } end end end # DELETE /posters/1 # DELETE /posters/1.json def destroy @poster.destroy respond_to do |format| format.html { redirect_to posters_url } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_poster @poster = Poster.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def poster_params params.require(:poster).permit(:title, :body, :publish_date, :type_id, :type_name, poster_images_attributes: :image ) end end </code></pre> <p>_form.html.erb: </p> <pre><code>&lt;%= simple_nested_form_for @poster, :html =&gt; {:multipart =&gt; true } do |f| %&gt; &lt;%= f.input :title, :autofocus =&gt; true %&gt; &lt;%= f.input :body %&gt; &lt;%= f.input :publish_date %&gt; &lt;%= f.input :type_id, :collection =&gt; Type.all, required: true %&gt; &lt;%= f.fields_for :poster_images do |images_f| %&gt; &lt;%= images_f.file_field :image %&gt; &lt;%= images_f.link_to_remove "X" %&gt; &lt;% end %&gt; &lt;%= f.link_to_add "Add image", :poster_images %&gt; &lt;div class="form-actions"&gt; &lt;%= f.button :submit, :class =&gt; 'btn-primary' %&gt; &lt;%= link_to t('.cancel', :default =&gt; t("helpers.links.cancel")), posters_path, :class =&gt; 'btn' %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>show.html.erb:</p> <pre><code>&lt;%- model_class = Poster -%&gt; &lt;div class="page-header"&gt; &lt;h1&gt;&lt;%=t '.title', :default =&gt; model_class.model_name.human.titleize %&gt;&lt;/h1&gt; &lt;/div&gt; &lt;dl class="dl-horizontal"&gt; &lt;dd&gt;&lt;h3&gt;&lt;%= @poster.title %&gt;&lt;/h3&gt;&lt;/dd&gt; &lt;dd&gt;&lt;%= @poster.body %&gt;&lt;/dd&gt; &lt;dt&gt;&lt;strong&gt;&lt;%= model_class.human_attribute_name(:publish_date) %&gt;:&lt;/strong&gt;&lt;/dt&gt; &lt;dd&gt;&lt;%= @poster.publish_date %&gt;&lt;/dd&gt; &lt;dt&gt;&lt;strong&gt;Author:&lt;/strong&gt;&lt;/dt&gt; &lt;dd&gt;&lt;%= link_to @poster.user.name, @poster.user %&gt;&lt;/dd&gt; &lt;dt&gt;&lt;strong&gt;Type:&lt;/strong&gt;&lt;/dt&gt; &lt;dd&gt;&lt;%= @poster.type.name %&gt;&lt;/dd&gt; &lt;/dl&gt; &lt;dt&gt;&lt;strong&gt;Image(s):&lt;/strong&gt;&lt;/dt&gt; &lt;% @poster.poster_images.each do |p| %&gt; &lt;%= content_tag "p_#{p.id}" do %&gt; &lt;%= image_tag p.image.url(:medium) %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;div class="form-actions"&gt; &lt;%= link_to t('.back', :default =&gt; t("helpers.links.back")), posters_path, :class =&gt; 'btn' %&gt; &lt;% if current_user &amp;&amp; (current_user.has_role?(:admin) || current_user.id==@poster.user_id) %&gt; &lt;% if can? :update, Poster %&gt; &lt;%= link_to t('.edit', :default =&gt; t("helpers.links.edit")), edit_poster_path(@poster), :class =&gt; 'btn' %&gt; &lt;% end %&gt; &lt;%= link_to t('.destroy', :default =&gt; t("helpers.links.destroy")), poster_path(@poster), :method =&gt; 'delete', :data =&gt; { :confirm =&gt; t('.confirm', :default =&gt; t("helpers.links.confirm", :default =&gt; 'Are you sure?')) }, :class =&gt; 'btn btn-danger' %&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p>And now two my questions:</p> <ol> <li><p>When I'm creating the poster, everything goes great. But when I'm trying to update my poster and push "edit", my form is coming in and I see all the fields that I had when I was creating poster. But if while CREATING poster I added a pic, when I see EDIT form, I have opened link for adding a pic. And if I use it, it's ok, but when I just edit my Name,for example, and save it without adding new pic, my "show.html.erb" shows me my previous pic and a sign of "broken" image after it. If I had two images, while updating poster, rails is trying to make me to add two more images or adds two broken-image signs itself. Any ideas?(if you understood what you've read above)</p></li> <li><p>The person who is checking my app says that I forgot to add some keys in my "poster_images_attributes: :image" in the controller in "poster_params". I found here, in SOF, that it should be enough, but he says that it's enough for creating poster, but not for edit and delete actions. Any ideas what else should I write in poster_params? Thanks</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload