我有以下模型:
class Contact
attr_accessor :name, :emails, :message
def initialize(attrs = {})
attrs.each do |k, v|
self.send "#{k}=", v
end
end
def persisted?
false
end
end
我以这种方式打电话给我的联系表:
<div class="email_form">
<%= render 'form' %>
</div>
这里是控制器:
class ShareController < ApplicationController
layout "marketing_2013"
respond_to :html, :js
def index
@contact = Contact.new
end
end
这里是表格:
<%= form_for(@contact) do |f| %>
<%= f.label :name, "Your Name" %>
<%= f.text_field :name %>
<%= f.label :text, "Send to (separate emails with a comma)" %>
<%= f.text_field :emails %>
<%= f.label :message, "Email Text" %>
<%= f.text_area :message %>
<%= f.submit %>
<% end %>
出于某种原因,我不断收到此错误:undefined method model_name for Contact:Class
我目前无法使用的任何原因?
除了您的config / routes.rb中正确的路由之外,您的模型上还将需要以下两个说明:
include ActiveModel::Conversion
extend ActiveModel::Naming
看看这个问题:form_for without ActiveRecord, form action not updating。
对于这些答案的route
部分,您可以将其添加到config / routes.rb:
resources :contacts, only: 'create'
这将产生以下路线:
contacts POST /contacts(.:format) contacts#create
然后您可以使用此操作(contacts#create)处理表单提交。
您的路线可能不会走到您认为会去的地方,因此@contact可能无效
运行“ rake route”并检查新路径。如果使用默认路径,则该路径为
new_contact_path ..和erb应该在文件中:app / views / contacts / new.html.erb
def new
@contact = Contact.new
end
将include ActiveModel::Model
添加到您的联系人文件中