2016-09-30 4 views
0

私はデータをレールに格納しようとしていますが、送信ボタンを押すたびに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:

が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 

モデル: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

'simple_form_for @image、HTMLは:{マルチパート:真}ん| F |'あなたは何のエラーを取得しなかったここ –

+0

'url'必要はありませんか? –

+2

'params.permit(:title、:description、:nickname、:creation)'を 'params.require(:image).permit(:title、:description、:ニックネーム、:作成)に調整しようとすることができますか' –

答えて

1

image_params methodrequireメソッドを追加すると、requireメソッドは特定のパラメータが存在することを保証し、指定されていない場合はエラーをスローします。 requireに渡されたキーに対してActionController::Parametersのインスタンスを返します。

def image_params 
    params.require(:image).permit(:title, :description, :nickname, :creation) 
end 
関連する問題