【问题标题】:Rails routing, NoMethodErrorRails 路由,NoMethodError
【发布时间】:2015-02-01 17:53:25
【问题描述】:

我使用的是 Rails 3.2 和 Ruby 4。当我浏览到 http://localhost:3000/account/new 时出现错误:

NoMethodError in Accounts#new
Showing D:/row/dev/basic/app/views/accounts/_form_account.html.erb where line #1 raised:
undefined method `accounts_path' for #<#<Class:0x42c8040>:0x6daa960>
Extracted source (around line #1):
1: <%= form_for(@account) do |f| %>
2: 
3:   <div>
4:    <%= f.label :username %><br>

我使用rails generate controller Controllernames index show new edit delete 创建了帐户视图。我还跑了rails generate model account。 根据我正在关注的在线 Rails 课程,这应该在 routes.rb 中创建:

编辑:我使用了rails generate model accounts,所以最后是 s。

  resources :accounts
  get 'accounts/:id/delete' => 'accounts#delete', :as => :accounts_delete

但是,这不是在 routes.rb 中创建的。经过一些编辑后,我的 routes.rb 是:

Basismysql::Application.routes.draw do

  # Public pages
  get '/page1' => 'pages#page1'
  get '/page2' => 'pages#page2'
  get '/page3' => 'pages#page3'

  get "/account/index" => 'accounts#index'
  get "/account/show" => 'accounts#show'
  get "/account/new" => 'accounts#new'
  get "/account/edit" => 'accounts#edit'
  get "/account/delete" => 'accounts#delete'
  get 'account/:id/delete' => 'accounts#delete', :as => :accounts_delete

  devise_for :users
  root :to => 'pages#index'
end

New.html.erb 是:

<div class="container">
  <h1>Accounts#new</h1>
  <p>Find me in app/views/accounts/new.html.erb</p>
</div>

<div class="container">
  <%= render "form_account" %>
</div>

而 _form_account.html.erb 是:

<%= form_for(@account) do |f| %>

  <div>
   <%= f.label :username %><br>
   <%= f.text_field :username %>
  </div>
  <div>
   <%= f.label :firstname %><br>
   <%= f.text_field :firstname %>
  </div>
  <div>
   <%= f.label :lastname %><br>
   <%= f.text_field :lastname %>
  </div>
  <div>
    <%= f.label :organisation %>
    <%= f.text_field :organisation %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

部分帐户控制器是:

  def new
    @account = Account.new
  end

  def create
    @account = Account.new(account_params)
    if @account.save
      redirect_to(:action => 'index')
    else
      render('new')
    end
  end

private
    def account_params
    params.require(:account).permit(:username, :firstname, :lastname, :organisation)
end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:
    get "/account/index" => 'accounts#index'
    get "/account/show" => 'accounts#show'
    get "/account/new" => 'accounts#new'
    get "/account/edit" => 'accounts#edit'
    get "/account/delete" => 'accounts#delete'
    get 'account/:id/delete' => 'accounts#delete', :as => :accounts_delete
    

    这不是你应该创建路由的方式,它们都是未命名的(除了最后一个),非restful并且都是get,替换为

    resources :accounts
    

    你的错误就会消失

    【讨论】:

      【解决方案2】:

      这行得通

      rails generate controller accounts index show new edit destroy
      

      注意:生成控制器时必须使用帐户而不是帐户

      rails generate model account
      

      注意:您必须拥有单数帐户

      在 routes.rb 中

      map.resources :accounts /or
      resources :accounts
      

      取决于 Rails 的版本

      【讨论】:

      • 谢谢,rails 生成模型帐户是一个错字。我确实使用过 rails 生成模型帐户
      【解决方案3】:

      除了:

      resources :accounts
      

      你可能需要:

      resource :account
      

      您已经通过将路由零碎添加到路由文件来启动它,但其中一些需要是PUTs 或POSTs 或DELETEs。 resource :account 是一个更简单的捷径(正确)。

      【讨论】:

      • 感谢resources :accounts 工作。我假设您提到了resource :account,因为我写了rails generate model account。这实际上是我问题中的一种类型,我确实使用了rails generate model accounts
      • @Nick 您应该在模型创建中使用单数 account,并且此 account 模型的对应路由将是 resources :accounts。你根本不需要resource :account
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多