使用 ActiveModel 来实现“联系我们”时遇到问题形式

发布于 2024-11-28 14:14:10 字数 1393 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2024-12-05 14:14:10

尝试替换

<%= form_for @contact_us, :url => contact_us_path do |f| %>
   <%= f.text_field :email %>
   <%= f.text_field :subject %>
   <%= f.text_area  :message %>
<% end %> 

<%= form_for @contact_us, :url => contact_us_path do |f| %>
   <%= f.text_field :email, :value => @contact_us.email %>
   <%= f.text_field :subject, :value => @contact_us.subject %>
   <%= f.text_area  :message, :value => @contact_us.message %>
<% end %> 

编辑:

我相信您必须自己添加质量分配功能,如下所示:

def initialize(attributes = {})
  attributes.keys.each do |attr|
    self.class.send(:attr_accessor, attr.to_sym)
    instance_variable_set "@" + attr.to_s, attributes[attr.to_sym]
  end
end

您实际上可以跳过此行:self.class.send(:attr_accessor, attr.to_sym) 如果您想像现在一样拥有 attr_accessor :email, :subject, :message

try to replace

<%= form_for @contact_us, :url => contact_us_path do |f| %>
   <%= f.text_field :email %>
   <%= f.text_field :subject %>
   <%= f.text_area  :message %>
<% end %> 

with

<%= form_for @contact_us, :url => contact_us_path do |f| %>
   <%= f.text_field :email, :value => @contact_us.email %>
   <%= f.text_field :subject, :value => @contact_us.subject %>
   <%= f.text_area  :message, :value => @contact_us.message %>
<% end %> 

EDIT:

I believe you'll have to add the mass-assigment functionality yourself, like so:

def initialize(attributes = {})
  attributes.keys.each do |attr|
    self.class.send(:attr_accessor, attr.to_sym)
    instance_variable_set "@" + attr.to_s, attributes[attr.to_sym]
  end
end

You can actually skip this line: self.class.send(:attr_accessor, attr.to_sym) if you want to have attr_accessor :email, :subject, :message like you do already

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文