2016-09-27 67 views
1

我得到了一个错误,我不知道它从何而来,这是我的代码images_controller.rb未定义的方法`images_path”错误轨道

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 

根据红宝石错误的代码行,那里的错误应该是是:

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

感谢的提前, 再见,

回答

0

默认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 
0

在你的routes.rb文件操作:

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

错误仍然存​​在,我真的不知道为什么它不能正常工作。 – Paul

+0

你可以在终端上显示完整的错误日志吗? – luissimo

+0

http://hpics.li/6c0fc70这个? – Paul

0

谢谢你们,这些解决方案引起关注,像雨果的名字再次形容了,谢谢。 现在,我必须更多地关注命名。

相关问题