0

入力ボックスを空白のままにすると。毎回このエラーが発生します。空白のときに新しいレコードを作成したくありません。そうでないときは、新しい記録を作りたいです。

この入力ボックスはネストされており、コントローラーのコードはエラーを回避するためにこのように記述されています

  def create
    # Check if there is any contact info added
    if params[:girl][:contact_attributes][:mail].empty?
      params[:girl].delete(:contact_attributes)
    end

    @girl = Girl.new(params[:girl])

    respond_to do |format|
      if @girl.save
        format.html { redirect_to @girl, notice: 'Girl was successfully created.' }
        format.json { render json: @girl, status: :created, location: @girl }
      else
        format.html { render action: "new" }
        format.json { render json: @girl.errors, status: :unprocessable_entity }
      end
    end
  end

ビューはこのようなものです

<%= form_for(@girl) do |f| %>
....

  <div class="field">
    <%= f.label :mail %><br />
    <%= f.fields_for :contact_attributes, @girl.contact do |contact| %>
    <%= contact.text_field :mail %>
    <% end %>
  </div>
....
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

私のモデル

class Girl < ActiveRecord::Base
  has_many :users
  has_one :contact
  accepts_nested_attributes_for :contact
  attr_accessible :id, :name_en, :name_ja, :gender_id, :contact_attributes, :photo, :tag_list

  searchable do 
    text :name_en, :name_ja
    text :contact do 
      contact.mail 
    end 
  end

    has_attached_file :photo,
     :styles => {
       :thumb=> "100x100>",
       :small  => "400x400>" } 

    acts_as_taggable_on :tags
    acts_as_commentable

end
4

3 に答える 3

1

設定する必要があります

@girl = Girl.new

直前のelseブロック内

format.html { render action: "new" }

エラーが発生するのは、新しいテンプレートをレンダリングし、その内部で form_for(@girl) が nil オブジェクト (@girl) を取得するためです。行をレンダリングするため<%= f.label :mail %><br />に、指定された @girl オブジェクトで mail メソッドを呼び出して、デフォルト値を取得しようとします。@girl オブジェクトは nil であり、新しいテンプレートをレンダリングする前に create アクションに設定されていないため、このエラーが発生します。

アップデート:

この投稿の最初の部分の回答で、あなたの状況を誤解しました。私の意見では、解決策は、新しいアクションをレンダリングするだけでなく、新しい女の子のパスにリダイレクトすることです。レンダリングはビューのレンダリングのみを行いますが、リダイレクトはフルスタックのリクエスト プロセスを行います。ルート new_girl_path が設定されていると仮定すると、置き換える必要がありformat.html { render action: "new" }ます

format.html { redirect_to new_girl_path }

実行`rake routesして、設定した名前付きルートを確認できます。

于 2012-07-13T06:32:36.217 に答える
1

私の問題は、次の数行のコードです。

if params[:girl][:contact_attributes][:mail].empty?
  params[:girl].delete(:contact_attributes)
end

ユーザー連絡先でメールが空の場合、連絡先属性が削除され、ユーザー オブジェクトのみが作成されています。

したがって、@girl.contact を呼び出すと、nil が返されます。

連絡先属性を削除した理由がわかりません。それでも削除したい場合は、もう 1 行追加する必要があります。

  if @girl.save
    format.html { redirect_to @girl, notice: 'Girl was successfully created.' }
    format.json { render json: @girl, status: :created, location: @girl }
  else
    #Assuming you have the association like: user has_one contact
    @user.build_contact
    format.html { render action: "new" }
    format.json { render json: @girl.errors, status: :unprocessable_entity }
  end

後もう一つ

<%= f.fields_for :contact_attributes, @girl.contact do |contact| %>

次のように簡単に書くことができます

<%= f.fields_for :contact do |contact| %>
于 2012-07-13T07:02:01.443 に答える
0

同じコード行を次のように置き換えます
<%= form_for( :girl, :url => {:action => :create}) do |f| %>

于 2012-07-13T06:40:52.023 に答える