Ruby on Rails错误 - Admin :: Store#new中的NoMethodError

时间:2015-12-01 09:06:00

标签: ruby-on-rails ruby ruby-on-rails-4

Ruby on Rails错误 - Admin :: Store#new中的NoMethodError 这是我的代码和项目结构。我的代码有什么问题?
我生成了这个命令“Rails generate controller admin / store”

生成控制器

控制器

/home/rok/Development/Pan/app/controllers/admin/store_controller.rb

    class Admin::StoreController < ApplicationController
             def new
               @store = Store.new
               #render "admin/store/new"
             end
     end

浏览

    /home/rok/Development/Pan/app/views/admin/store/new.thml.erb

     <h1>xx </h1>
     <p> xx </p>

     <%= simple_form_for (@store) do |form| %>

     <div class="form-inputs">
             <%= form.input :store_name, label: "xx" %>
             <%= form.input :store_address, label: "xx" %>
             <%= form.input :store_phone, label: "xx" %>
             <%= form.input :store_logitude, label: "xx" %>
             <%= form.input :store_latitude, label: "xx" %>
     </div>

     <div class="form-actions">
             <%= form.button :submit %>
     </div>
     <% end %>

模型

    /home/rok/Development/Pan/app/models/store.rb

    class Store < ActiveRecord::Base
      validates :store_name, presence: true
      validates :store_address, presence: true
      validates :store_phone, presence: true
      validates :store_longitude, presence: true
      validates :store_latitude, presence: true
    end

Admin :: Store#new中的NoMethodError 显示/home/rok/Development/Pan/app/views/admin/store/new.html.erb
第4行提出的地方:

#&lt;#:0x007f627c4ade00&gt;的未定义方法`stores_path' 提取的来源(第4行): 2 3 4 五 6 7

   <p> xx </p>

   <%= simple_form_for (@store) do |form| %>

   <div class="form-inputs">
       <%= form.input :store_name, label: "xx" %>

Rails.root:/ home / rok / Development / Pan

有什么问题?

2 个答案:

答案 0 :(得分:0)

您还需要提供名称空间: -

<%= simple_form_for ([:admin,@store]) do |form| %>
<% end %>

答案 1 :(得分:0)

您最好将名称控制器更改为复数

rails generate controller admin/stores

<强>的routes.rb

你必须这样做:

namespace :admin do
  resources :stores
end

然后,在你的表格中写下:

<%= simple_form_for ([:admin, @store]) do |form| %>
<% end %>

我希望这对你有所帮助。