使用 ActiveModel 来实现“联系我们”时遇到问题形式
我正在使用 Ruby on Rails 3.0.9,并且我正在尝试自己实现“联系我们”表单。所以...
...在我的模型文件中我有:
require 'active_model'
class ContactUs
include ActiveModel::Conversion
include ActiveModel::Validations
attr_accessor :email, :subject, :message
def initialize(attributes = {})
@attributes = attributes
end
validates :email,
:presence => true
validates :subject,
:presence => true
validates :message,
:presence => true
def persist
@persisted = true
end
def persisted?
false
end
end
...在我的视图文件中我有:
<%= form_for @contact_us, :url => contact_us_path do |f| %>
<%= f.text_field :email %>
<%= f.text_field :subject %>
<%= f.text_area :message %>
<% end %>
...在我的路由器文件中我有:
match 'contact_us' => 'pages#contact_us', :via => [:get, :post]
...在我的控制器文件中我有:
class PagesController < ApplicationController
def contact_us
case request.request_method
when 'GET'
@contact_us = ContactUs.new
when 'POST'
@contact_us = ContactUs.new(params[:contact_us])
end
end
end
使用上面的代码,当我提交的表单至少有一个空白字段(我这样做是为了使其不通过验证),并且表单被重新加载,但我没有自动填充这些字段。也就是说,重新加载表单后(按下提交按钮后会发生这种情况)字段值将全部设置为空白值。
问题是什么?我使用ActiveModel
有错吗?
I am using Ruby on Rails 3.0.9 and I am trying to implement a "Contact Us" form myself. So...
... in my model file I have:
require 'active_model'
class ContactUs
include ActiveModel::Conversion
include ActiveModel::Validations
attr_accessor :email, :subject, :message
def initialize(attributes = {})
@attributes = attributes
end
validates :email,
:presence => true
validates :subject,
:presence => true
validates :message,
:presence => true
def persist
@persisted = true
end
def persisted?
false
end
end
... in my view file I have:
<%= form_for @contact_us, :url => contact_us_path do |f| %>
<%= f.text_field :email %>
<%= f.text_field :subject %>
<%= f.text_area :message %>
<% end %>
... in my router file I have:
match 'contact_us' => 'pages#contact_us', :via => [:get, :post]
... in my controller file I have:
class PagesController < ApplicationController
def contact_us
case request.request_method
when 'GET'
@contact_us = ContactUs.new
when 'POST'
@contact_us = ContactUs.new(params[:contact_us])
end
end
end
Using the above code, when I submit the form with at least a blank field (I do that in order to make it doesn't pass validations) and the form is reloaded I don't get those fields auto-populated. That is, after reloading the form (this happens after pressing the submit button) field values are set all to blank values.
What is the problem? Am I wrong on using the ActiveModel
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试替换
为
编辑:
我相信您必须自己添加质量分配功能,如下所示:
您实际上可以跳过此行:
self.class.send(:attr_accessor, attr.to_sym)
如果您想像现在一样拥有attr_accessor :email, :subject, :message
try to replace
with
EDIT:
I believe you'll have to add the mass-assigment functionality yourself, like so:
You can actually skip this line:
self.class.send(:attr_accessor, attr.to_sym)
if you want to haveattr_accessor :email, :subject, :message
like you do already