【问题标题】:undefined method `profile' for nil:NilClassnil:NilClass 的未定义方法“profile”
【发布时间】:2014-05-23 07:15:01
【问题描述】:

我的编辑视图中出现错误“undefined method `profile' for nil:NilClass”。当我更新我的个人资料记录并在出现错误时重定向到编辑页面时,我遇到了呈现编辑视图的问题。这是我的 Profiles 控制器中的代码:

def edit
    @usermodel = current_usermodel
    @profile = current_usermodel.profile
    if current_usermodel.stripe_access_token.blank?
        flash.keep[:error]  = "Please, connect your #{view_context.link_to('Stripe Account', usermodel_omniauth_authorize_path(:stripe_connect))} to accept payments".html_safe
    end 
end


def update
    @profile = current_usermodel.profile
    respond_to do |format|
    if @profile.update_attributes(params[:profile])
        format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
        format.json { head :no_content }
    else
        format.html { render action: "edit" }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
    end
end

在视图中,我有以下形式:

<%= simple_form_for @usermodel.profile do |f| %>
<%= render "pages/error_display", :target => @usermodel.profile %>
</br>
    <b>Business Info </b>
...

@usermodel 为 nil,从控制器看来,情况并非如此

【问题讨论】:

  • 你应该检查为什么你的current_usermodel返回nil。

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

尝试添加

@usermodel = current_usermodel

也更新动作。

【讨论】:

    猜你喜欢
    • 2011-07-27
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多