strong-parameters

rails 4 strong params: make them conditional?

馋奶兔 提交于 2019-12-22 07:07:43
问题 Is there a way to make strong_params conditional? Without the need to write 2 separate methods? In case where one would like to add certain attributes to the permit list when a certain condition is true For example: devise_parameter_sanitizer.for(:user) {|u| u.permit(:user, :email, :role, )} I have this :role attribute permitted in above example. I only want this attribute to be permitted when in Rails.env.development is there a way to do this? 回答1: Does this achieve the desired results? user

Strong_parameters not working

孤人 提交于 2019-12-21 03:01:21
问题 With Ruby 1.9.3, Rails 3.2.13, Strong_parameters 0.2.1: I have followed every indication in tutorials and railscasts, but I can not get strong_parameters working. It should be something really simple, but I can not see where is the error. config/initializers/strong_parameters.rb: ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection) config/application.rb config.active_record.whitelist_attributes = false app/models/product.rb class Product < ActiveRecord::Base end app

Rails 4 strong parameters failing when creating instances in rails console

人走茶凉 提交于 2019-12-20 11:56:31
问题 Probably doing something stupid here, but here's my basic cookie cutter class: class League < ActiveRecord::Base private def league_params params.require(:full_name).permit! end end And when creating a new instance of League: 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 What exactly am I doing wrong here? This is a Rails 4.0.0.beta1 build + Ruby 2.0 ** UPDATE ** I realize now that strong

Rails - Strong parameters with empty arrays

余生颓废 提交于 2019-12-20 09:27:34
问题 I'm sending an array of association ids, say foo_ids to my controller. To permit an array of values, I use: params.permit(foo_ids: []) Now, the problem is that if I send an empty array of foo_ids , the parameter is ignored. Instead of clearing all foos as an empty array should do, the association is left alone, because foo_ids isn't permitted. This may be because an empty array is converted to nil in rails, and that nil value is ignored as strong parameters is looking for an array of scalar

Strong Params, Nested Attributes and Mongoid don't seem to work because of *_attributes suffix in form?

ε祈祈猫儿з 提交于 2019-12-19 10:29:18
问题 I'm trying to embed property groups on my main Person model to help keep my code organised but Rails 4's Strong Params is getting in the way. The Scenario I have a Person model and a PersonName model like so: class Person include Mongoid::Document embeds_one :name, class_name: 'PersonName' accepts_nested_attributes_for :name end class PersonName include Mongoid::Document embedded_in :person, inverse_of: :name # fields here end I then use the fields_for helper in my form to nest the PersonName

strong parameter and json input rails 4

北城以北 提交于 2019-12-19 02:47:40
问题 I am trying to save data through JSON String in which I have nested associated attributes. I do not want to use attr_accessible. I almost got the logic of strong parameter but still got the problem to make them work. I am getting JSON string and using it to save data using this 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 "

Rails 4 strong parameters param not found error with carrierwave

ⅰ亾dé卋堺 提交于 2019-12-17 18:33:21
问题 I'm having trouble with carrierwave and rails 4 strong parameters. I have a very simple model with a carrier wave upload button. I'd like to show an error message if someone submits the upload form without choosing a file to upload. Right now, I get a param not found:photo error with this message: # Never trust parameters from the scary internet, only allow the white list through. def photo_params params.require(:photo).permit(:image) end This error is happening because Rails 4's strong

Rails 4 - Strong Parameters - Nested Objects

时光毁灭记忆、已成空白 提交于 2019-12-17 01:44:08
问题 I've got a pretty simple question. But haven't found a solution so far. So here's the JSON string I send to the server: { "name" : "abc", "groundtruth" : { "type" : "Point", "coordinates" : [ 2.4, 6 ] } } Using the new permit method, I've got: params.require(:measurement).permit(:name, :groundtruth) This throws no errors, but the created database entry contains null instead of the groundtruth value. If I just set: params.require(:measurement).permit! Everything get's saved as expected, but of

Strong parameters with nested hash

£可爱£侵袭症+ 提交于 2019-12-14 00:17:24
问题 I have the following params and cannot get the strong parameters to work. Here's my basic code, runnable in the Rails console for simplicity: json = { id: 1, answers_attributes: { c1: { id: "", content: "Hi" }, c2: { id: "", content: "Ho" } } } params = ActionController::Parameters.new(json) Everything I've read says the following should work, but it only gives me the id and an empty hash of answers_attributes : params.permit(:id, answers_attributes: [:id, :content]) => { "id"=>1, "answers

Create multiple entries with checkbox and strong params in Rails4

可紊 提交于 2019-12-13 04:55:52
问题 I am having problems with the following scenario: My users do searches by keywords which produces a list. The user has 2 actions either add them to a favorites table or block them using check boxes. The problem I have is that when users click "Add to Favorites" the form passes a list of hashes to my strong params method and I am not able to pass it correctly. I think the problem is that the hash required by strong_params is inside another hash. Also I have no idea on how to pass the same hash