标签: strong-parameters

Rails 4强参数,无需参数

我正在使用Rails 4,我不知道在没有必要参数的情况下使用强参数的最佳方法是什么.那就是我做的:

def create
device = Device.new(device_params)
.................
end

private

def device_params
  if params[:device]
    params.require(:device).permit(:notification_token)
  else
    {}
  end
end
Run Code Online (Sandbox Code Playgroud)

我的设备型号不验证任何内容.我知道我也可以这样做:

device = Device.new
device.notification_token = params[:device][:notification_token] if params[:device] && params[:device][:notification_token]
Run Code Online (Sandbox Code Playgroud)

是否有任何惯例或正确的方法来做到这一点?

ruby ruby-on-rails strong-parameters ruby-on-rails-4

19
推荐指数
1
解决办法
9089
查看次数

CanCan load_and_authorize_resource触发禁止的属性

我有一个使用强参数的标准RESTful控制器.

class UsersController < ApplicationController
  respond_to :html, :js

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def edit
    @user = User.find(params[:id])
  end

  def create
    @user = User.new(safe_params)

    if @user.save
      redirect_to @user, notice: t('users.controller.create.success')
    else
      render :new
    end
  end

  def update
    @user = User.find(params[:id])

    if @user.update_attributes(safe_params)
      redirect_to @user, notice: t('users.controller.update.success')
    else
      render :edit
    end
  end

  def destroy
    @user = User.find(params[:id])

    if current_user != @user
      @user.destroy
    else
      flash[:error] = t('users.controller.destroy.prevent_self_destroy')
    end
    redirect_to …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails cancan rspec2 strong-parameters

18
推荐指数
2
解决办法
5607
查看次数

我应该如何将rails和simple_form用于嵌套资源?

我正在尝试同时使用另一个嵌套资源创建一个资源.我正在使用Rails4和simple_form 3.0.0rc.这是我的代码.

楷模:

class User < ActiveRecord::Base
  has_one :profile
  accepts_nested_attributes_for :profile
end

class Profile < ActiveRecord::Base
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

控制器:

class UsersController < ApplicationController
  def new
    @user = User.new
    @user.build_profile
  end

  def create
    user = User.new user_params
    user.save
    redirect_to root_url
#    @par =params
  end

  private
    def user_params
      params.require(:user).permit(:email, profile_attributes: [:name])
    end
end
Run Code Online (Sandbox Code Playgroud)

查看(新用户的表单)

<%= simple_form_for @user do |f| %>
  <%= f.input :email %>
  <%= simple_fields_for :profile do |p| %>
    <%= p.input :name %>
  <% end %>
  <%= f.submit %>
<% …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-attributes fields-for simple-form strong-parameters

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

Rails中的强参数3.2.8

该视频表明,可以保护通过控制器输入的输入,但仍然可以通过型号和规格进行质量分配.但是,在3.2.8中使用strong_parameters时,我没有将此文档记录为功能.

我知道我需要混入ActiveModel::ForbiddenAttributesProtection我的模型并config.active_record.whitelist_attributes = false进入config/application.rb.我也attr_accessible从模型中取出了所有的电话.

无论有没有mixin,我都会遇到质量分配错误.

ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: home_phone, cell_phone

我错过了什么吗?

ruby-on-rails strong-parameters

17
推荐指数
1
解决办法
8474
查看次数

Rails 4强参数 - 处理缺失的模型参数哈希

模特:帖子和用户

帖子belongs_to:用户
用户has_many:帖子

简单.

假设存在少数用户,我们访问Post的编辑页面.

<%= form_for @post do |f| %>
Run Code Online (Sandbox Code Playgroud)

...

<% User.all.each do |user| %>
  <div><%= f.radio_button "user_id", user.id %></div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

...

Post's Controller利用Rails 4强大的参数.

params.require(:post).permit(:user_id)
Run Code Online (Sandbox Code Playgroud)

假设编辑帖子表单只有单选按钮作为字段.

问题:抛出ActionController :: ParameterMissing异常.param not found:post

永远不会创建Post params哈希的原因,导致抛出上述异常.与空文本字段不同,空单选按钮不会触发要创建的模型的参数哈希.

如果Post模型要求user_id有效,该怎么办?当然,人们会想要再次渲染视图,原因是无法保存帖子.

问题:在坚持Rails约定的同时处理这种情况的优雅方法是什么?

更新:

关于这个进一步集思广益,我敢肯定可能有很多其他情况会产生这个问题; 它不一定对应于单选按钮.

strong-parameters ruby-on-rails-4

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

为什么切割params散列会对批量分配造成安全问题?

通过批量分配来防止安全风险的官方方法是使用attr_accessible.但是,一些程序员认为这不是模型的工作(或者至少不仅仅是模型).在控制器中执行此操作的最简单方法是切换params哈希:

@user = User.update_attributes(params[:user].slice(:name))
Run Code Online (Sandbox Code Playgroud)

但是文档说明:

请注意,使用Hash#except或Hash#slice代替attr_accessible来清理属性将无法提供足够的保护.

这是为什么?为什么白名单切片的params不能提供足够的保护?

更新: Rails 4.0将发布强参数,一个精确的参数切片,所以我猜整个切片事情毕竟不是那么糟糕.

