如果我有一个Devise模型User,其中只允许那些具有role:admin的用户查看某个url,我该如何编写RSpec集成测试来检查该url的状态是否返回200?
def login(user)
post user_session_path, :email => user.email, :password => 'password'
end
Run Code Online (Sandbox Code Playgroud)
在这个问题的答案中,这是伪建议:在请求规范中对验证进行验证,但我不能为我的生活让它与设计一起工作.当自动检查具有正确权限的Ability时,CanCan正在接收nil用户.
在集成规范中无法访问控制器,因此我无法存根current_user,但我想做类似的事情.
describe "GET /users" do
it "should be able to get" do
clear_users_and_add_admin #does what it says...
login(admin)
get users_path
response.status.should be(200)
end
end
Run Code Online (Sandbox Code Playgroud)
注意!!!:自提出问题以来,这一切都发生了变化.目前最好的方法是:http://github.com/plataformatec/devise/wiki/How-To : -Test-with-Capybara
嘿,伙计们.所以我想到了这个酷炫的想法,如果你登录然后你得到某种仪表板,否则你得到一个信息/登录/注册页面..所以我该怎么做..
我主要想在Routes中这样做=不是这样的
def index
if current_user.present?
render :action => 'logged_in'
else
render :action => 'logged_out'
end
end
Run Code Online (Sandbox Code Playgroud)
提前致谢!
/ Oluf Nielsen
我想扩展我的设计安装的注册表单.我创建了一个Profile模型,现在问自己,如何将表单的特定数据添加到此模型中.UserController设计在哪里?
提前致谢!
如何让f.error_messages在这里工作,或者我应该使用闪光灯?
如果是这样,在sessions_controller中应该覆盖什么?
<h2>Create an account</h2>
<% form_for resource_name, resource, :url => registration_path(resource_name) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :email %><br />
<%= f.text_field :email, :class => :big %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password, :class => :big %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, :class => :big %>
</p>
<p><%= f.submit "Create", :class => :submit %></p>
<% end %>
Run Code Online (Sandbox Code Playgroud)
PS.创建帐户的f.error_messages完全正常.
我有我的rails应用程序,我遇到了一个设计的主要问题.我有一个控制器:
class Users::SessionsController < Devise::SessionsController
prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
include Devise::Controllers::InternalHelpers
def new
clean_up_passwords(build_resource)
respond_to do |format|
format.html { render :layout => "sessions" }
format.mobile
end
end
# POST /resource/sign_in
def create
resource = User.find_by_email(params[:user][:email])
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message :notice, :signed_in
sign_in_and_redirect(resource_name, resource)
end
end
Run Code Online (Sandbox Code Playgroud)
问题是它永远不会记录用户,它总是停在这一行
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
Run Code Online (Sandbox Code Playgroud)
我甚至在实际的宝石文件中放了大量的记录器,看看我是否可以看到任何东西但没有任何东西,我真的不知道如何解决这个问题.如果我注释掉这一行,那么用户就会登录,但如果电子邮件不在数据库中并且适用于任何密码(这绝对不是正确的解决方案),则会失败
我该如何解决?
UPDATE
这有效,但似乎非常hackish
# POST /resource/sign_in
def create
resource = User.find_by_email(params[:user][:email])
redirect_to(new_user_session_path, :notice => 'Invalid Email …Run Code Online (Sandbox Code Playgroud) 我有一个全新的Rails 4.1.1应用程序,我正在自定义Devise电子邮件.我想让它们显示在新的Rails电子邮件预览功能上,所以我做了以下事情:
1)在我的config/development.rb文件中添加了以下代码段:
config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews"
Run Code Online (Sandbox Code Playgroud)
2)创建我的自定义设计的电子邮件UserMailer中app/mailers/user_mailer.rb:
class UserMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
layout "notifications_mailer"
end
Run Code Online (Sandbox Code Playgroud)
3)更改config/initializers/devise.rb为包含以下代码段:
config.mailer = 'UserMailer'
Run Code Online (Sandbox Code Playgroud)
4)添加了类UserMailerPreview以lib/mailer_previews用下面的内容:
class UserMailerPreview < ActionMailer::Preview
def confirmation_instructions
UserMailer.confirmation_instructions(User.first, {})
end
def reset_password_instructions
UserMailer.reset_password_instructions(User.first, {})
end
def unlock_instructions
UserMailer.unlock_instructions(User.first, {})
end
end
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.看起来我已经做好了一切.但后来我尝试confirmation_instructions在/ rails/mailers/user_mailer/confirmation_instructions路线上看到电子邮件的预览,我收到以下错误:
undefined method `confirmation_url' for #<#<Class:0x007fa02ab808e0>:0x007fa030fb7e80>
Run Code Online (Sandbox Code Playgroud)
我的confirmation_url.html.erb模板的代码如下所示: …
我想手动创建新的Users,而不强迫他们验证他们的电子邮件地址.
这个想法是允许现有用户自动添加他们的朋友,而无需他们的注册.对我正在努力解决的商业案例有意义.
如何用Devise实现这一目标?
由于设计我有很多用户,我想禁止一些问题制定者.Devise是否内置了这种支持?
谢谢
首先,我与谷歌和雅虎进行了密切的搜索,我发现了一些关于像我这样的主题的回复,但它们并没有真正涵盖我需要知道的内容.
我的应用程序中有几个用户模型,现在它是客户,设计师,零售商,似乎还有更多未来.它们都有不同的数据存储在他们的表格中,以及他们允许或不允许的网站上的几个区域.所以我想去设计+ CanCan的方式,并尝试我的运气与多态关联,所以我得到以下模型设置:
class User < AR
belongs_to :loginable, :polymorphic => true
end
class Customer < AR
has_one :user, :as => :loginable
end
class Designer < AR
has_one :user, :as => :loginable
end
class Retailer < AR
has_one :user, :as => :loginable
end
Run Code Online (Sandbox Code Playgroud)
对于注册,我已经为每个不同的用户类型定制了视图,我的路由设置如下:
devise_for :customers, :class_name => 'User'
devise_for :designers, :class_name => 'User'
devise_for :retailers, :class_name => 'User'
Run Code Online (Sandbox Code Playgroud)
现在注册控制器是标准的(这是"设计/注册"),但我想,因为我有不同的数据存储在不同的模型中,我也必须自定义这种行为!?
但与此设置我喜欢助手customer_signed_in?和designer_signed_in?,但我真正需要的是一个一般的助手一样user_signed_in?的可以访问的所有用户在网站上的区域,无论哪个用户类型.
我也喜欢路线助手new_user_session_path而不是几个new_*type*_session_path等等.事实上,我需要做的就是注册过程......
所以我想知道如果这是解决这个问题的方法??? 或者是否有更好/更容易/更少必须定制的解决方案?
罗伯特,提前谢谢
我的设计用户是"database_authenticatable"和"token_authenticatable".我尝试从控制台删除该用户的数据库中的"authentication_token"字段,但他们似乎仍然可以使用他们现有的身份验证令牌.删除用户完全有效,但我不想走那么远.
编辑:为清楚起见.我想使用rails 控制台注销用户.即运行rails console然后一些命令.
devise ×10
ruby-on-rails ×10
ruby ×4
cancan ×1
logout ×1
registration ×1
routes ×1
rspec ×1
warden ×1