2014-04-10 33 views
5

我试图编辑/使用simple_form更新模型记录,但窗体不会直接更改模型字段。相反,我提供了几个check_box_tag字段,告诉更新哪些字段需要更改。因此,更新没有收到我可以用来更新属性的params [:device]散列。我试图创建这个散列,但当我发出@ device.update_attributes(params [:device])时,我得到了ForbiddenAttributesError。ActiveModel :: ForbiddenAttributesError使用update_attributes创建params哈希我自己

我相信我强大的参数列表是正确的。如果我允许在编辑视图中处理一个模型字段(名称),我会收到预期的params [:device]散列,并且一切正常。如果我禁用该字段,因为我不希望它被更改,那么我需要自己创建哈希,并收到错误消息。当我查看我创建的散列时,它看起来等同于视图所传递的散列。我不明白它为什么失败。

环境是Ruby 2.0.0,Windows 8.1上的Rails 4.1与RubyMine 6.3。

形式为:< ...需要正确的格式,一旦工作...>

<%= simple_form_for @device do |f| %> 
     <legend><%= controller.action_name.capitalize %> Device:</legend> 
     <%= f.input :name, disabled: true %> 
     <%= check_box_tag(:is_admin, 0, @device.admin?) %> 
     <%= label_tag(:is_admin, "Make admin?") %> 
     <%= check_box_tag(:chg_pwd) %> 
     <%= label_tag(:chg_pwd, "Change password?") %> 
     <%= f.button :submit %> 
<% end %> 

】这个params:我收到的时候我送f.input:姓名,禁用:[设备]假的,允许视图生成params [:device]是:

ActionController::Parameters (3 element(s)) 
"{"name"=>"D105", "password"=>"D105Dvgr", "password_confirmation"=>"D105Dvgr"}" 

而且,一切正常。

】这个params [:设备]我创建是:

ActionController::Parameters (3 element(s)) 
"{"name"=>"D106", "password"=>"D106VdAd", "password_confirmation"=>"D106VdAd"}" 

而且,我收到禁止的属性错误,即使我看到了两者之间没有什么区别。

更新为:< ...代码重构的需求,一旦工作...>

class DevicesController < ApplicationController 
    before_filter :authenticate_device! 

... other methods removed here ... 

    def edit 
    @device = Device.find(params[:id]) 
    # my_page = render_to_string controller: 'devices', action: 'edit', layout: "application" 
    end 

    def update 
    authorize! :update, @device, :message => 'Not authorized as an administrator.' 
    @device = Device.find(params[:id]) 
    pwd_msg = "" 
    if params[:chg_pwd] 
     pwd_gen = @device.device + SecureRandom.urlsafe_base64(15).tr('lIO0=_\-', 'sxyzEUM').first(4) 
     params[:device] = {name: @device.name} if params[:device].nil? 
     params[:device][:password] = pwd_gen 
     params[:device][:password_confirmation] = pwd_gen 
     pwd_msg = ", new password is #{pwd_gen}" 
    end 
    if @device.update_attributes(params[:device]) 
     params[:is_admin] ? @device.add_role(:admin) : @device.remove_role(:admin) 
     flash[:notice] = ["Device updated" + pwd_msg] 
     redirect_to devices_path 
    else 
     @device.errors.messages.each do |key, value| 
     flash[:alert] = ["Unable to update device"] 
     @device.errors.messages.each do |key, value| 
      flash[:alert] << key.to_s.capitalize + " " + value[0] 
     end 
     end 
     redirect_to devices_path 
    end 
    end 

    private 

    def device_params 
    params.require(:device).permit(:device, :name, :email, :password, :password_confirmation, :encrypted_password, :salt, :role_ids, :is_admin, :chg_pwd) # TODO minimize when update is working 
    end 

end 

的模型是:

