2016-04-10 9 views
1

のための `simple '私はRuby on Railsの新機能であり、またstackoverflowの新機能です。私はシンプルフォームの宝石を使って訪問者がニュースレターにサインアップするコンタクトフォームを作成しています。 私はContactモデルとnew.html.erbビューファイルを持っています。問題は、連絡先リンクに移動したときに次のエラーメッセージが表示されることです。 "未定義のローカル変数またはメソッド"#<#:0xb44fae84> "私は間違っていますか?未定義のローカル変数またはメソッド#<#<クラス:0xb4ec5284>:0xb44ecae84>

注:私はRails 4.2.6とruby 2.2.1を使用しています。 ありがとうございます。

連絡先モデルは、次のようにapp/models/contact.rbにあります。

class Contact < ActiveRecord::Base 

has_no_table 

column :name, :string 
column :email, :string 
column :content, :string 

validates_presence_of :name 
validates_presence_of :email 
validates_presence_of :content 
validates_format_of :email, 
    :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i 
validates_length_of :content, :maximum => 500 


end 

及び以下に示すように、アプリ/ビュー/コンタクト/ new.html.erbのビューファイルである:エラーが言うよう

<% content_for :title do %>Contact<% end %> 
<h3>Contact</h3> 
<div class="form"> 
<%= simple_form_for @contact do |form| %> 
<%= simple.error_notification %> 
<%= form.input :name, autofocus: true %> 
<%= form.input :email %> 
<%= form.input :content, as: :text %> 
<%= form.button :submit, 'Submit', class: 'submit' %> 
<% end %> 
</div> 

答えて

0

simpleは、定義された方法ではありません。

使用フォームで簡単なフォームのエラー通知方法:

= f.error_notification

それとも、より良いカスタマイズのため、エラーをレンダリングする部分独自のヘルパーを追加します(私はこの例では、スリムなテンプレートエンジンを使用しています) :

_form_errors.html.slim:

- if resource.errors.any? 
    #error_explanation 
    .alert.alert-error 
     h2.error-header Please fix the following #{resource.errors.count} errors 
     ul 
     - resource.errors.full_messages.each do |msg| 
      li= "* #{msg}" 

フォームに

= render "partials/form_error", resource: @contact 
+0

アンソニーありがとうございました。私は "<%= simple.error_notification%>"ではなく "<%= form.error_notification%>"のようにnew.html.erbビューファイルを修正しましたが、まだ別のエラーがあります。 "未定義のメソッド' type 'for "string":String "ここで何か間違っていますか? –

関連する問題