Ruby on rails 4 上载不带任何gemfile Rails的图像4
我是RubyonRails新手。 我试图上传没有任何文件的图像。我在跟踪 这是我的books_controller.rb文件:Ruby on rails 4 上载不带任何gemfile Rails的图像4,ruby-on-rails-4,Ruby On Rails 4,我是RubyonRails新手。 我试图上传没有任何文件的图像。我在跟踪 这是我的books_controller.rb文件: class BooksController < ApplicationController before_action :set_book, only: [:show, :edit, :update, :destroy] def index @books = Book.all end def show end def n
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update, :destroy]
def index
@books = Book.all
end
def show
end
def new
@book = Book.new
end
def edit
end
def create
@book = Book.new(book_params)
respond_to do |format|
if @book.save
format.html { redirect_to @book, notice: 'Book was successfully created.' }
format.json { render action: 'show', status: :created, location: @book }
else
format.html { render action: 'new' }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @book.update(book_params)
format.html { redirect_to @book, notice: 'Book was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end
def destroy
@book.destroy
respond_to do |format|
format.html { redirect_to books_url }
format.json { head :no_content }
end
end
def upload
uploaded_io = params[:book][:image_url]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
end
private
def set_book
@book = Book.find(params[:id])
end
def book_params
params.require(:book).permit(:title, :description, :image_url)
end
end
我还创建了一个dirpublic/uploads。
但它不起作用。当我提交创建按钮时,显示如下内容:
TypeError:无法将ActionDispatch::Http::UploadedFile强制转换为字符串:插入到创建的书籍中\u at、说明、图像\u url、标题、更新的\u at值?、?、?、?、?、?、?、
怎么了
谢谢,
Mezbah你发布了你的图书控制器,并在谈论图像?什么图像?尝试上载每本书的图像。这是我的imgaes。我想我的上传操作有问题。请注意,任何advice.Book.rb文件都是空的。只有从ActiveRecord::Basei继承的类在上面添加了schema.rb文件。
<%= form_for @book do |f| %>
<% if @book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% @book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :image_url %><br>
<%= f.file_field :image_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
ActiveRecord::Schema.define(version: 20140724173638) do
create_table "books", force: true do |t|
t.string "title"
t.text "description"
t.string "image_url"
t.datetime "created_at"
t.datetime "updated_at"
end
end