Paper clip: missing image

I am working on a website that allows people who run a bed and breakfast business to post their rooms.

I would like to require them to include a “profile image” of posting when they publish it, but I also want to give them the opportunity to add more images later (this will be developed later).

I thought that the best thing that can be done is to use the Paperclip gem and have the “Accommodation and Photos” application in it, later owned by the former as an association.

A new photo is created when the object is created. It has id and accommodation_id attributes. However, the image never loads, and none of the Paperclip attributes are set (image_file_name: nil, image_content_type: nil, image_file_size: nil), so I get a “missing” Paperclip photo.

Any ideas on this? It held me back for several days.

Accommodation

models /accommodation.rb

class Accommodation < ActiveRecord::Base
  validates_presence_of :title, :description, :photo, :thing, :location
  attr_accessible :title, :description, :thing, :borough, :location, :spaces, :price
  has_one :photo
end

Controllers / accommodation _controller.erb

class AccommodationsController < ApplicationController

  before_filter :login_required, :only => {:new, :edit}

  uses_tiny_mce ( :options => {
    :theme => 'advanced',
    :theme_advanced_toolbar_location => 'top',
    :theme_advanced_toolbar_align => 'left',
    :theme_advanced_buttons1 => 'bold,italic,underline,bullist,numlist,separator,undo,redo',
    :theme_advanced_buttons2 => '',
    :theme_advanced_buttons3 => ''
  })

  def index
    @accommodations = Accommodation.all
  end

  def show
    @accommodation = Accommodation.find(params[:id])
  end

  def new
    @accommodation = Accommodation.new
  end

  def create
    @accommodation = Accommodation.new(params[:accommodation])
    @accommodation.photo = Photo.new(params[:photo])
    @accommodation.user_id = current_user.id
    if @accommodation.save
      flash[:notice] = "Successfully created your accommodation."
      render :action => 'show'
    else
      render :action => 'new'
    end
  end

  def edit
    @accommodation = Accommodation.find(params[:id])
  end

  def update
    @accommodation = Accommodation.find(params[:id])
    if @accommodation.update_attributes(params[:accommodation])
      flash[:notice] = "Successfully updated accommodation."
      render :action => 'show'
    else
      render :action => 'edit'
    end
  end

  def destroy
    @accommodation = Accommodation.find(params[:id])
    @accommodation.destroy
    flash[:notice] = "Successfully destroyed accommodation."
    redirect_to :inkeep
  end
end

view / post / _form.html.erb

<%= form_for @accommodation, :html => {:multipart => true} do |f| %>
  <%= f.error_messages %>
  <p>
    Title<br />
    <%= f.text_field :title, :size => 60 %>
  </p>
  <p>
    Description<br />
    <%= f.text_area :description, :rows => 17, :cols => 75, :class => "mceEditor" %>
  </p>
  <p>
    Photo<br />
    <%= f.file_field :photo %>
  </p>
  [... snip ...]
 <p><%= f.submit %></p>
<% end %>

Photo

The controller and views are the same as when creating Rails.

models /photo.erb

class Photo < ActiveRecord::Base
  attr_accessible :image_file_name, :image_content_type, :image_file_size
  belongs_to :accommodation
  has_attached_file :image,
    :styles => {
      :thumb=> "100x100#",
      :small  => "150x150>" }
end
+3
source share
1 answer

paperclip, , has_attached_file, , . :

<%= form_for @accommodation, :html => { :multipart => true } do |f| %>
  <%= f.fields_for :photo do |photo_fields| %>
    <p>
      Photo<br />
      <%= photo_fields.file_field :image %>
    </p>
  <% end %>
<% end %>

:

class AccommodationsController < ApplicationController

  # also protect create and update actions!
  before_filter :login_required, :only => [ :new, :create, :edit, :update ]

  def new
    # always make objects through their owner
    @accommodation = current_user.accommodations.build
    @accommodation.build_photo
  end

  def create
    @accommodation = current_user.accommodations.build(params[:accommodation])
    if @accommodation.save
       # always redirect after successful save/update
       redirect_to @accommodation
    else
       render :new
    end
  end
end

Rails :

class Accommodation
  has_one :photo
  accepts_nested_attributes :photo
  attr_accessible :photo_attributes, :title, :description, :etc
end

:

class Photo
  attr_accessible :image # individual attributes such as image_file_name shouldn't be accessible
  has_attached_file :image, :styles => "etc"
end

, , attr_accessible, .

+2

Source: https://habr.com/ru/post/1781548/


All Articles