简体   繁体   English

如何路由多个页面以“编辑”用户帐户信息(Rails)

[英]How to Route Multiple Pages to 'Edit' a Users Account Information (Rails)

I currently have a Ruby on Rails application with a typical User model. 我目前有一个带有典型用户模型的Ruby on Rails应用程序。 I would like to allow users to edit their own account information, and change their email or password. 我想允许用户编辑自己的帐户信息,并更改他们的电子邮件或密码。 However, I would like to have the corresponding forms for this on two different pages. 但是,我想在两个不同的页面上有相应的表格。

There is a users_controller.rb which already has a show , new , create , and edit path. 有一个users_controller.rb ,它已经有一个shownewcreateedit路径。 The problem is that using the edit path to update the User database would require all forms to be on the same page, which is inconvenient for users who only wish to update their email or password, and not both. 问题是使用edit路径update用户数据库将要求所有表单都在同一页面上,这对于只希望更新其电子邮件或密码的用户来说不方便,而不是两者都不方便。 Essentially, I need that edit path to be available on multiple pages. 基本上,我需要在多个页面上提供该edit路径。

Any suggestions? 有什么建议么?

Split the form up into separate .html.erb files (like email.html.erb and password.html.erb ) and in your controller write something like: 将表单拆分为单独的.html.erb文件(如email.html.erbpassword.html.erb ),并在控制器中编写如下内容:

def edit
  ...
  if params[:email]
    render 'email'
  end
  if params[:password]
    render 'password'
  end
end

Obviously the conditions can be whatever you want. 显然,条件可以是你想要的任何东西。

Not possible. 不可能。 Only one action per named route. 每个命名路由只有一个动作。

You could use one named route plus an extra parameter to tell the controller which form (or parts of a form) to render: 您可以使用一个命名路由加上一个额外参数来告诉控制器要呈现哪种表单(或表单的一部分):

named route                                  path
edit_user_path(@user, :form => "info")       mysite.com/users/1/edit?form=info
edit_user_path(@user, :form => "acct")       mysite.com/users/1/edit?form=acct
edit_user_path(@user)                        mysite.com/users/1/edit

Then read this parameter and switch between templates depending on the value. 然后读取此参数并根据值在模板之间切换。 No parameter will render the default view containing the full form: 没有参数将呈现包含完整表单的默认视图:

def edit
  # ...
  case params[:form]
  when "info"
    render :template => "info_only"
  when "acct"
    render :template => "name_and_password"
  else
    render :action => :edit
  end
end

I assume this is just for UX and not for security. 我认为这只是用于UX而不是用于安全性。 All of these separate forms will PUT to the same update action, meaning all attributes accessible in one form (eg. password) will be accessible in another form. 所有这些单独的表单将PUT到相同的更新操作,这意味着可以以另一种形式访问以一种形式(例如密码)访问的所有属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM