2015-08-25 133 views
0

我花了好幾天的時間尋找這個,我在創建用戶時創建了一個用戶名錶,並且在項目後期意識到我不需要擁有這些用戶表,而只是讓他們使用電子郵件。我嘗試過不同的事情,但沒有運氣。請注意:我不使用DEVISE。我也不想使用Devise。這裏是我的代碼,感謝先進的如果你能幫助我!不,這不是與另一個問題不一樣的問題,因爲我讀了這個問題,並且做了正確的答案並且沒有運氣。如何讓用戶使用他們的電子郵件或用戶名登錄?

這是users_controller.rb,

class UsersController < ApplicationController 
    before_action :set_user, only: [:show, :edit, :update, :destroy] 
    after_action :signed_in_after_register, only: :create 

    def index 
    @users = User.all 
    @user = User.find(session[:user_id]) 
    @user = User.search(params[:search]) 
    end 

    def dashboard 
    @user = User.find(session[:user_id]) unless session[:user_id] == "" 
    redirect_to login_path, notice: "You're not logged in" unless @user 
    @posts = @user.posts.order("created_at DESC").limit(3) 
    @comment = Comment.new 
    @post = Post.new 
    end 

    def newsfeed 
    @user = User.find(session[:user_id]) unless session[:user_id] == nil 
    redirect_to login_path, notice: "You're not logged in" unless @user 
    @posts = @user.posts.order("created_at DESC").limit(3) 
    end 

    def nav 
    @user = User.find(session[:user_id]) 
    end 
    def posts 
    @user = User.find(session[:user_id]) 
    @posts = @user.posts 
    end 

    def destroy 
    @user = User.find(session[:user_id]) unless session[:user_id] == "" 
    redirect_to login_path, notice: "You're not logged in" unless @user 
    end 

    def welcome 
    @user = User.find(session[:user_id]) unless session[:user_id] == "" 
    redirect_to login_path, notice: "You're not logged in" unless @user 
    end 

    def show 
    @user = User.find(session[:user_id]) unless session[:user_id] == "" 
    redirect_to login_path, notice: "You're not logged in" unless @user 
    @posts = @user.posts.order("created_at DESC").limit(3) 
    @comment = Comment.new 
    @post = Post.new 
    end 

    def new 
    @post = Post.new(params[:post_id]) 
    @user = User.new 
    end 

    def edit 

    end 

    def create 
    @user = User.new(user_params) 

    respond_to do |format| 
     if @user.save 
     format.html { redirect_to dashboard_path, notice: 'User was successfully created!' } 
     format.json { render :show, status: :created, location: @user } 
     else 
     format.html { render :new } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 

    respond_to do |format| 
     if @user.update(user_params) 
     format.html { redirect_to dashboard_path, notice: 'User was successfully updated.' } 
     format.json { render :show, status: :ok, location: @user } 
     else 
     format.html { render :edit } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @user.destroy 
    respond_to do |format| 
     format.html { redirect_to users_url, notice: 'User was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 

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

    def user_params 
     params.require(:user).permit(:first_name, :last_name, :name, :bio, :password, :password_confirmation, :email, :age, :profile_picture, :post, :body) 
    end 
end 

這裏是我的user.rb型號,

class User < ActiveRecord::Base 
    has_secure_password 
    validates :email, :first_name, :last_name, :name, presence: true, uniqueness: true 
    validates_inclusion_of :age, in: 10..100 
    validates :password, presence: true 
    has_many :posts 
    has_attached_file :profile_picture, :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
    :default_url => "app/assets/images/missing.png", 
    :path => ":rails_root/public/system/:class/:attachment/:id_partition/:style/:filename" 
    validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/.*\Z/ 

end 

這是我_form.html.erb,

<%= form_for @user, html: { multipart: true } do |f| %> 
    <% if @user.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2> 

     <ul> 
     <% @user.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
<div class="sign_up_form_2"> 
    <div class="field"> 
    <%= f.label :first_name %> 
    <%= f.text_field :first_name, placeholder: "First Name", class: "form-control" %> 
    </div> 
    <div class="field"> 
    <%= f.label :last_name %> 
    <%= f.text_field :last_name, placeholder: "Last Name", class: "form-control" %> 
    </div> 
    <div class="field"> 
    <%= f.label :email %><br> 
    <%= f.text_field :email, placeholder: "Email", class: "form-control" %> 
    </div> 
    <div class="field"> 
    <%= f.label :age %><br> 
    <%= f.number_field :age, placeholder: "Age", class: "form-control" %> 
    </div> 
    <div class="field"> 
    <%= f.label :password %><br> 
    <%= f.password_field :password, placeholder: "Password", class: "form-control" %> 
    </div> 
    <div class="field"> 
    <%= f.label :password_confirmation %><br> 
    <%= f.password_field :password_confirmation, placeholder: "Password Confirmation", class: "form-control" %> 
    </div><br> 
    <div class="field"> 
    <%= f.label :bio %> 
    <%= f.text_area :bio, placeholder: "Write something!", class: "form-control" %> 
    </div> 
    <div class="field"> 
    <%= f.label :profile_picture %> 
    <%= f.file_field :profile_picture %><br> 
    </div> 
    <div class="actions"> 
    <%= f.submit "Create Account", class: "btn btn-default btn-sign-up" %> 
    </div> 

<% end %> 
</div> 

這在我sessions_controller.rb中,

class SessionsController < ApplicationController 
    before_action :checked_logged_in, only: :login 
    def login 

    end 

    def create 
    user = User.find_by(name: params[:name]) 
    if user and user.authenticate(params[:password]) 
     session[:user_id] = user.id 
     redirect_to dashboard_path 
    else 
     redirect_to login_url, notice: "Invalid User or Password Combination" 
    end 

    end 

    def destroy 
    if session[:user_id] 
     session[:user_id] = nil 
     redirect_to login_url 

    end 

    end 
    private 

    def checked_logged_in 
    if session[:user_id] 
     redirect_to dashboard_path, notice: "You are already logged in" 

    end 

    end 
end 

以防萬一,這裏是我的Gemfile,

source 'https://rubygems.org' 

gem 'rails', '4.2.0' 

gem 'sqlite3' 

gem 'sass-rails', '~> 5.0' 

gem 'uglifier', '>= 1.3.0' 

gem 'coffee-rails', '~> 4.1.0' 

gem 'bcrypt-ruby' 

gem "twitter-bootstrap-rails" 

gem "paperclip", "~> 4.2" 

gem 'jquery-rails' 

gem 'turbolinks' 

gem 'jbuilder', '~> 2.0' 

gem 'sdoc', '~> 0.4.0', group: :doc 

gem 'imagemagick-identify', '~> 0.0.1' 

gem 'bootstrap-sass', '~> 3.3.3' 

group :development, :test do 

    gem 'byebug' 

    gem 'web-console', '~> 2.0' 
end 

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 

我願意,如果需要任何進一步解釋,我也很願意發佈任何代碼,你可能需要查看幫助我!感謝您的時間!

+0

您可能需要查看[rails基本認證指南](http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic.html) –

+0

可能的重複[如何允許用戶使用用戶\ _name或電子郵件使用Rails](http://stackoverflow.com/questions/31929897/how-to-allow-a-user-to-login-with-user-name-or-email-using-rails) – Sculper

+0

我讀過那臺Sculper,但在我的最後沒有運氣,所以我想我會問自己。 :) – genobambino

回答

1

在SessionsController#創建改變這一行:

user = User.find_by(name: params[:name]) 

這樣:

user = User.find_by(name: params[:name]) || 
     User.find_by(email: params[:name]) 

或者一些其他的變種,着眼於兩個領域:

user = User.where(["name = ? OR email = ?", params[:name], params[:name]]).first 

你也可以查看params[:name],如果它裏面有'@',請用電子郵件查看,否則用戶名稱。

+0

謝謝這麼多人!我會評價你的答案是正確的!這意味着很多,你給我嘗試的第三行代碼雖然不起作用,但另外兩行代碼是user = User.find_by(name:params [:name])|| User.find_by(email:params [:name])謝謝! – genobambino

+0

@genobambino剛剛注意到第三行中缺少']'。它現在應該可以工作。 –

+0

謝謝你!那意義重大! – genobambino

相關問題