rails 中的未定义方法“images_path”错误

undefined method `images_path' Error in rails

我收到一个错误,我不知道它从哪里来,这是我的代码 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

我总是遇到同样的错误但对我帮助不大:

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

根据 ruby 错误,应该是错误的代码行:

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

这是我的模型:

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

结束


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

提前致谢, 待会见,

在您的 routes.rb 文件中执行此操作:

resources "images", only: [:crea, :post], path: 'images'

默认simple_forms搜索images_pathPOST方法,这意味着你的控制器create动作

但是你有这个

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

您可以尝试将 resources 行更改为

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

并在您的控制器中

  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

谢谢两位,解决方案是关于雨果描述的名字,再次感谢。 现在,我必须更加注意命名。