Rails 回形针上传按钮不起作用
Rails Paperclip upload button not working
我尝试将 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>
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
您必须更改密码。
将此添加到您的代码中:
<%= f.file_field :image %>
而不是这个:
<%= form_for @video, url: videos_path, html: { multipart: true } do |form| %>
<%= form.file_field :image %>
<% end %>
我尝试将 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>
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
您必须更改密码。
将此添加到您的代码中:
<%= f.file_field :image %>
而不是这个:
<%= form_for @video, url: videos_path, html: { multipart: true } do |form| %>
<%= form.file_field :image %>
<% end %>