All threads / **ActiveModel::UnknownAttributeError in ExcelsController#create**
Ask A Question

Notifications

You’re not receiving notifications from this thread.

**ActiveModel::UnknownAttributeError in ExcelsController#create**

Vikram asked in Rails

Good day ,im usings ruby on rails (version 3) im trying to upload a image on my webpage but i keep on this error ActiveModel::UnknownAttributeError in ExcelsController#create , This is my first project , please help me out

controller/excel_controller.db

class ExcelsController < ApplicationController
before_action :set_excel, only: %i[ show edit update destroy ]

# GET /excels or /excels.json
def index
@excels = Excel.all

end

def purge_image
@excel = Excel.find(params[:id])
@excel.image.purge
redirect_back fallback_location: root_path, notice: "success"
end

# GET /excels/1 or /excels/1.json
def show
end

# GET /excels/new
def new
@excel = Excel.new
end

# GET /excels/1/edit
def edit
end

# POST /excels or /excels.json
def create
@excel = Excel.new(excel_params)

respond_to do |format|
  if @excel.save
    format.html { redirect_to @excel, notice: "Excel was successfully created." }
    format.json { render :show, status: :created, location: @excel }
  else
    format.html { render :new, status: :unprocessable_entity }
    format.json { render json: @excel.errors, status: :unprocessable_entity }
  end
end

end

# PATCH/PUT /excels/1 or /excels/1.json
def update
respond_to do |format|
if @excel.update(excel_params)
format.html { redirect_to @excel, notice: "Excel was successfully updated." }
format.json { render :show, status: :ok, location: @excel }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @excel.errors, status: :unprocessable_entity }
end
end
end

# DELETE /excels/1 or /excels/1.json
def destroy
@excel.destroy
respond_to do |format|
format.html { redirect_to excels_url, notice: "Excel was successfully destroyed." }
format.json { head :no_content }
end
end

def import
Excel.import(params[:file])
redirect_to excels_path, notice: "excel import successfully"
end

private
# Use callbacks to share common setup or constraints between actions.
def set_excel
@excel = Excel.find(params[:id])
end

# Only allow a list of trusted parameters through.
def excel_params
  params.require(:excel).permit(:Ordername, :Order_date, :Baby_name, :which_birthday_is_it, :height_in_cm, :weight_in_kg, :nick_name, :no_of_teeth, :i_love, :i_can, :i_say, :fav_food, :fav_ryhmes, :fav_song, :fav_toy, :add_input, :image)
end

end
model/excel.rb

class Excel < ApplicationRecord
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Excel.create! row.to_hash
has_one_attached :image
validates :image, attached: true, dimension: { width: { min: 800, max: 2400 } },
content_type: [:png, :jpg, :jpeg], size: { less_than: 100.kilobytes , message: 'is not given between size' }
end
end
end
view/excels/show.html.rb

Order name:

Order date:

Baby name:

Which birthday is it:

Height in cm:

Weight in kg:

Nick name:

No of teeth:

I love:

I can:

I say:

Fav food:

Fav ryhmes:

Fav song:

Fav toy:

Add input:

image:

<%= link_to 'Edit', edit_excel_path(@excel) %> |
<%= link_to 'Back', excels_path %>

index.html.rb


<% @excels.each do |excel| %>

<%= excel.Order_name %>
<%= excel.Order_date %>
<%= excel.Baby_name %>
<%= excel.which_birthday_is_it %>
<%= excel.height_in_cm %>
<%= excel.weight_in_kg %>
<%= excel.nick_name %>
<%= excel.no_of_teeth %>
<%= excel.i_love %>
<%= excel.i_can %>
<%= excel.i_say %>
<%= excel.fav_food %>
<%= excel.fav_ryhmes %>
<%= excel.fav_song %>
<%= excel.fav_toy %>
<%= excel.add_input %>
<%= excel.image %>
<%= link_to 'Show', excel %>
<%= link_to 'Edit', edit_excel_path(excel) %>
<%= link_to 'Destroy', excel, method: :delete, data: { confirm: 'Are you sure?' } %>

<% end %>
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 56,919+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.