【发布时间】:2014-06-08 17:35:35
【问题描述】:
如何在单独的控制器/视图中调用模型?
I.E.我有一个用户模型,在我的静态控制器中,在主方法/视图中,我想调用 @user 来获取注册表单。当我这样做时,它会引发无方法错误。有没有解决的办法?
我正在使用设计。
静态控制器
class StaticController < ApplicationController
def home
@user = User.new
end
end
Home.html.erb
<%= simple_form_for @user do |f| %>
<%= f.input :email %>
<%= f.button :submit %>
<% end %>
用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :trackable, :validatable
protected
def password_required?
false
end
end
以下解决方案不起作用。我得到一个
NOMETHODERROR in STATIC#HOME
undefined method `users_path' for #<#<Class:0x007fe888125480>:0x007fe8881247d8>
关于为什么会这样的任何想法?
【问题讨论】:
-
您的 routes.rb 文件中有资源 :users 吗?这就是创建这些辅助路径的原因
-
哇。我有 devise_for :users。为用户添加了资源,效果很好。它的小东西......
标签: ruby-on-rails ruby-on-rails-4 simple-form