无法编辑用户 - 字段不能为空?

时间:2017-05-06 23:16:58

标签: ruby-on-rails ruby-on-rails-4 model-view-controller omniauth

作为最后一分钟,我在我的Rails应用程序中添加了Omniauth策略(Facebook,Google,LinkedIn和Github)。为了做到这一点,我让它以“身份”而不是“用户”身份登录用户。在添加之后,出现了一些问题,而我遇到的一个问题就是编辑用户。当我点击提交以编辑用户时,我在视图中收到两个错误:

Github confirmation doesn't match Github
Github can't be blank

我的服务器日志如下:

Started PATCH "/users/33" for 127.0.0.1 at 2017-05-06 17:53:46 -0500
Processing by UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"qv+iPRvBjfSQFBk4a75j4pTnr3hh9QwiBX+XTAgHxxQ=", "user"=>{"email"=>"name+test24@gmail.com", "role"=>"student", "tries"=>"0", "full_name"=>"First Last", "github"=>"http://www.github.com/firstlast24", "github_confirmation"=>"http://www.github.com/firstlast24", "university_id"=>"8", "program_id"=>"2", "time_zone"=>"Pacific Time (US & Canada)"}, "commit"=>"Submit", "id"=>"33"}
  ActiveRecord::SessionStore::Session Load (0.4ms)  SELECT  "sessions".* FROM "sessions"  WHERE "sessions"."session_id" = '211808dcc204d0de865bb64272f6da52'  ORDER BY "sessions"."id" ASC LIMIT 1
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = $1 LIMIT 1  [["id", 33]]
  FormUser Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 2  ORDER BY "users"."id" ASC LIMIT 1
   (0.1ms)  BEGIN
  Identity Load (0.3ms)  SELECT  "identities".* FROM "identities"  WHERE "identities"."user_id" = $1 AND "identities"."provider" = 'github'  ORDER BY "identities"."id" ASC LIMIT 1  [["user_id", 33]]
  CACHE (0.0ms)  SELECT  "identities".* FROM "identities"  WHERE "identities"."user_id" = $1 AND "identities"."provider" = 'github'  ORDER BY "identities"."id" ASC LIMIT 1  [["user_id", 33]]
  CACHE (0.0ms)  SELECT  "identities".* FROM "identities"  WHERE "identities"."user_id" = $1 AND "identities"."provider" = 'github'  ORDER BY "identities"."id" ASC LIMIT 1  [["user_id", 33]]
  CACHE (0.0ms)  SELECT  "identities".* FROM "identities"  WHERE "identities"."user_id" = $1 AND "identities"."provider" = 'github'  ORDER BY "identities"."id" ASC LIMIT 1  [["user_id", 33]]
   (0.2ms)  ROLLBACK
  University Load (0.3ms)  SELECT  "universities".* FROM "universities"  WHERE "universities"."id" = $1 LIMIT 1  [["id", 8]]
  University Load (0.2ms)  SELECT "universities".* FROM "universities"
  Program Load (0.3ms)  SELECT  "programs".* FROM "programs"  WHERE "programs"."id" = $1 LIMIT 1  [["id", 2]]
  Program Load (0.3ms)  SELECT "programs".* FROM "programs"
  Rendered users/_form.html.erb (13.0ms)
  Rendered users/edit.html.erb within layouts/application (14.8ms)
Completed 200 OK in 102ms (Views: 87.5ms | ActiveRecord: 2.8ms)
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT

我认为我的应用现在无法区分用户和身份。任何建议将不胜感激!

users_controller

class UsersController < ApplicationController
    before_action :set_user, only: [:edit, :update, :destroy]
    before_filter :authenticate_user!
    before_action :check_if_admin

    # GET /users/1/edit
    def edit
        @user = User.find(params[:id])
        @programs = Program.all
        @universities = University.all
    end

    # PATCH/PUT /users/1
    # PATCH/PUT /users/1.json
    def update
        respond_to do |format|
            if @user.update(user_params)
                format.html { redirect_to @user, notice: 'User was successfully updated.' }
                format.json { render :show, status: :ok, location: @user }
            else
                @universities = University.all
                @programs = Program.all
                format.html { render :edit }
                format.json { render json: @user.errors, status: :unprocessable_entity }
            end
        end
    end

    private
    def user_params
        params.require(:user).permit(:email, :role, :full_name, :university_id, :program_id, :time_zone, :github, :github_confirmation, :tries)
    end
end

表单视图

<%= form_for(@user) do |f| %>

    {{ other fields }}

    <div class="field">
        <%= f.label :github %><br />
        <%= f.url_field :github, placeholder: 'http://www.github.com/example' %>
    </div>

    <div class="field">
        <%= f.label :github_confirmation %><br />
        <%= f.url_field :github_confirmation, placeholder: 'http://www.github.com/example' %>
    </div>

    <div class="field">
        <%= f.label :university_id %><br>
        <%= f.select :university_id, options_from_collection_for_select(@universities, 'id', 'name', @user.university.id), {include_blank: true}, {:class => 'select2'} %>
    </div>

    <div class="field">
        <%= f.label :program_id %><br>
        <%= f.select :program_id, options_from_collection_for_select(@programs, 'id', 'name', @user.program.id), {include_blank: true}, {:class => 'select2'} %>
    </div>

    <div class="field">
        <%= f.label :time_zone %></span><br>
        <%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones, {include_blank: true}, {:class => 'select2'} %>
    </div>

    <div class="actions">
        <%= f.submit 'Submit', :class => 'btn btn-primary' %>
    </div>
<% end %>

身份模型

class Identity < ActiveRecord::Base
  belongs_to :user
  validates_presence_of :uid, :provider
  validates_uniqueness_of :uid, :scope => :provider

  def self.find_for_oauth(auth)
    identity = find_by(provider: auth.provider, uid: auth.uid)
    identity = create(uid: auth.uid, provider: auth.provider) if identity.nil?
    identity.accesstoken = auth.credentials.token
    identity.refreshtoken = auth.credentials.refresh_token
    identity.name = auth.info.name
    identity.email = auth.info.email
    identity.nickname = auth.info.nickname
    identity.image = auth.info.image
    identity.phone = auth.info.phone
    identity.urls = (auth.info.urls || "").to_json
    identity.save
    identity
  end
end

用户模型

class User < ActiveRecord::Base

    validates :github, confirmation: { case_sensitive: false }
    validates :github_confirmation, presence: true, :on => :create, confirmation: { case_sensitive: false }
    has_many :identities
    def github
        identities.where( :provider => "github" ).first
    end

    def github_client
        @github_client ||= Github.client( access_token: github.accesstoken )
    end
end

如果您需要查看其他代码,请告知我们,谢谢您的帮助!

0 个答案:

没有答案