【问题标题】:Update User Info with restful_authentication plugin in Rails?在 Rails 中使用 restful_authentication 插件更新用户信息?
【发布时间】:2011-02-22 15:33:16
【问题描述】:

我想让用户能够使用 rails 中的 restful_authentication 插件更改他们的帐户信息。

我将这两种方法添加到我的用户控制器中:

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

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

      # Only update password when necessary
      params[:user].delete(:password) if params[:user][:password].blank?

      respond_to do |format|
        if @user.update_attributes(params[:user])
          flash[:notice] = 'User was successfully updated.'
          format.html { redirect_to(@user) }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
        end
      end
    end

另外,我将 new.html.erb 复制到 edit.html.erb。考虑到资源已经在 routes.rb 中定义,我希望它可以轻松工作,但不知何故,当我单击保存按钮时,它会调用 create 方法,而不是 update,使用 POST HTTP 请求。 之后,它会立即自动退出会话。

有什么想法吗?

【问题讨论】:

  • 可能,如果您显示您的edit.html.erb,这将有助于了解发生了什么。

标签: ruby-on-rails rest ruby-on-rails-plugins restful-authentication


【解决方案1】:

是否有可能在您的edit 方法中找不到所选用户? 然后@user 为空,您的表单认为您正在创建一个新用户。

尝试在edit方法中添加调试输出,像这样:

def edit
  @user = User.find(params[:id])
  logger.debug "Found user = #{@user.inspect}"
end

并检查您的日志消息是否显示用户已找到。

【讨论】:

    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 2011-10-30
    相关资源
    最近更新 更多