2017-02-20 84 views
0

我正在Rails教程中粗略地研究Rails中的一个小项目。我有Carrierwave,Fog和AWS在上传单张图像方面的出色工作。我想添加一个图片库,所以我想我会遵循SSR的链接回答:Rails 4 multiple image or file upload using carrierwaveRails Carrierwave多张图片未发布

一切似乎对我的模型很好,测试甚至通过,但是当我去创建或编辑一个上市,上传者似乎没有工作。它会挂起几分钟而不解决(即使是小文件大小),并且控制台不会读取提交时发布的操作(刚刚读取表单本身的最后一个GET为200)。除了命名约定(我使用Listing作为Post模型和listing_gallery而不是post_attachment),步骤是相同的​​。 Rails 5是否改变了Carrierwave的一切?

listing.rb

class Listing < ApplicationRecord 
VALID_PHONE_REGEX = /(\(*(\d{3})\)|(\d{3}-)|(\d{3}))\s*(\d{3})-*(\d{4})/ 
VALID_ZIP_REGEX = /\d{5}((-)?\d{4})?/ 

# One use has many listings 
belongs_to :user 

# Listing_gallery 
has_many :listing_galleries 
accepts_nested_attributes_for :listing_galleries 

# Descending order from newest to oldest as default 
default_scope -> { order(created_at: :desc) } 

# Carrier Wave mount uploader for images 
mount_uploader :picture, PictureUploader 
mount_uploader :image, PictureUploader 

# Validation of parameters before accepting listing 
validates :user_id, presence: true 
validates :description, presence: true 
validates :street_address, presence: true 
validates :city, presence: true, length: { maximum: 30 } 
validates :zip_code, presence: true, length: {minimum: 5, maximum: 10}, format: { with: VALID_ZIP_REGEX } 
validates :primary_contact, presence: true, length: {minimum: 10, maximum: 15}, format: { with: VALID_PHONE_REGEX } 


end 

listings_controller.rb

class ListingsController < ApplicationController 
before_action :logged_in_user, only: [:new, :create, :edit, :update, :destroy] 
before_action :correct_user, only: [:edit, :update, :destroy] 

def new 
    @listing = Listing.new 
    @listing_gallery = @listing.listing_galleries.build 
end 

def create 
    if current_user.admin? 
     @listing = current_user.listings.build(listing_params) 
     @listing.listing_status = "active" 

     respond_to do |format| 
      [email protected]! 
       params[:listing_galleries]['image'].each do |image| 
        @listing_gallery = @listing.listing_galleries.create!(:image => image) 
       end 

       flash[:success] = "New Listing created!" 
       redirect_to root_url 
      else 
       render 'new' 
      end 
     end 
    else 
     flash[:failure] = "You must be an admin to create a listing." 
     redirect_to root_url 
    end 
end 

def show 
    @listing = Listing.find(params[:id]) 
    @listing_galleries = @listing.listing_galleries.all 
end 

def edit 
    if current_user.admin? 
     @listing = Listing.find(params[:id]) 
     render 'edit' 
    end 
end 

def update 
    if current_user.admin? 
     @listing = Listing.find(params[:id]) 

     respond_to do |format| 
      if @listing.update_attributes(listing_params) 
       if params[:listing_galleries] != [] 
        params[:listing_galleries]['image'].each do |image| 
         @listing_gallery = @listing.listing_galleries.create!(:image => image) 
        end 
       end 

       flash[:success] = "Listing Updated!" 
       redirect_to @listing 
      else 
       render 'edit' 
      end 
     end 
    end 
end 

def destroy 
    if current_user.admin? 
     @listing.listing_status = "deleted" 

     if @listing.save! 
      flash[:success] = "Listing DELETED" 
      redirect_to request.referrer || root_url 
     else 
      flash[:failure] = "Could not remove listing" 
      redirect_to request.referrer || root_url 
     end 
    end 
end 

private 

# Check for necessary paramters 
def listing_params 
    params.require(:listing).permit(:description, :street_address, :city, :state, 
            :zip_code, :primary_contact, :secondary_contact, 
            :listing_status, :asking_price, :renobb, :picture, 
            { image: [] }, 
            listing_galleries_attributes: [:id, :listing_id, :image, :_destroy]) 
end 

# Ensure only the creator of the listing is destroying it 
def correct_user 
    @listing = current_user.listings.find_by(id: params[:id]) 

    if current_user.admin? 
     return true 
    end 

    redirect_to root_url if @listing.nil? 
end 

end 

列表/ new.html.erb

<% provide(:title, 'Create Listing') %> 

<div id="content"> 

<div id="sectionbanner"> 
    <p class="goldtext"> 
     Create a New Listing 
    </p> 
</div> 