ruby security ruby-on-rails mass-assignment strong-parameters

15
推荐指数
1
解决办法
5719
查看次数

在rails控制台中创建实例时,Rails 4强参数失败

可能在这里做一些愚蠢的事情,但这里是我的基本cookie切割器类:

class League < ActiveRecord::Base

    private
      def league_params
        params.require(:full_name).permit!
      end

end
Run Code Online (Sandbox Code Playgroud)

在创建联盟的新实例时:

2.0.0-p0 :001 > l = League.new(full_name: 'foo', short_name: 'bar')
WARNING: Can't mass-assign protected attributes for League: full_name, short_name
Run Code Online (Sandbox Code Playgroud)

我到底错在了什么?这是一个Rails 4.0.0.beta1 build + Ruby 2.0

**更新**

我现在意识到强大的参数现在在Controller中实施,而不是在模型中.最初的问题仍然存在.如果在控制器级别允许它们,如果我在Rails控制台中创建实例,如何正确地将属性列入白名单?我是否还需要attr_accessible在这种情况下也使用,从而完全复制哪些强参数试图"修复"?

ruby ruby-on-rails strong-parameters ruby-on-rails-4

15
推荐指数
2
解决办法
9583
查看次数

rails strong参数不接受哈希数组

我有一个组控制器,它接受哈希数组作为创建操作的POST请求的参数

  def create
    response = Group.create(current_user_id, group_params)
    render json: response
  end

  def group_params
    params.require(:group).permit(:group_name, :group_title, group_members: [])
  end
Run Code Online (Sandbox Code Playgroud)

但它不通过强参数接受group_members参数

以下是我的应用程序中带有params的传入请求

Started POST "/groups" for 127.0.0.1 at 2014-08-04 08:25:37 +0545
Processing by GroupsController#create as JSON
  Parameters: {"group"=>{"group_name"=>"Fourth group", "group_title"=>"fourth tester", "group_members"=>[{"id"=>"0833be3c-17db-11e4-904b-3f662703cb5b", "darknet_accountname"=>"@ckgagan", "access_level"=>"Write"}]}}
Unpermitted parameters: group_members
Completed 200 OK in 10ms (Views: 0.2ms)
Run Code Online (Sandbox Code Playgroud)

我看过很多帖子,说在允许内部添加group_members:[]会有效但在我的情况下不起作用.

arrays parameters strong-parameters

15
推荐指数
1
解决办法
7244
查看次数

Rails 4.0 with Devise.嵌套属性Unpermited参数

我正在使用Devise和Rails 4开发一个Web应用程序.我有一个用户模型,我已经扩展了2个额外的表单字段,这样当用户注册时,他也可以提交他的名字/姓氏.(基于http://blog.12spokes.com/web-design-development/adding-custom-fields-to-your-devise-user-model-in-rails-4/).我现在想要添加一个机构模型.此模型has_many:用户和用户belongs_to:institution.我希望能够在注册用户的同一表格上注册该机构的名称.我知道我需要在我的Institution模型中使用nested_attribute ,因为这是父类,我将稍微展示一下.当我尝试注册用户时,我进入控制台:Unpermited参数:机构.

我的提示是我无法根据我的子类(User)更新我的父类(Institution).可能有解决方案吗?或者有没有人经历类似的事情?

class Institutions < ActiveRecord::Base
    has_many :users, 
    accepts_nested_attributes_for :users
end

class User < ActiveRecord::Base
     devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
     belongs_to :institution
end
Run Code Online (Sandbox Code Playgroud)

registrations/new.html.erb这里我有嵌套表格

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|     %>
<%= devise_error_messages! %>
.
. 
    <%= f.fields_for :institutions do |i| %>
        <p><%= i.label :name %><br />
        <%= i.text_field :institutions_attr %></p>
    <% end %>
Run Code Online (Sandbox Code Playgroud)

基于我之前链接的教程,我创建了一个新的 …

ruby-on-rails nested-forms nested-attributes devise strong-parameters

14
推荐指数
3
解决办法
2万
查看次数

轨道4中未允许的参数

我读过collection_check_boxes但我不明白如何设置检查值.我有以下型号:

class Objective < ActiveRecord::Base

  has_many :indicators
  has_many :objective_children, class_name: "Objective", foreign_key: "parent_id"

  def objective_ids
    objective_children.collect{|o| o.id}
  end

  def objective_ids= objectives_ids
    objectives_ids.each do |id|
      objective_children << Objective.find(id)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

编辑视图:

<%= form_for(@objective) do |f| %>
  <%= f.collection_check_boxes :objective_ids, Objective.all, :id, :name %>
  <%= f.submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

html复选框没问题,但我不知道如何设置值objective.我被尝试定义objective_ids= objectives_ids但没有任何反应.

在控制器中:

class ObjectivesController < ApplicationController
    def objective_params
      params.require(:objective).permit(:name, :code, :description, :objective_ids)
    end
end
Run Code Online (Sandbox Code Playgroud)

编辑 日志文件说Unpermitted parameters: perspective_id, objective_ids

checkbox ruby-on-rails view-helpers strong-parameters ruby-on-rails-4

14
推荐指数
1
解决办法
4916
查看次数