无法从表单中存储数据

时间:2016-09-30 10:03:29

标签: ruby-on-rails ruby

我试图将数据存储在rails中,但我根本不能,每次按下提交按钮时,base上的值都为null,方法save在我按下提交按钮之前尝试存储数据,这是我的代码:

images_controller.rb:

class ImagesController < ApplicationController
  layout 'home2'
  #before_action :image
  def new
    @image = Image.new
  end

  def create
    @image = Image.new(image_params)

    if @image.save
      flash[:success] = "Your creation has been uploaded"
      #redirect_to "/showcase"
    else
      flash[:error] = "Your creation has not been uploaded"
      render :new
    end
  end

    private

    def image_params
      params.permit(:title, :description, :nickname, :creation)
    end
end

我的观点create.html.erb:

<h2 align="center">Post your own creation right here !</h2>
<br>
<div align="center">
  <%= link_to 'Back', '/showcase', :class => "buttonShow" %>
</div>

<%= simple_form_for @image, url: "/showcase/post", html: { multipart: true } do |f| %>
  <%= f.input :title, :required => true %>
  <%= f.input :description, :required => true %>
  <%= f.input :nickname, :required => true %>
  <div align="center">
  <%= f.file_field :creation, :required => true %>
    <%= f.button :submit, "Post !", :class => "buttonShow1"  %>
  </div>
<% end %>

route.rb:

Rails.application.routes.draw do
  resources "images", only: [:create, :new]
  resources "contacts", only: [:new, :create]
  get 'contacts/contact'

  #get 'images/crea'
  #get 'images/post'

  get 'auth/auth'

  root "auth#auth"
  #get 'contact' => 'contacts#contact'
  get 'showcase/post' => 'images#create'
  #post 'showcase/post' => 'images#create'
  get 'showcase' => 'images#new'
  post 'showcase/post' => 'images#create'
  patch 'showcase/post' => 'images#new'
  get 'images' => 'images#new'
  #get 'images/new' => 'showcase'
  #resources "contacts", only: [:new, :create]
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

model:image.rb

class Image < ApplicationRecord

  has_attached_file :creation,
    :styles => {
      :large => "1000x1000>",
      :medium => "500x500>",
      :thumb => "300x300#"
    }
  #validates :creation, :attachment_presence => true
  validates_attachment_content_type :creation, content_type: /\Aimage\/.*\z/
  # has_attached_file :image_filename
  # validates_attachment :image_filename, :presence => true
  #validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/]
  #attribute :title, :presence => true
  # attribute :description, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  # attribute :nickname, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
end

schema.rb

ActiveRecord::Schema.define(version: 20160930075924) do

  create_table "contacts", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.string   "message"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "images", force: :cascade do |t|
    t.string   "title"
    t.string   "description"
    t.string   "nickname"
    t.datetime "created_at",            null: false
    t.datetime "updated_at",            null: false
    t.string   "creation_file_name"
    t.string   "creation_content_type"
    t.integer  "creation_file_size"
    t.datetime "creation_updated_at"
  end

end

我根本无法将表单中的数据存储在我的基础中,我已尝试过所有内容。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

^([\s\S]+?)\s*Contact 中添加require方法,image_params method方法确保存在特定参数,如果未提供,则require方法会引发错误。它会为传递到require的密钥返回ActionController::Parameters的实例。

require