2017-01-10 2 views
0

Я пытаюсь интегрировать Paperclip в свое веб-приложение. Проблема в том, что кнопка загрузки не работает. Я могу выбрать файл, ввести название и т. Д., Но когда я нажимаю на загрузку, ничего не происходит.Rails Кнопка загрузки скрепки не работает

, когда я удалить в файле _form.html.erb кнопка загрузки

<%= form_for @video, url: videos_path, html: { multipart: true } do |form| %> 


<%= form.file_field :image %> 



<% end %> 

снова работает, но опять же, без привязанности скрепки.

Любые идеи, как исправить это?

_form.html.erb Файл

<div class="container"> 

<%= form_for(@video) do |f| %> 
    <% if @video.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@video.errors.count, "error") %> prohibited this video from being saved:</h2> 

     <ul> 
     <% @video.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :jwPlayer %><br> 
    <%= f.text_field :jwPlayer %> 
    </div> 
    <div class="field"> 
    <%= f.label :description %><br> 
    <%= f.text_area :description %> 
    </div> 
    <div class="field"> 
    <%= f.label :title %><br> 
    <%= f.text_area :title %> 
    </div> 
    <%= form_for @video, url: videos_path, html: { multipart: true } do |form| %> 
    <%= form.file_field :image %> 
    <% end %> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 
</div> 

Video_controller

class VideosController < ApplicationController 
    before_action :set_video, only: [:show, :edit, :update, :destroy] 

    # GET /videos 
    # GET /videos.json 
    def index 
    @videos = Video.all 
    end 

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

    # GET /videos/new 
    def new 
    @video = Video.new 
    end 

    # GET /videos/1/edit 
    def edit 
    end 

    # POST /videos 
    # POST /videos.json 
    def create 
    @video = Video.new(video_params) 

    respond_to do |format| 
     if @video.save 
     format.html { redirect_to @video, notice: 'Video was successfully created.' } 
     format.json { render :show, status: :created, location: @video } 
     else 
     format.html { render :new } 
     format.json { render json: @video.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /videos/1 
    # PATCH/PUT /videos/1.json 
    def update 
    respond_to do |format| 
     if @video.update(video_params) 
     format.html { redirect_to @video, notice: 'Video was successfully updated.' } 
     format.json { render :show, status: :ok, location: @video } 
     else 
     format.html { render :edit } 
     format.json { render json: @video.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /videos/1 
    # DELETE /videos/1.json 
    def destroy 
    @video.destroy 
    respond_to do |format| 
     format.html { redirect_to videos_url, notice: 'Video was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

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

    # Never trust parameters from the scary internet, only allow the white list through. 
    def video_params 
     params.require(:video).permit(:jwPlayer, :description, :title, :image) 
    end 
end 

Video.rb

class Video < ActiveRecord::Base 



    has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" } 
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/ 

end 
+0

Можете ли вы отправить ошибка/журналы? –

ответ

0

Вы должны тя nge ваш код.

Добавьте это в ваш код:

<%= f.file_field :image %> 

вместо этого:

<%= form_for @video, url: videos_path, html: { multipart: true } do |form| %> 
    <%= form.file_field :image %> 
<% end %> 
+0

и добавьте в форму: html: {multipart: true} –

+0

Спасибо. Ваш ответ был решением! Теперь он отлично работает! – Prometheus

Смежные вопросы