多种联系表格 - Rails 3
我对这一切都是相对新手,如果这听起来很疯狂,我很抱歉!
我使用过本教程: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您需要在您希望呈现表单的每个操作中创建一个
@support
变量。另一种选择是重构表单以采用参数,这样您就更加灵活。例如,从您的角度来看:
现在,您不必在
_form.html.erb
中引用@support
,而只需引用support
因为它是本地分配。另一种选择是进一步重构表单,并担心在部分之外创建实际的
form
标记。如:
app/views/supports/new.html.erb
app/views/supports/_form.html.erb
在这种情况下,当你渲染一个
partial
与object
选项,您将在您的部分中获得一个与部分同名的局部变量。您可以在表单路径中保持更多的灵活性,但仍然可以呈现表单内部Support
对象的主要内容,同时在整个应用程序中保持一致。为了澄清,您可以通过执行以下操作在其他地方使用它:
app/views/foos/_create_foo_support.html.erb
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:
Now, instead of referring to
@support
in your_form.html.erb
, you'd refer to simplysupport
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
app/views/supports/_form.html.erb
In this case, when you render a
partial
with theobject
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 aSupport
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
无论您在何处使用联系表单,都必须传递
@support
对象。它在SupportsController#new
中工作,因为您在那里初始化变量。在您想要使用该表单的所有其他地方,您必须执行相同的操作。You have to pass
@support
object wherever you use your contact form. It's working inSupportsController#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.