Paul Paul - 2 months ago 7
Ruby Question

Impossible to store data in base from a form

Im trying to store data in rails, but I simply can't, everytime I push with submit button, the values on base are null and the method save try to store data before I push button submit, here is my code :

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


my view 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


I simply can't store data with the form in my bases, Ive tried everything.

Thanks in advance for your help.

Answer

Add a require method in image_params method and the require method ensures that a specific parameter is present, and if it's not provided, the require method throws an error. It returns an instance of ActionController::Parameters for the key passed into require.

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