<!-- Article --> 
<div id="article"> 

    <div class="form"> 
     <%= form_for(@listing, html: { multipart: true}) do |form| %> 
      <%= render 'shared/error_messages_listings' %> 

      <%= form.label :street_address %> 
      <br> 
      <%= form.text_field :street_address, class: 'form-control' %> 

      <br> 
      <%= form.label :city %> 
      <br> 
      <%= form.text_field :city, class: 'form-control' %> 

      <br> 
      <%= form.label :state %> 
      <br> 
      <%= form.text_field :state, class: 'form-control' %> 

      <br> 
      <%= form.label :zip_code %> 
      <br> 
      <%= form.text_field :zip_code, class: 'form-control' %> 

      <br> 
      <%= form.label :primary_contact %> 
      <br> 
      <%= form.text_field :primary_contact, class: 'form-control' %> 

      <br> 
      <%= form.label :secondary_contact %> 
      <br> 
      <%= form.text_field :secondary_contact, class: 'form-control' %> 

      <br> 
      <%= form.label :asking_price %> 
      <br> 
      <%= form.number_field :asking_price, class: 'form-control' %> 

      <br> 
      <%= form.label :description %> 
      <br> 
      <%= form.text_area :description, placeholder: "Details of the lot, rules, etc..." %> 

      <br> 
      <%= form.label :renobb %> 
      <br> 
      <%= form.check_box :renobb %> 

      <br> 
      <span class="picture"> 
       <%= form.file_field :picture, accept: 'image/jpeg, image/gif, image/png' %> 
      </span> 

      <br> 
      <%= form.label :image %> 
      <br> 
      <%= form.file_field :image, :multiple => true, name: "listing_galleries[image][]" %> 

      <br> 
      <%= form.submit "Create Listing", class: "button" %> 

     <% end %> 
    </div> 

</div> 

</div> 

listing_gallery.rb

class ListingGallery < ApplicationRecord 

belongs_to :listing 

mount_uploader :image, PictureUploader 


end 
从控制器和视图

listing_galleries_controller.rb

class ListingGalleriesController < ApplicationController 
before_action :set_listing_gallery, only: [:show, :edit, :update, :destroy] 

# GET /listing_galleries 
# GET /listing_galleries.json 
def index 
@listing_galleries = ListingGallery.all 
end 

# GET /listing_galleries/1 
# GET /listing_galleries/1.json 
def show 
end 

# GET /listing_galleries/new 
def new 
@listing_gallery = ListingGallery.new 
end 

# GET /listing_galleries/1/edit 
def edit 
end 

# POST /listing_galleries 
# POST /listing_galleries.json 
def create 
@listing_gallery = ListingGallery.new(listing_gallery_params) 

respond_to do |format| 
    if current_user.admin? 
    if @listing_gallery.save 
     format.html { redirect_to @listing_gallery, notice: 'Listing gallery was successfully created.' } 
     format.json { render :show, status: :created, location: @listing_gallery } 
    else 
     format.html { render :new } 
     format.json { render json: @listing_gallery.errors, status: :unprocessable_entity } 
    end 
    end 
end 
end 

# PATCH/PUT /listing_galleries/1 
# PATCH/PUT /listing_galleries/1.json 
def update 
respond_to do |format| 
    if current_user.admin? 
    if @listing_gallery.update(listing_gallery_params) 
     format.html { redirect_to @listing_gallery, notice: 'Listing gallery was successfully updated.' } 
     format.json { render :show, status: :ok, location: @listing_gallery } 
    else 
     format.html { render :edit } 
     format.json { render json: @listing_gallery.errors, status: :unprocessable_entity } 
    end 
    end 
end 
end 

# DELETE /listing_galleries/1 
# DELETE /listing_galleries/1.json 
def destroy 
    if current_user.admin? 
    @listing_gallery.destroy 
    respond_to do |format| 
    format.html { redirect_to listing_galleries_url, notice: 'Listing gallery was successfully destroyed.' } 
    format.json { head :no_content } 
    end 
end 
end 

private 
# Use callbacks to share common setup or constraints between actions. 
def set_listing_gallery 
    @listing_gallery = ListingGallery.find(params[:id]) 
end 

# Never trust parameters from the scary internet, only allow the white list through. 
def listing_gallery_params 
    params.require(:listing_gallery).permit(:listing_id, :image, { image: [] }, :_destroy) 
end 
end 

EDIT更多文件。 (对齐可能对SO很时髦,但在项目中没问题。)

回答

0

我真的不知道这是否是错字,但是您的问题出现在您的视图的第1行。

%= form_for(@listing, html: { mulipart: true}) do |form| %>

你也失踪多部分的拼写,我想是你不能上传多张图片的原因。

将其更改为:

<%= form_for(@listing, html: { multipart: true}) do |form| %>

要了解更多详情,请参阅Carrierwave文档的Multiple File Uploads部分。

+0

非常感谢您的关注。这并没有带来帮助,但我相信它会带来新的问题。 –

0

多种图片上传功能,您需要将这个宝石添加到您的Gemfile

gem 'carrierwave', github: 'carrierwaveuploader/carrierwave' 

,并允许PARAMS作为一个开放的阵列控制器:{image: []}了解更多详情 https://github.com/carrierwaveuploader/carrierwave

检查宝石文档您似乎也有一个上传器'图像'你没有安装在你的模型中。您只能安装'图片'

+0

添加了gem,运行了捆绑包安装,没有错误,并且添加了您在我能想到的控制器之间的每个组合中指出的注释,并且它的表现仍然相同。 x.x我欣赏提示。任何其他想法? –

+0

你得到什么错误或发生了什么?你能否在这里更新你的模型和控制器,以便我们看到你所做的改变?如果您进行了更改,也请更新表格并且可能是上传者。你使用哪个参数来进行多文件上传? –

+0

更新了文本与更改(尽管,我试图添加/删除:图像每一种方式)。即使在命令行上,我也没有得到任何回应。它只是挂起,就像它试图上传图像一样。跟随两个指令集几乎都是T.谢谢你看到这一切。 –