- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的 Controller :
class ArticlesController < ApplicationController
def index
@articles = Article.organise
end
def show
@article = Article.find(params[:id])
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
@article.update(article_params)
redirect_to @article
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :brief, :posted_by, :posted_at, :posted_from, :image_file_name)
end
end
我的看法:
<p>
<%= f.label :Image %><br/>
<%= f.file_field :image_file_name %>
</p>
我的模型:
class Article < ActiveRecord::Base
def self.organise
order("posted_at desc")
end
end
我在 Controller 中的参数:
def article_params
params.require(:article).permit(:title, :brief, :posted_by, :posted_at, :posted_from, :image_file_name)
end
正在返回此错误,我认为它与 Controller 参数有关,但我不太确定。
ActiveRecord::StatementInvalid in ArticlesController#create
TypeError: can't cast ActionDispatch::Http::UploadedFile to string: INSERT INTO "articles" ("brief", "created_at", "image_file_name", "posted_at", "posted_by", "posted_from", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)
最佳答案
我相信您没有使用任何 gem 进行文件上传。
<%= form_for @article, {},:html => {:multipart => true } do |f| %>
<p>
<%= f.label :Image %><br/>
<%= f.file_field 'file' %>
<%= f.submit "Upload" %>
</p>
<% end %>
将此添加到您的创建操作中并上传到相同的服务器位置/assets/images
if params[:article].present?
file = params[:article][:file]
File.open(Rails.root.join('app','assets', 'images', file.original_filename), 'wb') do |f|
f.write(file.read)
end
end
关于ruby-on-rails - Rails file_field 不上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23360919/
是否可以为 file_field 助手生成的按钮提供您自己的标题? 最佳答案 您可以针对您的情况检查此答案: How to change the button text of ? 答案将向您展示如何在
如何在 ruby on rails 中自定义文件字段“浏览”按钮。 cutomise 从某种意义上说,我想更改按钮的颜色和样式。这在 ROR 中可能吗?如果是这样,任何人都可以帮助我.... 最佳
使用回形针和 ruby-mp3info 上传和读取文件信息,我有一个创建播放列表及其子轨道的表单: prohibited this playlist from b
我目前有以下上传图片的方法,但我还想添加一个限制,以防止用户上传任何高度小于50px的文件。这可能吗? file_field "form", "image", accept: 'image/png,i
你看到file_field提供了一个窗口让用户选择某个文件并上传到服务器端。但是我想要的只是文件名。我怎么能只得到文件名,我不需要文件本身。有什么建议吗? 最佳答案 不确定你为什么需要这个,但你可以试
我有一个按钮,我想执行由 Rails 中的 file_field 生成的操作。这是我现在拥有的 erb 代码: > Upload from computer 我遵循了 thi
我正在使用带有 Carrier Wave 的 Rails 4 和 Ruby 2 以及 jQuery Mobile 1.3,并且用户有一个带有 Logo 的配置文件。我已经在控制台中测试了后端 Carr
在我的模型中,我使用回形针将头像附加到我的用户身上: has_attached_file :avatar, :styles => {square_tiny: '50x50#', square_
我想更改 file_field 的 css。而不是显示原始浏览按钮。我想使用简单的上传按钮来上传文件。 我如何更改 file_field 的 css 并在其上设置另一个图像?所以它看起来像一个按钮而不
我的 Controller : class ArticlesController 我的模型: class Article {:multipart => true
我有一个非常标准的 Rails 表单: Create a New Listing {:multipart => true} do |f| -%>
我正在尝试使用 PaperClip 并一直在关注他们 GitHub 页面上的快速入门。 我已经安装了 gem、模型和 Controller ,在 View 中我有: { :multipart =>
在包含带有选择文件输入的表单的网页上工作,如以下代码段所示: Import: Choose File ... 使用 Watir WebDriver,以下返
我的 file_field 标签只是将文件名而不是文件本身传递给 Controller 。 在我的用户模型中: mount_uploader :avatar, AvatarUploader val
我正在使用 Railscast 197 中找到的代码来使用 jQuery 创建嵌套表单。我面临的挑战是嵌套模型使用 file_field 元素,因此在查看 edit View 时我只看到空 file_
具有以下形式: = form_for company, html: { multipart: true } do |form| = form.label :logo, 'Upload Imag
我的系统: Windows 10 Pro 64-bit ruby 2.1.9p490 (2016-03-30 revision 54437) [x64-mingw32] FireFox 47.0.1
升级到 Rails 4.2 后,我将上传的文件保存到数据库 ( pg ) 的代码失败并显示错误消息:“字符串包含空字节”。 我创建了一个只有一个模型和一个二进制字段的新 Rails 4.2 应用程序。
我在 Rails 中遇到了文件上传问题。我找到了 file_field :file助手,可以与 form_for(@some_model) 一起使用.但是,我找不到这种情况的任何用法,因为这些标签用于
使用 nested_form_for 时并且有一个 file_field在fields_for我从回形针中收到以下错误: Paperclip::AdapterRegistry::NoHandlerEr
我是一名优秀的程序员,十分优秀!