【问题标题】:Rails Paperclip upload button not workingRails 回形针上传按钮不起作用
【发布时间】:2017-05-24 19:18:12
【问题描述】:

我尝试将 Paperclip 集成到我的 Web 应用程序中。问题是,上传按钮不起作用。我可以选择一个文件,输入一个标题等,但是当我点击上传时没有任何反应。

当我在 _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>

视频控制器

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

视频.rb

 class Video < ActiveRecord::Base



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

end

【问题讨论】:

  • 你能发布错误/日志吗?

标签: ruby-on-rails ruby rubygems paperclip image-uploading


【解决方案1】:

你必须改变你的代码。

将此添加到您的代码中:

 <%= f.file_field :image %>

而不是这个:

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

【讨论】:

  • 并添加以形成: html: { multipart: true }
  • 谢谢。你的答案就是解决方案!现在可以正常使用了!
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
相关资源
最近更新 更多