Vikram

Joined

100 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in NoMethodError in ImagesController#import

Good Day , I have an error while upload csv file into my webpage Error NoMethodError in ImagesController#import undefined method `import' for Image:Class Iam fresher into the ruby on rails plz help me out

Model/image.rb

class Image < ApplicationRecord
has_one_attached :avatar
end
def self.import
CSV.foreach(file.path, headers: true) do |row|
Image.create! row.to_hash
end
end

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

<%= notice %>

Order name: <%= @excel.Order_name %>

Order date: <%= @excel.Order_date %>

Baby name: <%= @excel.Baby_name %>

Which birthday is it: <%= @excel.which_birthday_is_it %>

Height in cm: <%= @excel.height_in_cm %>

Weight in kg: <%= @excel.weight_in_kg %>

Nick name: <%= @excel.nick_name %>

No of teeth: <%= @excel.no_of_teeth %>

I love: <%= @excel.i_love %>

I can: <%= @excel.i_can %>

I say: <%= @excel.i_say %>

Fav food: <%= @excel.fav_food %>

Fav ryhmes: <%= @excel.fav_ryhmes %>

Fav song: <%= @excel.fav_song %>

Fav toy: <%= @excel.fav_toy %>

Add input: <%= @excel.add_input %>

image: <% if @post.image.attached? %> <%= image_tag @post.image, width: "200px" %> <%= link_to "Delte image", purge_image_post_path(@post), method: :delete %> <% end %>

<%= 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 %>