小编Bra*_*axo的帖子

Rails:你可以将参数传递给after_create方法吗?

在我的应用程序中,只有具有管理员角色的用户才可以创建新用户.在新用户表单中,我为可能分配给新用户的每个可用角色都有一个选择框.

我希望使用after_create回调方法将角色分配给用户.如何在after_create方法中访问选择框的选定值?

  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        flash[:notice] = 'User creation successful.'
        format.html { redirect_to @user }
      else
        format.html { render :action => 'new' }
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

在用户模型中,我有:

  after_create :assign_roles

  def assign_roles
    self.has_role! 'owner', self
    # self.has_role! params[:role]
  end
Run Code Online (Sandbox Code Playgroud)

我收到错误,因为模型不知道是什么角色.

activerecord ruby-on-rails

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

jQuery:是否可以将DOM元素分配给变量以供以后使用?

我正在开发一个使用jQuery的项目,我对Mootools更熟悉.

我先从我的代码开始.

var customNamespace = {
    status: 'closed',
    popup: $('#popup'),

    showPopup: function() {
        // ...  
    }
}

$(document).ready(function(){
    console.log($('#popup'));
    console.log(customNamespace.popup);
    console.log($(customNamespace.popup));

    $('#popup').fadeIn('slow');
    (customNamespace.popup).fadeIn('slow');
    $(customNamespace.popup).fadeIn('slow');
});
Run Code Online (Sandbox Code Playgroud)

我的目标是每次我想用#popup div做一些事情时都没有jQuery遍历DOM,所以我想把它保存到一个变量中以便在整个脚本中使用它.

当页面加载时,控制台会按照我的预期打印出对象3次,所以我认为对于每种方法,fadeIn都可以正常工作.但事实并非如此

$('#popup').fadeIn('slow');
Run Code Online (Sandbox Code Playgroud)

实际上在div中淡出.

即使我删除了我的命名空间哈希,只是将对象保存到全局变量,然后执行

var globalVariable = $('#popup');
.
.
.
globalVariable.fadeIn('slow');
Run Code Online (Sandbox Code Playgroud)

也没有像我想的那样工作.jQuery可以做我想做的事情吗?

javascript jquery

6
推荐指数
2
解决办法
7966
查看次数

标签 统计

activerecord ×1

javascript ×1

jquery ×1

ruby-on-rails ×1