2011-04-12 103 views
2

(ruby 1.9.2 rails 3.0.6)爲什麼form_for對我不起作用

我是ROR的新手,對不起我的簡單問題。

我有一個表格和控制器,當我啓動我的應用程序,我可以看到該日誌已顯示。不過也有例外渲染視圖...

一切都應該蠻好看的..問題是什麼?我該如何解決它?

謝謝。

undefined method `users_path' for #<#<Class:0x47c7678>:0x47c6118> 
Extracted source (around line #2): 

1: <h1>Welcome Aboard</h1> 
2: <%= form_for(@user) do |f| %> 
3: <table> 
4:  <tr> 
5:  <td><%= f.label :username %></td> 

class RegisterController < ApplicationController 

    def sign_up 
    logger.debug("sign_up inoked") 

    @user = User.new 

    logger.debug("sign_up finished") 
    end 

end 

的意見/註冊/ sign_up.html.erb

<%= form_for(@user) do |f| %> 
    <table> 
    <tr> 
     <td><%= f.label :username %></td> 
     <td><%= f.text_field :username %></td> 
    </tr> 
    <tr> 
     <td><%= f.label :password %></td> 
     <td><%= f.password_field :password %></td> 
    </tr> 
    <tr> 
     <td><%= f.label :password_confirmation %></td> 
     <td><%= f.password_field :password_confirmation %></td> 
    </tr> 
    <tr> 
     <td colspan="2"></td> 
    </tr> 
    <tr> 
     <td colspan="2"><%= f.submit "Sign up" %></td> 
    </tr> 
    </table> 
<% end %> 

回答

9

看來你沒有相應的路由。你有這樣一行:

resources :users 

在你的routes.rb文件中?如果不嘗試添加,請。或者只是創建名爲route的特定users_path。

+0

添加資源後:用戶。它正在工作。 但爲什麼我需要將我的模型添加到路線? – jojo 2011-04-12 07:19:47

+0

form_for默認查找resource_path(你的情況下是@users),所以users_path。這是RESTful的方式。這條路線不存在,你得到一個錯誤。如果你想指定另一條路線,你可以使用「form_for @user,:url => your_custom_path」,但它必須是某種東西。 – Spyros 2011-04-12 07:37:36

+0

是的,當你認爲軌道是JUST模型,視圖控制器時有點令人困惑......但是一個路線也是工作所需要的。我猜「模型,視圖,路線,控制器」並不輕鬆。 – Jenny 2011-04-25 18:48:58