尝试使用设计(自定义设计)在表单中添加字段时出现异常?
我正在尝试在表单中添加字段名字,我在视图模板、用户模型中进行了更改,更新了表(rails g add_firstname_to user firstname:string migration)。但我仍然# 获取异常未定义方法“firstname”。我缺少什么?我的文件看起来像这个
视图
<h2>Sign up</h2>
<%= form_for(resource_name, resource, :url => registration_path(resource_name)) do |f| %>
<%= f.error_messages %>
<table>
<tr><td><label>First name</label></td>
<td><%= f.text_field :firstname %></td></tr>
<tr><td><label>Email</label></td>
<td><%= f.text_field :email %></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>
</table>
<p><%= f.submit "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
用户模型类
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable, :timeoutable and :activatable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation,:firstname
end
I am trying to to add a field first name in my form ,I have changed in the view template,user model,updated the table(rails g add_firstname_to user firstname:string migration).But i am still getting an exception undefined method `firstname' for #. what am i missing ?my file looks like this
view
<h2>Sign up</h2>
<%= form_for(resource_name, resource, :url => registration_path(resource_name)) do |f| %>
<%= f.error_messages %>
<table>
<tr><td><label>First name</label></td>
<td><%= f.text_field :firstname %></td></tr>
<tr><td><label>Email</label></td>
<td><%= f.text_field :email %></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>
</table>
<p><%= f.submit "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
User model class
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable, :timeoutable and :activatable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation,:firstname
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个一步一步教程会告诉你一切。 :)
看看你的代码,有两个建议:
你说你运行了
rails g add_firstname_to user firstname:string migration
我希望这是这里的拼写错误,但您应该使用
add_firstname_to_users
- 注意 user 上的 s。正如第一条评论所示,您是否运行了
rake db:migrate
?This step by step tutorial will tell you everything. :)
Looking at your code, two suggestions:
You said you runned
rails g add_firstname_to user firstname:string migration
I hope this is a typo in here, but you should use
add_firstname_to_users
- Notice the s on user.As the first comment suggests, did you run
rake db:migrate
?