问题描述
我有一个表单,我想出现在每个页面的顶部,所以我将它包含在/app/views/layouts/application.html.erb 文件中,但出现错误 undefined method
model_name' for NilClass:Class` 尝试加载页面时.
这是 application.html.erb 中的表单片段
这是我的/app/controllers/user_controller.rb
class UserController
我认为我遇到了这个错误,因为表单在 application.html.erb 文件中,我需要以某种方式指定路径,但我又一次对 rails 很陌生.
如果您需要发布任何其他内容,请告诉我.
编辑按照 Ola 的建议,在我的 app/views/layouts/application.html.erb 文件中,我有这样的内容:
<%=yield :add_user %>
在我的 app/views/users/new.html.erb 文件中我有这个
<%= form_for @user do |f|%><h3>添加新联系人</h3>名字
<%= f.text_field :first_name %><br/>姓氏<br/><%= f.text_field :last_name %><br/>电子邮件<br/><%= f.text_field :email %><小时/><%= f.submit "添加联系人" %><%结束%><%结束%>
表单未呈现,因为我的 url 是
http://localhost:3000/admin/index
,所以它在 app/views/中寻找 add_user
content_foradmin/index.html.erb
解决方案
如果表单包含在多个操作(页面)中,您需要将
@user
设置为 something 或 form_for
指令将失败(它没有任何内容可以为 for 创建表单).
更好的方法(和 Rails 默认)是为每个动作设置单独的视图,每个视图只包含该动作所需的元素,并加载到您的
application.html.erb
布局中.所以你会有一个
app/views/users/new.html.erb
视图,其中包含表单.您很少需要在Rails 中定义任何加载路径,它们都来自模型、控制器和动作名称——或者来自您的routes.rb
.这是约定优于配置 范式的核心部分,该范式运行在 Rails 中如此深入.
如果您确实需要有一个表单来在每个页面上创建一个新对象(例如通常用于 UserSessions),您可以重写您的
form_for
使其不依赖于存在的对象:
form_for(User.new)
或者如果你需要强制它发布到
create
方法
form_for(User.new, :url => { :action => "create" })
您可以在
I have a form that i want to appear at the top of every page so i've included it in the /app/views/layouts/application.html.erb file and i get the error
undefined method
model_name' for NilClass:Class` when trying to load the page.
Here's the form snippet in application.html.erb
<%= form_for @user do |f| %>
<h3>Add new contact</h3>
<%= f.text_field :first_name %><br />
<%= f.text_field :last_name %>
<%= f.text_field :email %>
<hr />
<%= f.submit "Add Contact" %>
<% end %>
Here's my /app/controllers/user_controller.rb
class UserController < ApplicationController
def index
end
def new
@user = User.new
end
end
I'm thinking that i'm hitting this error because since the form is in the application.html.erb file, i need to somehow specify the path, but then again i'm very new to rails.
Let me know if you need anything else posted.
EDITFollowing Ola's suggestion, In my app/views/layouts/application.html.erb file I have something like this:
<div>
<%= yield :add_user %>
</div>
and in my app/views/users/new.html.erb file I have this
<% content_for :add_user do %>
<%= form_for @user do |f| %>
<h3>Add new contact</h3>
First Name<br />
<%= f.text_field :first_name %><br />
Last Name<br />
<%= f.text_field :last_name %><br />
Email<br />
<%= f.text_field :email %>
<hr />
<%= f.submit "Add Contact" %>
<% end %>
<% end %>
The form is not rendered because my url is
http://localhost:3000/admin/index
and so it's looking for the add_user
content_for in app/views/admin/index.html.erb
解决方案
If the form is included for multiple actions (pages) you need to set
@user
to something or the form_for
directive will fail (it won't have anything to create a form for).
A much better approach (and the Rails default) is to have separate views for each action, each including only the elements required by that action, and loaded into your
application.html.erb
layout with <%= yield %>
. So you would have a app/views/users/new.html.erb
view which has the form in it. You very rarely need to define any load paths in Rails, they are all derived from the model, controller and action names - or from your routes.rb
. This is a core part of the convention over configuration paradigm which runs so deep in Rails.
Edit: If you do need to have a form for creating a new object on every page (often used for UserSessions for example), you can rewrite your
form_for
so that it doesn't depend on an object being present:
form_for(User.new)
or if you need to force it to post to the
create
method
form_for(User.new, :url => { :action => "create" })
You can read more about resource driven
form_for
in the Ruby On Rails API docs.
这篇关于NilClass:Class 的 Rails 未定义方法“model_name"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!