9

ユーザーがプロファイルを更新しようとすると、アプリがランダムに「nil:NilClassの未定義のメソッド`map'」エラーをスローしているようです。

しかし、奇妙なのは、更新時にエラーが発生すると言っているのに、エラー行が実際に表示されていることです。

完全なエラー:

users#update (ActionView::TemplateError) "undefined method `map' for nil:NilClass"

On line #52 of app/views/users/edit.html.erb

Line 52: <%= options_from_collection_for_select(@networks_domestic, 'id', 'name', @user.network_id) %>

そして、ここに最近のエラーからのパラメータがあります:

{"user"=>{"email_notify"=>"email@example.com", "network_id"=>"", 
"password_confirmation"=>"[FILTERED]", "mobile"=>"", "password"=>"[FILTERED]", 
"email"=>"email@example.com"}, "action"=>"update", "_method"=>"put", "id"=>"5089", 
"controller"=>"users"}

正直なところ、どこから探し始めるのかわからない。IEからは同じ情報を更新できるが、Firefoxからは更新できないとユーザーに言われました。そして、同じ情報を使用すると、問題なく更新できます。だから、私は困惑しています。

4

2 に答える 2

19

Best guess...

Your edit function properly defines @networks_domestic so everything is great until you encounter an error in the update function and call render :action => "edit".

Render does not call the edit function but rather just renders the edit view. So, in the case of a failed update you will have to define @networks_domestic before returning from update.

So say, for example, you have the following:

  def edit
    @user = User.find(params[:id])
    @networkd_domestic = [...]
  end

  def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        flash[:notice] = "User was successfully updated."
        format.html { redirect_to(admin_users_url) }
      else
        format.html { render :action => "edit" }
      end
    end
  end

You will get the error you are describing because @networkd_domestic is not defined in the error condition in the update function.

Add @networkd_domestic = [...] before the edit render and you should be good.

于 2009-11-12T19:27:07.580 に答える
6

@networks_domenticコントローラーに正しく設定されていますか?<%= @networks_domestic.inspect %>52 行目の直前に追加して、結果を確認します。コントローラーで確認し、ビュー@networkd_domestic.nil?に送信しないことを確認してくださいnil

編集:

ソースを見ると、渡したコレクション (この場合は @networks_domestic) をoptions_from_collection_for_select呼び出していることがわかります。map

于 2009-11-12T17:36:54.590 に答える