【问题标题】:Edit and update of user info. in RoR编辑和更新用户信息。在 RoR
【发布时间】:2021-07-18 22:35:15
【问题描述】:

我正在使用设计进行用户管理,所以它让用户使用默认的电子邮件和密码字段进行注册。

我在用户模型中添加了新字段/列,例如用户名、名称和公司。

所以我有一个配置文件视图,其中包含路由“/users/1”和一个 link_to 助手,它允许我编辑和更新我的用户信息。

默认情况下,我只能使用 users/edit route 来编辑我的用户信息。我如何管理一个新的或单独的编辑和更新选项,在我的个人资料视图中使用不同的路线说“/users/1/edit”。

我在此之前阅读了一些帖子,但没有帮助我。如果有人可以概述我应该做的事情。感谢阅读:))

编辑:

路由文件

root 'public#index'
devise_for :users
resources :users do
  put 'users/:id/edit', to: 'users#edit'
end

用户控制器

class UsersController < ApplicationController
  
  before_action :authenticate_user!
  after_action :verify_authorized
  before_action :set_user, only: %i[ show edit update ]

  def index
    @users = User.all
    authorize User
  end

  def show
    authorize @user
  end

  def edit
    if current_user == @user
      @user.update()
    end
  end

  def update
    authorize @user
    if @user.update(secure_params)
      redirect_to users_path, :notice => "User updated."
    else
      render 'edit'
    end
  end

  private

    def secure_params
        params.require(:user).permit(:designation, :company,      
        :username)
    end

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

end

在我看来要去编辑:

<% if current_user.id == @user.id %>
  <%= link_to 'Edit My profile', edit_user_path(@user), method: :edit, 
  class:"btn btn-primary" %>
<% end %>

【问题讨论】:

  • 你的意思是你想set up an admin role
  • @LamPhan 不是真的。只是每个用户都可以稍后更新自己的用户信息。就像我们可以编辑我们的个人资料以在社交媒体中完成用户详细信息一样。
  • 我不太确定你为什么想要那个?如果您想允许用户只更新他的个人资料而不更新任何其他人,为什么要在路由中指定用户的 ID?通过使用users/edit devise 可以找出当前登录的用户并编辑该用户。如果我误解了这个问题,请告诉我。
  • @crodev 我同意,你是对的。但我只是想知道如果我愿意并且如果可能的话,我可以如何根据自己的方便修改自定义设计路线。也许我错了。
  • 我的意思是,如果你真的想要user/:id/edit 路线,你可以自己做一个吗?在你的用户控制器中有一个user/:id/edit 指向users#edit 动作的路由,然后在那里执行逻辑。如果你对这种方法感兴趣,如果你愿意,我可以写一个答案。

标签: ruby-on-rails


【解决方案1】:

如果你真的想要一个路由 user/:id/edit 并且不使用 Devise 默认的 users/edit 路由(编辑当前登录的用户)。您可以执行以下操作:

假设您有一个用户控制器(如果您没有,请创建一个)并向其添加一个 edit 操作来处理编辑逻辑:

class UsersController < ApplicationController
  # other code
  def edit
    user = User.find_by(id: params[:id]) # this id will be passed through the route
    
    # Now here you need some authorization logic to prevent users from updating others.
    # If you use CanCanCan, Pundit or any other authorization gem then write
    # this logic there

    if current_user == user
      user.update() # do your update logic here with params you have

      # render some json or whatever you want
    else
      # render some error messages in format you are using
    end
      
  end
end

这是控制器逻辑,现在您需要在routes.rb 文件中注册此路由:

put 'user/:id/edit', to: 'users#edit'

这将编辑 ID 为:id 指定的用户。

再次注意:这不是我会采用的方法,我宁愿只使用 users/edit 路由并更新当前登录的用户,但你想要一个这样的例子,所以这样做你会

【讨论】:

  • 我没有收到“没有路线匹配 [POST]”/users/1/edit”。我有更新的问题。你能告诉我出了什么问题吗?@crodev
  • @goku 你设置了一个 PUT 路由——适用于带有 PUT 动词的表单提交,不适用于一个链接,它是一个 GET 请求。
  • @DaveNewton 在我看来是不是我弄错了?对不起,我还在想办法。
猜你喜欢
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多