我有一个功能正常的Rails 3应用程序使用has_many:通过关联,当我将其重新设置为Rails 4应用程序时,让我从Rails 4版本中的相关模型中保存ID.
这两个版本的三个相关模型是相同的.
Categorization.rb
class Categorization < ActiveRecord::Base
belongs_to :question
belongs_to :category
end
Run Code Online (Sandbox Code Playgroud)
Question.rb
has_many :categorizations
has_many :categories, through: :categorizations
Run Code Online (Sandbox Code Playgroud)
Category.rb
has_many :categorizations
has_many :questions, through: :categorizations
Run Code Online (Sandbox Code Playgroud)
在这两个应用程序中,类别ID都会像这样传递到create动作中
"question"=>{"question_content"=>"How do you spell car?", "question_details"=>"blah ", "category_ids"=>["", "2"],
Run Code Online (Sandbox Code Playgroud)
在Rails 3应用程序中,当我创建一个新问题时,它会插入问题表,然后插入到分类表中
SQL (82.1ms) INSERT INTO "questions" ("accepted_answer_id", "city", "created_at", "details", "province", "province_id", "question", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["accepted_answer_id", nil], ["city", "dd"], ["created_at", Tue, 14 May 2013 17:10:25 UTC +00:00], ["details", "greyound?"], ["province", …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的问题.但到目前为止还没有找到解决方案.
所以这是我发送给服务器的JSON字符串:
{
"name" : "abc",
"groundtruth" : {
"type" : "Point",
"coordinates" : [ 2.4, 6 ]
}
}
Run Code Online (Sandbox Code Playgroud)
使用新的许可方法,我得到:
params.require(:measurement).permit(:name, :groundtruth)
Run Code Online (Sandbox Code Playgroud)
这不会引发任何错误,但创建的数据库条目包含null而不是groundtruth值.
如果我只是设置:
params.require(:measurement).permit!
Run Code Online (Sandbox Code Playgroud)
所有东西都按预期保存,但当然,这会破坏强参数提供的安全性.
我找到了解决方案,如何允许数组,但没有找到使用嵌套对象的单个示例.这必须是某种可能的,因为它应该是一个非常常见的用例.那么它是怎样工作的?
ruby-on-rails nested-attributes strong-parameters ruby-on-rails-4
我正在构建一个带有Rails 4强参数的Web应用程序.
在构建管理员后台控制器时,我想知道允许所有模型属性的最佳方法是什么?
现在,我写了这个:
def user_params
params.require(:user).permit(User.fields.keys)
end
Run Code Online (Sandbox Code Playgroud)
你觉得有更好的方法吗?
在Rails 3中,可以将属性插入到params中,如下所示:
params[:post][:user_id] = current_user.id
Run Code Online (Sandbox Code Playgroud)
我试图在Rails 4中做类似的事情,但没有运气:
post_params[:user_id] = current_user.id
. . . .
private
def post_params
params.require(:post).permit(:user_id)
end
Run Code Online (Sandbox Code Playgroud)
Rails忽略了这种插入.它不会抛出任何错误,它只是悄然失败.
我正在使用rails 4.0分支的设计以及ruby 2.0.0p0和Rails 4.0.0.beta1.
这是一个问题,我在检查我是否以正确的方式进行,或者我还有其他事情要做.我确信很多人转向Rails 4.0都面临着同样的问题(谷歌搜索类似的东西后).
我已阅读以下链接:
现在使用devise我创建了一个User模型,我使用上面的gists创建了以下控制器(并确保将它包含在我的routes文件中).我的额外参数是first_name和last_name.
class Users::RegistrationsController < Devise::RegistrationsController
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
end
def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
end
private :sign_up_params
private :account_update_params
end
Run Code Online (Sandbox Code Playgroud)
还有什么我应该做的吗?这是从现在开始做事的最佳方式(因为放弃了attr_accessor).我的表单似乎工作正常(新的和更新).gists说使用"resource_params"但是在我的服务器日志中总是给出"Unpermitted parameters"错误.
我的模型中有一个数组字段,我正在尝试更新它.
我的强参数方法如下
def post_params
params["post"]["categories"] = params["post"]["categories"].split(",")
params.require(:post).permit(:name, :email, :categories)
end
Run Code Online (Sandbox Code Playgroud)
我在控制器中的动作如下
def update
post = Post.find(params[:id]
if post and post.update_attributes(post_params)
redirect_to root_url
else
redirect_to posts_url
end
end
Run Code Online (Sandbox Code Playgroud)
但是,每当我提交更新帖子时,在我的开发日志中我都会看到
Unpermitted parameters: categories
Run Code Online (Sandbox Code Playgroud)
传递的参数是
Parameters: {"utf8"=>"?", "authenticity_token"=>"auth token", "id"=>"10",
"post"=>{"name"=>"Toni Mitchell", "email"=>"eileen_hansen@hayetokes.info", "categories"=>",2"}}
Run Code Online (Sandbox Code Playgroud)
我想这与属性categories是一个数组的事实有关,因为其他一切看起来都很好.然后,我可能是错的.那么,我的代码出了什么问题,为什么在明确允许的情况下不让我保存类别字段呢?谢谢.
我已将Devise添加到我的Rails 4应用程序中,并成功将用户名等添加到我的用户模型中.此外,我能够使用lazy way™存储这些字段,即
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname) }
end
end
Run Code Online (Sandbox Code Playgroud)
但是,我试过了
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname) }
devise_parameter_sanitizer.for(:edit) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname) }
end
Run Code Online (Sandbox Code Playgroud)
但是这并没有像预期的那样工作(编辑动作调用时没有存储用户名).为了让它发挥作用,我还需要做些什么吗?谢谢!
更新:在ActiveAdmin中已经有一个解决方案之前询问了这个问题.正如Joseph所述,ActiveAdmin文档现在包含此信息,但此处的答案是为使用旧版ActiveAdmin的人提供的.
当strong_parameters 0.1.4与Rails 3.2.8中的ActiveAdmin 0.5.0一起使用时,如果您使用的模型使用StrongParameters,则包括:
include ::ActiveModel::ForbiddenAttributesProtection
Run Code Online (Sandbox Code Playgroud)
如果您尝试创建/编辑记录,则会在日志中收到以下错误:
ActiveModel::ForbiddenAttributes (ActiveModel::ForbiddenAttributes)
Run Code Online (Sandbox Code Playgroud) ruby-on-rails activeadmin ruby-on-rails-3.2 strong-parameters
我使用以下参数创建一个http put请求:
{"post"=> {"files"=> {"file1"=>"file_content_1","file2"=>"file_content_2"}},"id"=>"4"}
我需要在我的代码中允许哈希数组.基于手册,我尝试过这样:
> params.require(:post).permit(:files) # does not work
> params.require(:post).permit(:files => {}) # does not work, empty hash as result
> params.require(:post).permit! # works, but all params are enabled
Run Code Online (Sandbox Code Playgroud)
如何正确使用?
UPD1:file1,file2 - 是动态密钥
我想知道如何集成这两个宝石(设计+强参数),因为强的params可能会被添加到4.0中的rails core
欢迎任何帮助谢谢