2015-09-06 49 views
-1

我收到錯誤如何調試「NoMethodError」在Ruby中

NoMethodError in Styles#new undefined method 'styles_path' for <Class:0x00000006288268>:0x0000000632ca20 

它工作得很好,如果我沒有form_for選項,讓我們說更換的Hello World整個片段。在該應用/視圖/樣式

new.html.erb文件代碼文件夾

<div class="row"> 
    <div class="well col-md-8 col-md-offset-2"> 
    <%= form_for @style do |f|%> 
     <%= f.label :name %> 
     <%= f.text_field :name %> 
     <%= f.submit 'Create style' , class: 'btn btn-success' %> 
    <%end%> 
    </div> 
</div> 

予定義的樣式控制器:

class StylesController < ApplicationController 

    def new 
    @style = Style.new 
    end 

end 

的路由文件具有以下片斷

resources :styles, only: [:new,:show] 

回答

2

form_for您正在發佈表格styles_path(POST )來創建一個新的風格,但是,在你的routes.rb你只提到newshow。您需要爲create添加一個,並且您還需要在控制器中添加一個名爲create的新動作。

的routes.rb

resources :styles, only: [:new, :show, :create] 

控制器:

class StylesController < ApplicationController 

    def new 
    @style = Style.new 
    end 

    def create 
    # your code 
    end 

end 

建議:你應該read this