多种联系表格 - Rails 3

发布于 2024-12-14 00:21:30 字数 1799 浏览 0 评论 0原文

我对这一切都是相对新手,如果这听起来很疯狂,我很抱歉!

我使用过本教程: http://www.railsmine .net/2010/03/rails-3-action-mailer-example.html

我有一个新的联系表单,效果很好。

控制器位于 app/controllers/support_controller.rb

class SupportsController < ApplicationController
  def new
    # id is required to deal with form
    @support = Support.new(:id => 1)
   end

  def create
    @support = Support.new(params[:support])
    if @support.save
      redirect_to('/', :notice => "Support was successfully sent.")
    else
      flash[:alert] = "You must fill all fields."
      render 'new'
    end
  end
end

,模型位于 /app/models/support.rb

class Support
  include ActiveModel::Validations

  validates_presence_of :email, :sender_name, :support_type, :content
  # to deal with form, you must have an id attribute
  attr_accessor :id, :email, :sender_name, :support_type, :content

  def initialize(attributes = {})
    attributes.each do |key, value|
      self.send("#{key}=", value)
    end
    @attributes = attributes
  end

  def read_attribute_for_validation(key)
    @attributes[key]
  end

  def to_key
  end

  def save
    if self.valid?
      Notifier.support_notification(self).deliver!
      return true
    end
    return false
  end
end

然而,视图仅在views/supports/new.html.rb 中工作(渲染-views/supports/_form.html.erb )

所以我可以从 localhost:3000/support/new 调用模型/控制器,但是如果我尝试从根目录(例如 app/view/contact.html.erb)在另一个视图中渲染相同的表单,我得到

undefined method `model_name' for NilClass:Class

:认为这是因为它正在从支持目录中调用支持模型。

我是否必须在 @support 上创建一个实例才能调用它?如果是这样,最好的方法是什么?我想我已经快到了。我只想在多个页面上提供联系表格,而不仅仅是支持/新

谢谢

查理

I am relative newbie to all this, so sorry if this sounds mad!

I have used this tutorial: http://www.railsmine.net/2010/03/rails-3-action-mailer-example.html

And I have a new contact form working great.

The controller is at app/controllers/support_controller.rb

class SupportsController < ApplicationController
  def new
    # id is required to deal with form
    @support = Support.new(:id => 1)
   end

  def create
    @support = Support.new(params[:support])
    if @support.save
      redirect_to('/', :notice => "Support was successfully sent.")
    else
      flash[:alert] = "You must fill all fields."
      render 'new'
    end
  end
end

And the model at /app/models/support.rb

class Support
  include ActiveModel::Validations

  validates_presence_of :email, :sender_name, :support_type, :content
  # to deal with form, you must have an id attribute
  attr_accessor :id, :email, :sender_name, :support_type, :content

  def initialize(attributes = {})
    attributes.each do |key, value|
      self.send("#{key}=", value)
    end
    @attributes = attributes
  end

  def read_attribute_for_validation(key)
    @attributes[key]
  end

  def to_key
  end

  def save
    if self.valid?
      Notifier.support_notification(self).deliver!
      return true
    end
    return false
  end
end

The views however only work in views/supports/new.html.rb (rendered - views/supports/_form.html.erb)

So I can call the Model / Controller from localhost:3000/support/new but if I try and render the same form in another view from the root directory e.g. app/view/contact.html.erb I get:

undefined method `model_name' for NilClass:Class

I think this is because it is calling the support model away from the supports directory.

Do I have to create an instance on @support so it can be called? If so what is the best way of doing that? I think I am nearly there. I just want the contact form on multiple pages not just in suppport/new

Thanks

Charlie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

当爱已成负担 2024-12-21 00:21:30

是的,您需要在您希望呈现表单的每个操作中创建一个 @support 变量。

另一种选择是重构表单以采用参数,这样您就更加灵活。例如,从您的角度来看:

<%= render :partial => "supports/form", :locals => {:support => @support} %>

现在,您不必在 _form.html.erb 中引用 @support,而只需引用 support 因为它是本地分配。

另一种选择是进一步重构表单,并担心在部分之外创建实际的 form 标记。

如:

app/views/supports/new.html.erb

<%= form_for @support do |form| %>
  <%= render :partial => "suppports/form", :object => form %>
<% end %>

app/views/supports/_form.html.erb

<%= form.text_field :foo %>
<%= form.text_field :bar %>
...

在这种情况下,当你渲染一个 partialobject 选项,您将在您的部分中获得一个与部分同名的局部变量。您可以在表单路径中保持更多的灵活性,但仍然可以呈现表单内部 Support 对象的主要内容,同时在整个应用程序中保持一致。

为了澄清,您可以通过执行以下操作在其他地方使用它:

app/views/foos/_create_foo_support.html.erb

<%= form_for @foo.support do |form| %>
   <%= render :partial => "supports/form", :object => form %>
<% end %>

Yes, you would need to create a @support variable in each action you wish to render your form.

Another option would be to refactor the form to take a parameter, that way you're a bit more flexible. For example, from your view:

<%= render :partial => "supports/form", :locals => {:support => @support} %>

Now, instead of referring to @support in your _form.html.erb, you'd refer to simply support as it's a local_assign.

Yet another option would be to refactor the form a little further, and worry about creating the actual form tag outside of the partial.

Such as:

app/views/supports/new.html.erb

<%= form_for @support do |form| %>
  <%= render :partial => "suppports/form", :object => form %>
<% end %>

app/views/supports/_form.html.erb

<%= form.text_field :foo %>
<%= form.text_field :bar %>
...

In this case, when you render a partial with the object option, you will get a local variable in your partial with the same name as the partial. You maintain a little bit more flexibility in the path of your form, but can still render the meat of what a Support object is inside of the form while remaining consistent across your app.

To clarify, you could use this somewhere else by doing something like:

app/views/foos/_create_foo_support.html.erb

<%= form_for @foo.support do |form| %>
   <%= render :partial => "supports/form", :object => form %>
<% end %>
七七 2024-12-21 00:21:30

无论您在何处使用联系表单,都必须传递 @support 对象。它在 SupportsController#new 中工作,因为您在那里初始化变量。在您想要使用该表单的所有其他地方,您必须执行相同的操作。

You have to pass @support object wherever you use your contact form. It's working in SupportsController#new because you initialize the variable there. In all other places where you want to use the form, you'll have to do the same.

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