Search code examples
ruby-on-railsrubysimple-form

undefined method `images_path' Error in rails


I got an error and I dont know where it come from, this is my code images_controller.rb

class ImagesController < ApplicationController
  layout 'home2'
  #before_action :set_image, only: [:show, :edit, :update, :destroy]
  def crea
    @image = Image.new
  end

  def post
    @image = Image.new(params[:images])
  end
end

post.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 do |f| %>
  <%= f.input :title, :required => true %>
  <%= f.input :description, :required => true %>
  <%= f.input :nickname, :required => true %>
  <%= f.button :submit, "Post !", :class => "buttonShow"  %>
<% end %>

routes.rb

Rails.application.routes.draw do
  resources "images", only: [:crea, :post]
  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' => 'images#crea'
  get 'showcase/post' => 'images#post'
  #resources "contacts", only: [:new, :create]
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

I got always the same error who don't help me a lot :

undefined method `images_path' for #<#<Class:0x007ffe6c718578>:0x007ffe6c68ab38>
Did you mean?  image_alt

The line of code, where the error is supposed to be is according to the ruby error :

<%= simple_form_for @image do |f| %>

this is my models :

class Image < ApplicationRecord
      attribute :title,      :validate => true
      attribute :description,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
      attribute :nickname, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i

end


ActiveRecord::Schema.define(version: 20160927210003) 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.string   "image_filename"
        t.datetime "created_at",     null: false
        t.datetime "updated_at",     null: false
      end

    end

Thank's in advance, See you soon,


Solution

  • By default simple_forms search the images_path with POST Method, that means the create action of your controller

    But you have this

      resources "images", only: [:crea, :post]
      # Many other code
      get 'images/crea'
    

    You can try changing the resources line to

      resources "images", only: [:crea, :create]
    

    and in your controller

      class ImagesController < ApplicationController
        layout 'home2'
        #before_action :set_image, only: [:show, :edit, :update, :destroy]
        def crea
          @image = Image.new
        end
    
        def create
          @image = Image.new(params[:images])
        end
      end