Tag: 强参数

深嵌套导轨4表格

我有3个模型Item接受嵌套的问题和问题属性接受嵌套的答案属性。 我正在尝试以相同的forms创建一个有问题和答案的项目。 item.rb的 class Item < ActiveRecord::Base has_many :questions, dependent: :destroy accepts_nested_attributes_for :questions end question.rb class Question < ActiveRecord::Base belongs_to :item has_many :answers, dependent: :destroy accepts_nested_attributes_for :answers end answer.rb class Answer < ActiveRecord::Base belongs_to :question end item_controller.rb class ItemsController [:content, :helper_text, :kind], :answers_attributes => [:content, :correct]) end end _form.haml = simple_form_for(@item) do |f| = f.input […]

强参数和json输入轨道4

我试图通过JSON字符串保存数据,其中我有嵌套的关联属性。 我不想使用attr_accessible。 我几乎得到了强参数的逻辑,但仍然有问题使它们工作。 我正在获取JSON字符串并使用它来使用它来保存数据 data = request.body.read @inputData = Person.new(JSON.parse(data)) @inputData.save! if@inputData.valid? render :status => 200, :json => “Data inserted successfully” else render :status => 404, :json => “Not Inserted ” end 我已经定义了permit strong参数方法允许这样的嵌套属性 def referral_params params.require(:person).permit(:id, user_attributes: [:id, :first_name, :last_name, :email], device_attributes: [:id, :os_type, :os_version], location_attributes: [:id, :latitude, :longitude], duration_attributes[:id, :start_time, :end_time]) end 但我不知道如何使用此regerral_params方法以及JSON输入字符串….

Rails 4 / Devise / MongoDB:使用自定义属性和强参数的“未允许的参数”

尝试将嵌套的自定义属性Profile (一个Mongoid文档)添加到我的设计用户类。 提交Devise注册表单时,它还应创建用户和相应的Profile对象。 我希望最终结果在我的MongoDB中看起来像这样: 用户: { # Devise fields: “email”: “my@email.com”, … # Custom field “profile” : “” } 轮廓: { “first_name”: “Dave”, …. } 不幸的是,每当我提交注册时,我都会在控制台中收到此消息。 它成功创建了一个用户,但无法创建关联的配置文件。 Started POST “/” for 127.0.0.1 at 2013-04-20 23:37:10 -0400 Processing by Users::RegistrationsController#create as HTML Parameters: {“utf8″=>”✓”, “authenticity_token”=>”awN2GU8EYEfisU0”, “user”=> {“profile_attributes”=> {“first_name”=>”Dave”, “birthday(2i)”=>”4”, “birthday(3i)”=>”21”, “birthday(1i)”=>”1933”, “occupation_title”=>”Software Developer”}, “password”=>”[FILTERED]”, “password_confirmation”=>”[FILTERED]”, “email”=>”my@email.com”}} […]

导轨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 编辑视图: html复选框没问题,但我不知道如何将值设置为objective 。 我试过定义objective_ids= objectives_ids但没有任何反应。 在控制器中: class ObjectivesController < ApplicationController def objective_params params.require(:objective).permit(:name, :code, :description, :objective_ids) end end 编辑日志文件显示未Unpermitted parameters: perspective_id, objective_ids

rails 4强参数+动态hstore键

我在使用Hstore和动态访问器克服Rails 4中新的强params需求时遇到了问题 我有一个Hstore列,名为:content ,我想用它来存储多种语言的内容,即:en, :fr等。我不知道在模型或控制器中预先设置哪种语言。 store_accessor :content, [:en, :fr] #+226 random other il8n languages won’t work. 如何在rails 4中为一列覆盖强params(或允许动态hstore键)? params.require(:article).permit( :name, :content, :en, :fr #+226 random translations ) 缺乏… params.require(:article).permit! 这当然有效。

Rails中的强参数3.2.8

该video表明可以保护通过控制器输入的输入,但仍然可以通过型号和规格进行质量分配。 但是,在3.2.8中使用strong_parameters时,我没有将此文档记录为function。 我知道我需要将ActiveModel::ForbiddenAttributesProtection混合到我的模型中,并在config/application.rb设置config.active_record.whitelist_attributes = false 。 我还从模型中提取了所有的attr_accessible调用。 无论有没有mixin,我都会遇到质量分配错误。 ActiveModel::MassAssignmentSecurity::Error: Can’t mass-assign protected attributes: home_phone, cell_phone 我错过了什么吗?

我应该如何将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 控制器: 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 查看(新用户的表单) […]

Rails 4强参数param not found with carrierwave

我遇到了载波和轨道4强参数的问题。 我有一个非常简单的模型,带有载波上传按钮。 如果有人提交上传表单而没有选择要上传的文件,我想显示错误消息。 现在,我得到一个param not found:photo的param not found:photo此消息的param not found:photo错误: # Never trust parameters from the scary internet, only allow the white list through. def photo_params params.require(:photo).permit(:image) end 发生此错误是因为Rails 4的强参数要求提供图像参数以提交表单,但由于用户未选择图像,因此不存在。 我无法找到解决此问题的方法,并将其重定向到相同的操作并显示错误消息。 有没有办法用强参数做到这一点? 当我尝试使用没有选择照片的表单时,这是开发日志: https : //gist.github.com/leemcalilly/09b6166ce1af6ca6c833 当我选择照片并成功上传时,这是开发日志: https : //gist.github.com/leemcalilly/1f1e584f56aef15e7af1 其他相关文件:* models / photo.rb – https://gist.github.com/leemcalilly/95c54a5df4e4ee6518da * controllers / photos_controller.rb – https://gist.github.com/leemcalilly/c0723e6dc5478b0f914d * uploaders / […]

#..Rails 4的未定义方法`model_name_question’

我在提交表单后收到此错误:(在索引页面中) 3, :style => “width:80%”, :placeholder => “enter your question.” %> 我有question模型: class Question < ActiveRecord::Base validates :question, presence: true belongs_to :category belongs_to :questioner end 和问题控制器: class QuestionsController < ApplicationController def index @quiz = Question.new @questioner = Questioner.new end def new @quiz = Question.new(quiz_params) end def show @quiz = Question.find(params[:id]) end def edit @quiz = […]

在Rails 4中使用强参数的值白名单

是否可以使用强参数来确保使用可能值的白名单过滤属性? 例如,我有一个参数年龄,我想确保只能有这个值[10,20,30,40,50]。 是否可以使用强参数逻辑来确保? 谢谢