我想检查设备 IP 地址访问我的网站的次数(root to=>home#index),如果该地址多次访问我的网站,则执行一段代码(任何内容)。我如何使用 Rails 实现这一点?换句话说,我如何访问并设置一个变量来存储用户 ip_address 登录的数量index action?根页面是 home#index。为了澄清更多,用户不必是注册用户,我只想通过计算机 ip 地址跟踪。我需要注释掉的帮助索引操作中的单词。有设计,current_sign_in_ip但我不确定这是否是为了解决这个问题场景,因为我对设计和导轨非常陌生。非常感谢!
控制器
class HomeController < ApplicationController
def index
#access user ip address
#set a variable to increment number of times the address come to a site
# if no_of_ip_login is_greater_than_1 #In other words if it is a returning user
redirect_to :controller=>"home" ,:action=>"show"
end
end
Run Code Online (Sandbox Code Playgroud) ruby-on-rails request devise ruby-on-rails-3 ruby-on-rails-3.1
我正在使用 Devise 进行身份验证,并想在注册表中添加验证码,我已经阅读过有关验证码的内容,有人可以告诉我如何集成两者吗?
authentication ruby-on-rails recaptcha devise ruby-on-rails-3
我正在使用 Rails 4 构建 API,并使用Devise进行身份验证。我也覆盖了SessionsController登录用户。我有一个资源被调用user并设置了适当的devise_for路由。该create操作如下:
def create
self.resource = warden.authenticate(auth_options)
## Handle error / success json based on resource
.......
end
Run Code Online (Sandbox Code Playgroud)
由于资源是user如果我curl对数据参数进行处理,例如:
{"user": {"email": "hello@example.org", "password": "password"} }
Run Code Online (Sandbox Code Playgroud)
有用。
我只想知道如何自定义参数,以便devise/warden能够适当地选择它。即,我想发送以下请求数据,它应该进行身份验证:
{"email": "hello@example.org", "password": "password"}
Run Code Online (Sandbox Code Playgroud) 如何使用 devise (3.4.1) 获取 Rails (4.2.1) 应用程序中的 Devise 范围列表?
就像是:
> devise_scopes
#=> [User, Admin]
Run Code Online (Sandbox Code Playgroud) In my ruby on rails application I've a problem for the user logout with devise
I've this navbar:
<% if current_user %>
<nav class="top-bar" data-topbar role="navigation">
<ul class="title-area">
<li class="name">
<h1><%= link_to 'Prova CMS', articles_path %></h1>
</li>
<li class="toggle-topbar menu-icon"><a href="#"></a></li>
</ul>
<section class="top-bar-section">
<ul class="right">
<li><%=link_to 'Gestisci Articoli', articles_path %></li>
<li><%=link_to 'Gestisci Categorie', categories_path %></li>
<li class="has-dropdown">
<a href="#"><%= current_user.email %></a>
<ul class="dropdown">
<li><%= link_to "Logout", destroy_user_session_path, :method => :delete %></li>
</ul>
</li>
</ul>
<% if current_page?(articles_path) %>
<ul …Run Code Online (Sandbox Code Playgroud) 我有一个设计用户,里面我有 admin 作为布尔值默认为 false。我如何在我的 ruby on rails 应用程序中修复我的路线,以便它仅允许将 admin 设为 true 的管理员访问某些页面。
更新:我更改了我得到的第一个答案,其中说要创建一个 is_admin?方法并指定我的控制器中的操作。但是,当我这样做时,我得到一个:
undefined method `admin' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
更新 2:
产品控制器:
class ProductsController < ApplicationController
before_action :is_admin?, only: [:edit, :update, :destroy]
before_action :set_product, only: [:show]
Run Code Online (Sandbox Code Playgroud)
应用控制器:
def is_admin?
if signed_in?
redirect_to root_path unless current_user.admin
end
end
Run Code Online (Sandbox Code Playgroud) 我正在使用 Rails 4.2 和设计。每当我尝试使用重置密码页面时,都会出现以下错误:
Nil location provided. Can't build URI. on Password reset
Run Code Online (Sandbox Code Playgroud)
在生产中,它带有 500 内部服务器错误。
带有重置令牌的电子邮件确实会发送。如果用户打开电子邮件中的链接,他们可以返回站点更改密码。尽管如此,单击“重置密码按钮”时总会出现 500 错误。
我以前用过很多次设计,从来没有遇到过这个问题。我怀疑它可能与本教程有关:
并在我的应用程序控制器中显式编写resource_name,resource和devise_mapping方法,但它们并没有干扰任何其他设计路线,即使将它们注释掉,错误仍然存在。
编辑:
我并没有PasswordController坚持使用 Devise 的大多数默认设置。我也没有respond_with在我的应用程序中的任何地方使用。
我的设计模型如下所示:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :confirmable, :lockable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:stripe_connect]
validates :username, presence: true
validates :time_zone, presence: true
has_one :teacher, dependent: :destroy
has_many :bookings, foreign_key: :student_id
has_many :reviews, foreign_key: :student_id, dependent: :destroy
has_many :messages, dependent: :destroy …Run Code Online (Sandbox Code Playgroud) 这里似乎有一个类似的问题,但没有人能够回答。似乎也有些不同。
我正在从 Rails 4.2.4 升级到 Rails 5.0.0.1。当我尝试在 30 天后运行密码过期时,它会执行更新操作,其中第二行出现问题:
def update
resource.extend(Devise::Models::DatabaseAuthenticatablePatch)
if resource.update_with_password(resource_params)
warden.session(scope)['password_expired'] = false
set_flash_message :notice, :updated
sign_in scope, resource, :bypass => true
redirect_to stored_location_for(scope) || :root
else
clean_up_passwords(resource)
respond_with(resource, action: :show)
end
end
Run Code Online (Sandbox Code Playgroud)
这是运行的模块:
module Devise
module Models
module DatabaseAuthenticatablePatch
def update_with_password(params, *options)
new_password = params[:password]
new_password_confirmation = params[:password_confirmation]
result = if new_password.present? && new_password_confirmation.present?
update_attributes(params, *options)
else
self.assign_attributes(params, *options)
self.valid?
self.errors.add(:password, new_password.blank? ? :blank : :invalid)
self.errors.add(:password_confirmation, new_password_confirmation.blank? ? :blank : :invalid)
false
end …Run Code Online (Sandbox Code Playgroud) 我正在创建一个rails api,我希望资源处于/api/v1/路由状态,包括注册和身份验证。
我创建了以下路线,为我提供了 Devise 所需的路线:
config/routes.rb
namespace :api, defaults: { format: :json } do
namespace :v1 do
devise_for :users, as: 'api'
end
end
Run Code Online (Sandbox Code Playgroud)
它提供:
Prefix Verb URI Pattern Controller#Action
new_api_user_session GET /api/v1/users/sign_in(.:format) api/v1/sessions#new {:format=>:json}
api_user_session POST /api/v1/users/sign_in(.:format) api/v1/sessions#create {:format=>:json}
destroy_api_user_session DELETE /api/v1/users/sign_out(.:format) api/v1/sessions#destroy {:format=>:json}
cancel_api_user_registration GET /api/v1/users/cancel(.:format) api/v1/registrations#cancel {:format=>:json}
new_api_user_registration GET /api/v1/users/sign_up(.:format) api/v1/registrations#new {:format=>:json}
edit_api_user_registration GET /api/v1/users/edit(.:format) api/v1/registrations#edit {:format=>:json}
api_user_registration PATCH /api/v1/users(.:format) api/v1/registrations#update {:format=>:json}
PUT /api/v1/users(.:format) api/v1/registrations#update {:format=>:json}
DELETE /api/v1/users(.:format) api/v1/registrations#destroy {:format=>:json}
POST /api/v1/users(.:format) api/v1/registrations#create {:format=>:json}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试注册用户时,出现以下错误
ActionController::RoutingError: …Run Code Online (Sandbox Code Playgroud) 嗨,我有一个 rails 6 应用程序,我希望用户在注册时创建一个帐户。
我正在使用设计进行身份验证。
我有两个模型用户(设计)和帐户
class User < ApplicationRecord
has_merit
enum role: [:user, :tech, :admin, :manager]
belongs_to :account
accepts_nested_attributes_for :account
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :admin
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :invitable, :registerable,
:recoverable, :rememberable, :validatable
end
class Account < ApplicationRecord
has_many :users, dependent: :destroy
has_many :clients, dependent: :destroy
end
Run Code Online (Sandbox Code Playgroud)
在注册(设计注册)期间,我希望用户创建一个帐户。
在控制台中它的工作原理:
User.create(email: 'test@email.com', password: "test", password_confirmation: "test, account_attributes: {name: "testaccount"})
Run Code Online (Sandbox Code Playgroud)
按预期创建。 …