小编Thi*_*ent的帖子

Rails 4 + Devise:如何清理多个新参数?

作为Rails的新手,我正在学习如何实现Devise.

为此,我将于2015年3月26日开始关注Ilya Bodrov-Krukowski的Site Point教程.

本教程教授的内容之一是如何使用附加字段创建注册表单:name.

在更新相关视图后,作者解释了以下内容:

如果您正在使用带有strong_params的Rails(默认情况下在Rails 4中启用),则需要再执行一步:该:name属性必须列入白名单.

他建议这段代码这样做:

before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) << :name
  devise_parameter_sanitizer.for(:account_update) << :name
end
Run Code Online (Sandbox Code Playgroud)

这工作得很好......但我的问题是我想在注册表单中添加多个新字段.

实际上,我希望我的注册表格要求用户输入他们的first_name,last_namecompany_name.

我更新了我的视图,我确实在表单中找到了正确的字段.

但是,到目前为止,我还没有能够将这三个属性列入白名单.

我试着这样做:

before_action :configure_permitted_parameters, if: :devise_controller?

    protected

    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) << :first_name, :last_name, :company_name
      devise_parameter_sanitizer.for(:account_update) << :first_name, :last_name, :company_name
    end
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,我得到一个unexpected ","错误.

我也尝试过:

before_action :configure_permitted_parameters, if: :devise_controller?

    protected

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :company_name) }
  devise_parameter_sanitizer.for(:account_update) { |u| …
Run Code Online (Sandbox Code Playgroud)

attributes ruby-on-rails devise strong-parameters ruby-on-rails-4

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

Ruby:根据数值替换数组中的元素

我DID检查这个问题,以及其他类似的问题,它们都没有提供用多个测试值替换数组元素的解决方案.

我有一个Ruby数组:

array = ["america", "europe", "asia", "africa", "france", "usa", "spain", "paris", "los angeles"]
Run Code Online (Sandbox Code Playgroud)

我想转换这个数组,以获得以下结果:

array = ["continent", "continent", "continent", "continent", "country", "country", "country", "city", "city"]
Run Code Online (Sandbox Code Playgroud)

我的第一次尝试是做那样的事情:

array.collect! do |element|
  (element == "america") ? "continent" : element
  (element == "europe") ? "continent" : element
  (element == "asia") ? "continent" : element
  (element == "africa") ? "continent" : element
  (element == "france") ? "country" : element
  (element == "usa") ? "country" : element
  (element == "spain") ? …
Run Code Online (Sandbox Code Playgroud)

ruby arrays

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

获取没有方括号和双引号的数组元素值

我有几个Ruby数组:

array1 = ["a", "b"]
array2 = ["a", "b", "c"]
array3 = ["a", "b", "c", "d"]
array4 = ["a", "b", "c", "d", "e"]
Run Code Online (Sandbox Code Playgroud)

我需要返回以下字符串:

#array1
"a"

#array2
"a and b"

#array3
"a, b and c"

#array4
"a, b, c and d"
Run Code Online (Sandbox Code Playgroud)

永远不应该显示数组的最后一个元素.

我事先并不知道数组包含多少元素或这些元素的值.

为了实现我的需要,我提出了以下方法:

def format_array(array)
  if array.length - 1 == 1
    array[0].to_s
  elsif array.length - 1 == 2
    array[0].to_s + " and " + array[1].to_s
  elsif array.length - 1 > 2
    array.sort.each_with_index do |key, index|
      unless key == "e" …
Run Code Online (Sandbox Code Playgroud)

ruby arrays sorting indexing

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

Rails 4 x postgresql:ActiveRecord :: InvalidForeignKey

在我使用的Rails 4应用程序中,postgresql我遇到了一个我似乎无法理解的错误:

Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.2ms)

ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR:  insert or update on table "comments" violates foreign key constraint "fk_rails_2fd19c0db7"
DETAIL:  Key (commentable_id)=(52) is not present in table "posts".
: INSERT INTO "comments" ("body", "commentable_id", "commentable_type", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"):
  app/controllers/comments_controller.rb:52:in `block in create'
  app/controllers/comments_controller.rb:51:in `create'
Run Code Online (Sandbox Code Playgroud)

这是来自的代码comments_controller.rb:

def create
    @commentable = load_commentable
    @comment = @commentable.comments.build(comment_params)
    @comment.user_id = current_user.id
    respond_to do |format|
      if @comment.save …
Run Code Online (Sandbox Code Playgroud)

postgresql activerecord ruby-on-rails foreign-keys ruby-on-rails-4

0
推荐指数
1
解决办法
646
查看次数