7

私は次のモデルを持っています:

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

私が現在持っているものが機能しない理由は何ですか?

4

3 に答える 3

14

config/routes.rb の正しいルートに加えて、モデルには次の 2 つの指示も必要です。

include ActiveModel::Conversion
extend  ActiveModel::Naming

この質問を見てください: form_for without ActiveRecord, form action not updates

これらの回答の一部については、routeこれを config/routes.rb に追加できます。

resources :contacts, only: 'create'

これにより、次のルートが生成されます。

contacts POST /contacts(.:format)  contacts#create

次に、このアクション (contacts#create) を使用して、フォームの送信を処理できます。

于 2013-02-13T20:09:58.457 に答える
0

あなたのルートはおそらくあなたが思っている場所には行かないので、 @contact はおそらく nill です

「rake routes」を実行し、新しいパスを確認します。デフォルトを使用している場合、ルートは

new_contact_path .. erb はファイルにある必要があります: app/views/contacts/new.html.erb

  def new
    @contact = Contact.new
  end

于 2013-02-13T19:53:31.630 に答える