class Device < ActiveRecord::Base 

    rolify 
    devise :database_authenticatable, :rememberable, :trackable, :validatable 

    validates :device, 
      presence: true, 
      length: {minimum: 4 }, 
      uniqueness: {case_sensitive: false } 
    validates :name, 
      presence: true 

    def remember_me 
    true unless self.admin? 
    end 

    def admin 
    self.add_role :admin 
    end 

    def not_admin 
    self.remove_role :admin 
    end 

    def admin? 
    self.has_role? :admin 
    end 
    def device? 
    self.has_role? :device 
    end 
    def vip? 
    self.has_role? :vip 
    end 

    def login=(login) 
    @login = login 
    end 
    def login 
    @login || self.device || self.email 
    end 

    def self.find_first_by_auth_conditions(warden_conditions) 
    conditions = warden_conditions.dup 
    if login = conditions.delete(:login) # Note one equal sign. Strange but true. 
     where(conditions).where(["lower(device) = :value OR lower(email) = :value", { :value => login.downcase }]).first 
    else 
     where(conditions).first 
    end 
    end 

end 

新信息:我忘了提供信息我在ApplicationController中。这从安东·特拉普修复处理强大的参数宝石是尚未完全轨道4兼容:

before_filter do 
    resource = controller_name.singularize.to_sym 
    method = "#{resource}_params" 
    params[resource] &&= send(method) if respond_to?(method, true) 
    end 

我发现,使用的建议的解决方案:

@device.update_attributes(device_params) 

不一样,如果一个模型字段已更新。结果是“param not found:device”。如果没有模型字段被更新,它可以工作。所以,整个问题都引发了真正错误的问题。

回答

1

此修复程序是作为attr_accessor将字段添加到模型,但不包括数据库,以便在表单内正确使用它。

attr_accessor :is_admin, :chg_pwd 

然后修改视图:

<%= simple_form_for @device do |f| %> 
    <legend><%= controller.action_name.capitalize %> Device:</legend> 
    <%= f.input :name, disabled: true %> 
    <%= f.input :is_admin, as: :boolean, checked_value: true, unchecked_value: false %> 
    <%= f.input :chg_pwd, as: :boolean, checked_value: true, unchecked_value: false %> 
    <%= f.button :submit %> 
<% end %> 

然后,由于从安东特拉普应用控制器代码:

before_filter do 
    resource = controller_name.singularize.to_sym 
    method = "#{resource}_params" 
    params[resource] &&= send(method) if respond_to?(method, true) 
    end 

我能更新设备控制器领域如下:

@device.update_attributes(params[:device]) 
3

DevicesController#update行动,改变

@device.update_attributes(params[:device]) 

@device.update_attributes(device_params) 

当您使用Rails 4.1,你需要加入白名单,你想插入/更新数据库中的属性。当你直接通过属性update_attributes方法没有允许他们收到ActiveModel::ForbiddenAttributesError

UPDATE

要解决param not found: device

def device_params 
    if params[:device] 
     params.require(:device).permit(:device, :name, :email, :password, :password_confirmation, :encrypted_password, :salt, :role_ids, :is_admin, :chg_pwd) # TODO minimize when update is working 
    end 
    end 
+0

好的,这个变化使得整体感觉和工作。不过,我仍然怀疑,为什么它在涉及模型领域时的方式发挥作用?你知道吗?谢谢... –

+0

'为什么当涉及模型领域的时候它的工作方式?'意味着什么?哪个模型字段?你的意思是这个代码在任何情况下工作吗? –

+0

在编辑视图中,如果我使用“f.input:name,disabled:false”,则将params [:device]发送到使用params [:davice] [:name]设置的更新操作。在这种情况下,更新操作就像我一样。相反,如果我使用“f.input:name,disabled:true”,以便名称字段不能更改,而params [:device]为nil。为了update_attributes,我如图所示构建了params [:device]。两个哈希都是等价的。但是,即使为我编写的编辑视图使用@ device.update_attributes(params [:device])工作,我在构建的散列上收到了ForbiddenAttributeError。 –

相关问题