不使用编辑操作编辑用户列

发布于 2024-12-18 17:23:39 字数 1073 浏览 3 评论 0原文

使用 Rails 3.1 和 authlogic。我有一个用户模型,我想将表单分成几个部分,这样我就不会在一个表单中混合配置文件、帐户设置、更改密码。这些列都在同一个表中。

问题是,每当它没有通过任一操作的验证时,我只能返回一条闪存错误消息。我想使用正常的错误消息嵌入到同一操作中,但因为我使用的是 redirect_to :back,所以我无法存储任何错误消息。

我应该怎么做才能以本机方式使用 Rails 错误消息?

这是我的代码:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "Welcome!"
      redirect_back_or_default account_url
    else
      render :action => :new
    end
  end

  def show
    @user = @current_user
    @header_title = 'Account settings'
  end

  def edit
    @user = @current_user
  end

  def update
    @user = @current_user # makes our views "cleaner" and more consistent
    if @user.update_attributes(params[:user])
      flash[:notice] = "Account settings updated."
      redirect_to :back
    else
      flash[:error] = "Errors found. Please check again."      
      redirect_to :back
    end
  end

  def password
    @user = @current_user
  end
end

Using Rails 3.1 and authlogic. I have a User model, where I want to separate the forms to several parts, so that I don't mix the profile, account settings, change password in one form. These columns are all in the same table.

Problem with this is that whenever it doesn't pass the validation on either of the action, I could only return a flash error message. I want to use the normal error messages to be embedded on the same action, but because I am using redirect_to :back, I can't store any error messages.

What should I do to use the Rails error messages the native way?

Here are my code:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "Welcome!"
      redirect_back_or_default account_url
    else
      render :action => :new
    end
  end

  def show
    @user = @current_user
    @header_title = 'Account settings'
  end

  def edit
    @user = @current_user
  end

  def update
    @user = @current_user # makes our views "cleaner" and more consistent
    if @user.update_attributes(params[:user])
      flash[:notice] = "Account settings updated."
      redirect_to :back
    else
      flash[:error] = "Errors found. Please check again."      
      redirect_to :back
    end
  end

  def password
    @user = @current_user
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2024-12-25 17:23:39

由于错误消息存储在实例变量上,并且这些实例变量不会出现在用户浏览器在重定向后发出的 GET 请求中,因此您应该渲染您希望从更新操作中将它们带回的模板。

由于您有可以 PUT 更新的不同部分,因此您可以检查更新操作中的引用以决定要呈现哪个模板。

或者(也许更干净),您可以为这些不同的部分创建新资源。考虑名称可能很困难,但这将使您能够分离关注点并可能编写更少的代码。

Because error messages are stored on instance variables, and those instance variables won't be around for the GET request the user's browser will make after a redirect, you should instead render the template you wish to take them back to from the update action.

Since you have different sections that can possibly PUT to update, you might inspect the referer within the update action in order to decide which template to render.

Alternatively (and perhaps, more cleanly), you could create new resources for these different sections. Thinking of names may be difficult, but that would allow you to separate concerns and potentially write less code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文