随着最近升级到Rails 4,使用类似下面的代码更新属性不起作用,我收到一个ActiveModel::ForbiddenAttributes错误:
@user.update_attributes(params[:user], :as => :admin)
Run Code Online (Sandbox Code Playgroud)
用户在模型中具有以下attr_accessible行:
attr_accessible :role_ids, :as =>admin
# or any attribute other than :role_ids contained within :user
Run Code Online (Sandbox Code Playgroud)
你如何在Rails 4中完成同样的任务?
ruby-on-rails attr-accessible activemodel strong-parameters ruby-on-rails-4
使用以下代码在后台会发生什么?
class User < ActiveRecord::Base
attr_accessor :name
attr_accessible :name
end
Run Code Online (Sandbox Code Playgroud)
提示:实例化类时,是否会持久化到数据库?为什么或者为什么不?
在阅读了Rails 3.1 API中的attr_accessible后,我发现其中有一个as :admin选项.我想知道两件事.
如果用户有一个admin标志,我的控制器如何告诉我的模型用户是管理员.
如果用户是所有者,我可以:as => owner在我的模型中指定,并且我的控制器如何通知我的模型他们是项目的所有者.
ruby-on-rails mass-assignment attr-accessible ruby-on-rails-3.1
当我使用它attr_accessible来指定我的模型I中的哪些字段将公开时,脚本/控制台也是如此?我的意思是我没有指定的东西attr_accessible也不能通过控制台访问?
我刚才已经做了一点阅读attr_accessor,attr_accessible和强大的参数在几个不同的位置:
attr_accessor和attr_accessible之间的区别
如何在Rails 4中使用attr_accessible?
http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html
而我正在考虑大规模任务:
http://code.tutsplus.com/tutorials/mass-assignment-rails-and-you--net-31695
我无法理解attr_accessible与强参数之间的区别.我对上述主题的理解并不是100%自信所以我可能会错过一些简单但我知道他们做类似的工作.
但是,attr_accessible强参数有什么区别?它们只是同一个东西的不同名称吗?我们为什么要从一个移到另一个?
任何信息表示赞赏.
如果尝试批量分配attr_accessible不允许的属性,是否有办法让rails引发错误?
这在开发中很方便,以提醒我为什么我的闪亮的新模型不起作用,并且还可以登录生产以检测恶意活动.
我正在使用rails 2.3.8,但可能很快就会迁移到3.
我有一个Rails应用程序,其中包含一个包含admin属性的用户模型.它已被锁定使用attr_accessible.我的模型看起来像这样:
attr_accessible :name, :email, :other_email, :plant_id, :password, :password_confirmation
attr_accessible :name, :email, :other_email, :plant_id, :password, :password_confirmation, :admin, :as => :admin
Run Code Online (Sandbox Code Playgroud)
以下是我的用户控制器中的更新方法:
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user], :as => current_user_role.to_sym)
flash[:notice] = "Profile updated"
redirect_to edit_user_url(@user)
else
render 'edit'
end
end
Run Code Online (Sandbox Code Playgroud)
我的应用程序控制器中有一个帮助方法,它将角色作为字符串传回:
def current_user_role
@current_user_role ||= current_user.admin? ? "admin" : "default"
end
helper_method :current_user_role
Run Code Online (Sandbox Code Playgroud)
我也开始config.active_record.whitelist_attributes = true了config/application.rb.
我已经验证该current_user_role方法是根据当前用户的管理状态返回正确的值.Rails没有抛出质量分配错误.但是当我以管理员身份登录时尝试更新用户的管理状态时,Rails会执行更新并默默忽略该admin属性.在Rails控制台中拉出用户的记录表明该记录尚未被修改.
我觉得有一个特定于Ruby或Rails的问题,我不知道.我无法找到有关使角色动态化的任何信息.我能找到的最好的就是这个.
我想创建大量的属性,如果用这样的方法调用构造,可以轻松完成,
attr_accessor :attr_list
def attr_list
[:x1, :y1, :x2, :y2]
end
Run Code Online (Sandbox Code Playgroud)
这不起作用.有没有其他方法来实现这一目标?
任何帮助将不胜感激.
当我想测试RSpec 是否无法访问属性时我就是这样做的
class Foo
attr_accesible :something_else
end
describe Foo do
it('author should not be accessible') {lambda{described_class.new(:author=>true)}.should raise_error ActiveModel::MassAssignmentSecurity::Error}
it('something_else should be accessible'){lambda{described_class.new(:something_else=>true)}.should_not raise_error ActiveModel::MassAssignmentSecurity::Error}
end
Run Code Online (Sandbox Code Playgroud)
这样做有更好的方法吗?
...谢谢
deprecated_mass_assignment_security.rb:17:in `attr_accessible': `attr_accessible` is extracted out of Rails into a gem. Please use new recommended protection model for params(strong_parameters) or add `protected_attributes` to your Gemfile to use old one. (RuntimeError)
Run Code Online (Sandbox Code Playgroud)
我尝试了消息所说的内容,并添加gem 'strong_parameters'到我的内容中Gemfile.
但是当我这样做时,rails s我得到了上面的错误.
我试过了:
config.active_record.whitelist_attributes = true
Run Code Online (Sandbox Code Playgroud)
在confgi/application.rb,也与false,但实际上我不明白这个选项.
ruby-on-rails attr-accessible ruby-on-rails-3 strong-parameters ruby-on-rails-4