2017-01-05 14 views
0

私はRuby on Railsの初心者で、現在私の最初のアプリを開発中です.Deviseはこのアクション(認証)を得るのに最高の宝石です。しかし、私は働く方法を理解していません。ユーザがログインしているかどうかを確認し、ログインページにリダイレクト

簡単なブログを作成することは簡単です。初心者ガイドを取得するには、私はRoRを使用する素晴らしいアイデアを持っていますが、Deviseでどのように動作させるかによって異なります。

私はこのURLを試してみてください。

Rails 3 + Devise: user_signed_in? for a different user in the database?(2010年から)あなたは、この言語で初心者開発者のための認証を使用する他の方法を持っている場合は

Rails, Devise. Disallow User actions unless AdminUser(私の問題をIs'nt)

に動作しません、私に教えてください

マイGemfile:

source 'https://rubygems.org' 

git_source(:github) do |repo_name| 
    repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") 
    "https://github.com/#{repo_name}.git" 
end 


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 
gem 'rails', '~> 5.0.1' 
# Use sqlite3 as the database for Active Record 
gem 'sqlite3' 
# Use Puma as the app server 
gem 'puma', '~> 3.0' 
# Use SCSS for stylesheets 
gem 'sass-rails', '~> 5.0' 
# Use Uglifier as compressor for JavaScript assets 
gem 'uglifier', '>= 1.3.0' 
# Use CoffeeScript for .coffee assets and views 
gem 'coffee-rails', '~> 4.2' 
# See https://github.com/rails/execjs#readme for more supported runtimes 
# gem 'therubyracer', platforms: :ruby 

# Use jquery as the JavaScript library 
gem 'jquery-rails' 
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 
gem 'turbolinks', '~> 5' 
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 
gem 'jbuilder', '~> 2.5' 
# Use Redis adapter to run Action Cable in production 
# gem 'redis', '~> 3.0' 
# Use ActiveModel has_secure_password 
# gem 'bcrypt', '~> 3.1.7' 

# Use Capistrano for deployment 
# gem 'capistrano-rails', group: :development 

group :development, :test do 
    # Call 'byebug' anywhere in the code to stop execution and get a debugger console 
    gem 'byebug', platform: :mri 
end 

group :development do 
    # Access an IRB console on exception pages or by using <%= console %> anywhere in the code. 
    gem 'web-console', '>= 3.3.0' 
    gem 'listen', '~> 3.0.5' 
    # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 
    gem 'spring' 
    gem 'spring-watcher-listen', '~> 2.0.0' 
end 

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 

# Devise for login 
gem 'devise' 

# Bootstrap 
gem 'bootstrap-sass', '~> 3.3.6' 

# Google Web Fonts 
gem 'google-webfonts-rails' 

事前に、

+0

deviseを使用する必要はありません。あなたのAuth in Railsを書くためのチュートリアルがたくさんあります。 –

+0

さて、他の開発者は "この宝石はあなたの要求を解決する:デビーズ、それを使う方法を学ぶ":/ –

+0

私の経験では、最初は簡単ですが、それをカスタマイズする時間が来たら本当に初心者ではありません。長期的には、実際にはそれほど複雑ではないため、認証が実際にどのように機能するのかを理解することができます。 –

答えて

0

組み込みのDeviseメソッドauthenticate_userを使用することはできません! (またはあなたのモデル名が何であれ)?通常、これはこのように、before_action文でコントローラレベルで実装されています

class Controller < ApplicationController 
    before_action :authenticate_user! 
end 

また前にアクションが適用されるアクションを制限することができます。その方法はあなたが望むものを正確に行い、まずユーザーがログインしているかどうかをチェックし、そうでなければサインアップページにリダイレクトします。

+0

私のモデル名は「ユーザー」(デフォルト)です.dashboard_controller.rbにあなたの提案を書いています。ユーザーがログインしていることを確認する)コンソール戻り値: '未定義のメソッド 'authenticate_user!' #の意味ですか? authenticate_with_http_digest ' –

+0

ドキュメントに沿ってDeviseを正しく設定しましたか? Userモデルとroutesファイルにいくつかのメソッドを追加する必要があります –

0

before_action :authenticate_user!モデルがユーザーであると仮定すると、あなたのコントローラ

にこれを追加//。お使いのモデルがユーザー以外の名前(ex:memberの場合)のユーザーauthenticate_member!

関連する問題