简体   繁体   English

Ruby on Rails-如何将图像上传器添加到simple_form Gem

[英]Ruby on Rails - How would I add a image uploader to the simple_form Gem

I'm creating a basic house listing page to get to grips with Rails and I'm trying to work out how I would add a image file uploader to a simple_form gem on Ruby on Rails. 我正在创建一个基本的房屋列表页面,以便与Rails保持联系,而且我正在尝试找出如何在Ruby on Rails上将图像文件上传器添加到simple_form gem上。

Currently, I have a basic form which asks for a title, price and description, whilst I was creating the form I added an input field for image but instead of an input field, I would like to allow a user to add an image file which would be added to the listing with house.image 目前,我有一个基本的表格,要求输入标题,价格和说明,在创建表格时,我为图像添加了一个输入字段,但我想允许用户添加一个图像文件,而不是输入字段将与house.image添加到清单中

My code is as follows: 我的代码如下:

index.html.erb - index.html.erb-

<p><%= link_to "Add a House", new_house_path %></p>

<% @houses.each do |house| %>

  <div class="house">
    <p><%= house.image %></p>
    <h2><%= house.title %></h2>
    <h2><%= house.price %></h2>
    <p><%= house.description %></p>
  </div>
<% end %>

<p><%= paginate @houses %></p>

house.rb - house.rb-

class House < ActiveRecord::Base
    validates :title, presence: true
    validates :price, presence: true
    validates :description, presence: true
    validates :image, presence: true
end

houses_controller.rb - house_controller.rb-

class HousesController < ApplicationController
  def index
    @houses = House.page(params[:page]).per(20).order(created_at: :desc)
  end

  def new
    @house = House.new
  end

  def create
    @house = House.new(params.require(:house).permit(:title, :price, :description, :image))
  if @house.save
    redirect_to root_path
  else
    render "new"
  end
  end

end

new.html.erb - new.html.erb-

<%= simple_form_for @house do |form| %>
 <%= form.input :title, label: "House title" %>
 <%= form.input :price, label: "House price" %>
 <%= form.input :description, label: "Describe your house" %>
 <%= form.input :image, label: "Image of house" %>
 <%= form.button :submit %>
<% end %>

You should be able to change: 您应该可以更改:

<%= form.input :image, label: "Image of house" %>

to

<%= form.input :image, as: :file, label: "Image of house" %>

This will create a standard HTML file input field which allows your users to select local files for upload. 这将创建一个标准的HTML文件输入字段,该字段允许您的用户选择要上传的本地文件。 There are information about this and other field types available Simple Form input field types . 有关此字段和其他字段类型的信息,可以使用“ 简单表单”输入字段类型

Additionally, you will want to consider a gem such as Paperclip or Carrierwave to handle receiving files on the server (in the model). 此外,您将需要考虑使用诸如回形针或Carrierwave之类的宝石来处理服务器(在型号中)上的文件。

And if you really want a custom input type in simple_form, you can read about how to implement that in the simple_form github repo under "Custom Inputs" -- this would NOT be necessary for uploading an image. 而且,如果您确实希望在simple_form中使用自定义输入类型,则可以在“自定义输入”下的simple_form github存储库中了解有关如何实现该类型的信息-上载图像不是必需的。 It would be syntactic sugar for your code. 对于您的代码,这将是语法糖。

For image upload you can use paperclip 对于图像上传,您可以使用回形针

1 : https://github.com/thoughtbot/paperclip or dragonfly 1https//github.com/thoughtbot/paperclip蜻蜓

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM