rails 通过电子邮件更新用户记录

rails updating a user's record by their email

我有一个带有账户模型和账户经理(用户)的应用程序。

我希望客户经理能够通过输入电子邮件将其他用户添加到帐户中。

客户经理的帐号id应该添加到新用户的记录中。

我的模型看起来像

class Account < ActiveRecord::Base
  has_many users
  ...
end

class User < ActiveRecord::Base
  belongs_to account
  ...
end

在控制台中,它相当简单。

u = User.find(2) 
u.update_attributes(account_id: 1)

但我不确定如何在网站上执行此操作。

我有下面的部分,它呈现得很好,但是当我在框中放入一封电子邮件时,没有任何反应。

我收到消息,用户未添加。

<h4>Add User</h4>
<div class = "row">
    <div class="col-md-6 col-md-offset-3">
        <%= form_for(:add_user, url: add_users_path) do |f| %>
      <%= f.hidden_field @account = Account.find(params[:id]) %>
      <%= f.label :email %>
      <%= f.email_field :email, class:'form-control' %>
      <%= f.submit "Submit", class: "btn btn-primary" %>
  <% end %>
    </div>
</div>

我不一定 want/need 路由到另一个页面,例如 "url: add_users_path" 但是如果没有那一行,代码就会中断并且不会呈现。

我将此添加到 user.rb

def add_user_to_account
  update_attribute(:account_id)
end

我也创建了这个控制器

class AddUsersController < ApplicationController
    before_action :get_user,   only: [:edit, :update]

  def new
  end

  def edit
  end

  def create
    @user = User.find_by(email: params[:email])
    if @user
        @user.add_user_to_account
        flash[:info] =  "Added User to Account"
        redirect_to accounts_url
    else 
        flash[:danger] = "User NOT Added"
        redirect_to accounts_url
    end 
  end

  def update
    if params[:user][:account_id].empty?
      @user.errors.add(:account_id, "can't be empty")
      render 'edit'
    elsif @user.update_attributes(user_params)
      log_in @user
      flash[:success] = "Account Updated"
      redirect_to @user
    else
      render 'edit'
    end
  end  


  private

    def user_params
      params.require(:user).permit(:account_id, :user_id)
    end

    def get_user
      @user = User.find_by(email: params[:email])
    end
end

我走在正确的轨道上吗?有一个更好的方法吗?

更新:添加了架构

create_table "accounts", force: :cascade do |t|
    t.string   "name"
    t.integer  "account_manager_id"
    t.integer  "subscription_id"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
  end

create_table "users", force: :cascade do |t|
   t.string   "name"
    t.string   "password_digest",   null: false
    t.string   "remember_digest"
    t.string   "email",             null: false
    t.string   "activation_digest"
    t.boolean  "activated"
    t.datetime "activated_at"
    t.string   "reset_digest"
    t.datetime "reset_sent_at"
    t.datetime "created_at",        null: false
    t.datetime "updated_at",        null: false
    t.boolean  "access_granted"
    t.boolean  "requested_access"
    t.string   "slug"
    t.integer  "account_id"
  end

我认为你的参数有误。

您正在使用 params[:email] 而不是 params[:add_user][:email] 通过电子邮件查找用户

在你的控制器中

class UsersController < ApplicationController
  ...
  def add_users
     user = User.find_by(email: params[:add_user][:email])
     if user
       user.add_user_to_account
       ...
     else
       ...
     end
  end 
end