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
<p id="notice"><%= notice %></p>
<p>
<strong>Order name:</strong>
<%= @excel.Order_name %>
</p>
<p>
<strong>Order date:</strong>
<%= @excel.Order_date %>
</p>
<p>
<strong>Baby name:</strong>
<%= @excel.Baby_name %>
</p>
<p>
<strong>Which birthday is it:</strong>
<%= @excel.which_birthday_is_it %>
</p>
<p>
<strong>Height in cm:</strong>
<%= @excel.height_in_cm %>
</p>
<p>
<strong>Weight in kg:</strong>
<%= @excel.weight_in_kg %>
</p>
<p>
<strong>Nick name:</strong>
<%= @excel.nick_name %>
</p>
<p>
<strong>No of teeth:</strong>
<%= @excel.no_of_teeth %>
</p>
<p>
<strong>I love:</strong>
<%= @excel.i_love %>
</p>
<p>
<strong>I can:</strong>
<%= @excel.i_can %>
</p>
<p>
<strong>I say:</strong>
<%= @excel.i_say %>
</p>
<p>
<strong>Fav food:</strong>
<%= @excel.fav_food %>
</p>
<p>
<strong>Fav ryhmes:</strong>
<%= @excel.fav_ryhmes %>
</p>
<p>
<strong>Fav song:</strong>
<%= @excel.fav_song %>
</p>
<p>
<strong>Fav toy:</strong>
<%= @excel.fav_toy %>
</p>
<p>
<strong>Add input:</strong>
<%= @excel.add_input %>
</p>
<p>
<strong>image:</strong>
<% if @post.image.attached? %>
<%= image_tag @post.image, width: "200px" %>
<%= link_to "Delte image", purge_image_post_path(@post), method: :delete %>
<% end %>
</p>
<%= link_to 'Edit', edit_excel_path(@excel) %> |
<%= link_to 'Back', excels_path %>
index.html.rb
<tbody>
<% @excels.each do |excel| %>
<tr>
<td><%= excel.Order_name %></td>
<td><%= excel.Order_date %></td>
<td><%= excel.Baby_name %></td>
<td><%= excel.which_birthday_is_it %></td>
<td><%= excel.height_in_cm %></td>
<td><%= excel.weight_in_kg %></td>
<td><%= excel.nick_name %></td>
<td><%= excel.no_of_teeth %></td>
<td><%= excel.i_love %></td>
<td><%= excel.i_can %></td>
<td><%= excel.i_say %></td>
<td><%= excel.fav_food %></td>
<td><%= excel.fav_ryhmes %></td>
<td><%= excel.fav_song %></td>
<td><%= excel.fav_toy %></td>
<td><%= excel.add_input %></td>
<td><%= excel.image %></td>
<td><%= link_to 'Show', excel %></td>
<td><%= link_to 'Edit', edit_excel_path(excel) %></td>
<td><%= link_to 'Destroy', excel, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>