小编Rub*_*tic的帖子

Devise + CanCan只是阻止其他用户编辑对象

你如何阻止其他用户编辑一个对象,比如一个不属于他们自己的个人资料对象呢?

大多数在线示例都是具有多个用户角色的复合体,我无法使其工作,但必须简单:

  def initialize(user)

      can :update, Profile do |profile|
        profile.try(:user) == current_user
      end

  end
Run Code Online (Sandbox Code Playgroud)

在我的ProfilesController#edit中

authorize! :update, @profile
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails devise cancan ruby-on-rails-3

4
推荐指数
1
解决办法
4331
查看次数

在rails 3.1的资产管道之外包含JS文件?

我想包含一些不在资产管道中的js文件,我尝试将它们包含在rails.root和这些文件的完整路径中,但这不是许可的工作原因。

我知道我可以在资产管道JS application.js中手动包括每个单独的JS文件,但这会做很多工作,因为我的JS在开发过程中发生了很多变化,因此,如果有一种方法可以包括js文件,那就太好了外部资产管道。

有人对我该如何处理有一些建议吗?

include ruby-on-rails-3.1 asset-pipeline

4
推荐指数
1
解决办法
3454
查看次数

在rails 3.2 + devise 2.0之后,设计列confirmed_at不再更新

升级到rails 3.2并设计2.0并单击通过电子邮件发送的确认链接或从开发中的控制台复制后,confirmed_at列不会更新.结果就是你无法登录.

解决这个问题的方法是什么,因为日志中没有任何内容,可访问的属性包含了confirmed_at,因此该列应该是可更新的.

ruby-on-rails devise ruby-on-rails-3 ruby-on-rails-3.1

4
推荐指数
1
解决办法
1372
查看次数

禁用rails 3.2.x中的查询日志记录进行开发?

我遵循了关于禁用活动记录的SQL查询日志记录的所有主题.谁知道如何在rails 3.2.x中正确禁用它?

sql logging activerecord ruby-on-rails-3 ruby-on-rails-3.2

4
推荐指数
1
解决办法
1324
查看次数

检测是否在轨道上提交了带有红宝石的表单?

有没有办法检测表单是否已提交?我试图基于自定义验证设置类,如下例所示,这可能吗?

  .control-group{ :class => ("error" if form_is_submitted ) }
Run Code Online (Sandbox Code Playgroud)

现在尝试:

  .control-group{ :class => ("error" if params[:user][:profile_attributes][:gender] == nil) }
Run Code Online (Sandbox Code Playgroud)

如果表单未提交,则会失败,因为params是nill并抛出错误

forms submit ruby-on-rails-3

4
推荐指数
1
解决办法
1万
查看次数

未确认时设计重定向到自定义网址而不是闪存通知

使用自定义故障类实现设计用于检测用户是否未确认的触发器是什么?warden_message不起作用.有谁知道?

class CustomFailure < Devise::FailureApp

  def redirect_url

    if warden_options[:scope] == :user
      new_user_registration_path
    else
      new_user_registration_path
    end
  end

  def respond

    if http_auth?
      http_auth
    else
      store_location!
      flash[:alert] = i18n_message unless flash[:notice]

      if warden_message == :unconfirmed
        redirect_to "/confirm"
      else
        redirect_to sign_in_path
      end
    end

  end

end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails devise ruby-on-rails-3

4
推荐指数
1
解决办法
832
查看次数

设计可重新配置,使用单独的邮件模板重新确认电子邮件?

Devise支持可重新配置选项,当用户更改电子邮件时,他们必须确认新电子邮件.

但是,使用标准确认邮件模板.我想为必须重新确认的电子邮件使用单独的邮件模板.

devise的registrations_controller.rb:

  def update

    self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
    prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)

    if resource.update_with_password(resource_params)
      if is_navigational_format?
        flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
            :update_needs_confirmation : :updated
        set_flash_message :notice, flash_key
      end
      sign_in resource_name, resource, :bypass => true
      respond_with resource, :location => after_update_path_for(resource)
    else
      clean_up_passwords resource
      respond_with resource
    end
  end
Run Code Online (Sandbox Code Playgroud)

任何人都知道在哪里挂钩Devise让它使用单独的电子邮件文本来重新确认电子邮件,而不是使用默认的确认文本?

ruby-on-rails actionmailer confirmation devise ruby-on-rails-3

4
推荐指数
1
解决办法
1344
查看次数

redirect_to在if语句中不能按预期工作

试图找出为什么我的nil检查在调用没有param的方法或者没有产生记录的param id时失败.

@game       = Game.where(:id => params[:id]).first

if @game.nil?
  redirect_to root_path
end
Run Code Online (Sandbox Code Playgroud)

在控制台中这很好用.

>> pp @game.nil?
=> true
Run Code Online (Sandbox Code Playgroud)

在应用程序中,这失败了(它永远不会重定向!),为什么?

编辑1:

在控制台中(param id为nil或不存在记录值):这有效:

unless Game.exists?(params[:id])
  raise('ok')
end
Run Code Online (Sandbox Code Playgroud)

但不是在现实生活中的应用程序:(我几乎用各种方式检查记录是否存在或有效,代码只是通过此检查并继续原样

编辑2:

看看其他一些代码,我注意到我使用了一个返回语句,IT似乎用它解决了这个问题.

作品:

unless Game.exists?(params[:id])
  redirect_to root_path
  return
end
Run Code Online (Sandbox Code Playgroud)

失败:

unless Game.exists?(params[:id])
  redirect_to root_path
end
Run Code Online (Sandbox Code Playgroud)

不太确定为什么它需要在redirect_to explicit之后返回

redirect ruby-on-rails ruby-on-rails-4

4
推荐指数
1
解决办法
9389
查看次数

Rspec路由规范失败,param id被反转?

我的rspec路由规范给出了不清楚的错误.在预期的params v/s中,id param处于反向位置的真实参数.为什么以及如何解决?

require "spec_helper"

describe GameController do
  describe "routing" do

    game = FactoryGirl.create(:game)

    it "routes to #show" do
      get("/game/1").should route_to("game#show", :id => 1)
    end

  end
end
Run Code Online (Sandbox Code Playgroud)

抛出错误:

  1) gameController routing routes to #show
     Failure/Error: get("/game/1").should route_to("game#show", :id => 1)
       The recognized options <{"action"=>"show", "controller"=>"game", "id"=>"1"}> did not match <{"id"=>1, "controller"=>"game", "action"=>"show"}>, difference:.
       <{"id"=>1, "controller"=>"game", "action"=>"show"}> expected but was
       <{"action"=>"show", "controller"=>"game", "id"=>"1"}>.
     # ./spec/routing/game_routing_spec.rb:11:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

specs rspec ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

4
推荐指数
1
解决办法
1076
查看次数

在所有载波上传的图像中找不到生产中的载波404

我使用载波将资产上传到我的应用程序,它们存储在/public/uploads/photos/file/user_id/user_id.jpg中以简化操作.

只有在制作中我才能获得所有这些图像的404.我尝试chmod 777上传-R*作为最后的手段,但这没有帮助.

我是否需要设置一些我在这里缺少的东西才能使它工作,因为这些资产不在资产管道之内?

upload ruby-on-rails http-status-code-404 carrierwave

3
推荐指数
1
解决办法
1597
查看次数