NoMethodError: undefined method `permit' for #<Hash:0x007facebc78c98> in Rails Minitest - ruby-on-rails

i am getting this permit error for strong parameters in rails Minitest.
This works fine when i run through normal flow(apart from testing)
Whenever i run my test it gives me this weird error.
Why this doesn't work only while testing
parameter
#params_return_type = {:client_notes_and_action_items=>[{"notes_action_type"=>"return_help", "title"=>"test title", "description"=>"", "to_do_action_items_attributes"=>{"0"=>{"linked_item_id"=>"987", "linked_item_type"=>"client_purchases_shipping_detail", "initial_request"=>"true"}}, "estimated_completion"=>'Mon, 30 Mar 2020', "assigned_to"=>"45", "assigned_by"=>"41"}],"client_id"=>"76576"}
Error while running test
NoMethodError: undefined method `permit' for #<Hash:0x007facebc78c98>
Did you mean? print
this is my strong parameter
private
def self.client_notes_and_action_item_params(params)
params.permit( :client_id, :notes_action_type, :category, :description, :user_id, :image,
:comments, :status, :estimated_completion, :actual_completion, :title,
to_do_action_items_attributes: [:id, :linked_item_id, :linked_item_type, :deleted,
:initial_request])
end
my api code is like that
params[:client_notes_and_action_items].each do |client_notes_and_action_item|
ClientNotesAndActionItem.transaction do
action_item = ClientNotesAndActionItem.new(client_notes_and_action_item_params(client_notes_and_action_item))
and code breaks here on the last line of the code
Any idea whats the issue.
If strong parameter was an issue then i should get this error for all , normal flow works fine
only Minitest test breaks.

You need to convert Hash object to ActionController::Parameters object as permit is a method of ActionController::Parameters
params = {:client_notes_and_action_items=>[{"notes_action_type"=>"return_help", "title"=>"test title", "description"=>"", "to_do_action_items_attributes"=>{"0"=>{"linked_item_id"=>"987", "linked_item_type"=>"client_purchases_shipping_detail", "initial_request"=>"true"}}, "estimated_completion"=>'Mon, 30 Mar 2020', "assigned_to"=>"45", "assigned_by"=>"41"}],"client_id"=>"76576"}
#params_return_type = ActionController::Parameters.new(params)
Give it a try.

Related

Rails 5 Permitting controller action changed params

So i have the following params permitted.
p = params.permit(:a, :b, :c, :lines => [:location_id, :quantity, :product => [:id]])
In my controller action, i add to the lines param the data i've permitted.
p['lines'] << {"product"=>{"id"=>"123456"}, "quantity"=>"2", "location_id"=>"123456"}
This is how the params looked after they've been changed.
puts params['lines']
#> [<ActionController::Parameters {"product"=>{"id"=>"123456"}, "quantity"=>"2", "location_id"=>"123456"} permitted: false>]
But as you can see it's not permitted. What am i missing here? I am using Rails 5.
To get permitted (whitelisted) params, you always have to make sure that you call the permitted version, p in your case, whenever params changes.
The difference between params and p is that params.permit(...) returns a permitted copy of itself and assigns it to p. So params permission state remains unchanged.
Try with puts p['lines'] instead of puts params['lines'] to see if you get the desired result.

undefined method `keys' for "{\"prompt\"=>\"What is your name?\"}":String

I have a serialized field that I want to use in functional tests in Rails 4.
Fixtures are saving the hash as a string.
Step.rb
serialize :custom, ActiveRecord::Coders::NestedHstore
steps.yml
one:
name: Simple example.
custom:
name:
prompt: What is your name?
In step.custom, I want the hash {name: {prompt: "What is your name?"}. In testing, instead, I get a string along with the error:
undefined method `keys' for "{\"prompt\"=>\"What is your name?\"}":String
This is causing my tests to fail of course, because my codebase is expecting a Hash. I tried <%= hash.to_yaml.inspect %> but that doesn't seem to work.
I would try this:
require "yaml"
new_hash = YAML.parse hash

How to permit custom params using strong parametes gem?

I am getting params as follows.
{"utf8"=>"✓", "authenticity_token"=>"auth_token=", "js_data_entry"=>[{"data_entry"=>[{"name"=>"test_book22", "node_id"=>"65", "field_type"=>"Text", "options_attributes"=>[{"option_value"=>""}], "location"=>"Body", "rank"=>"", "must_fill"=>"0", "multi"=>"0", "role"=>["4"], "update_db"=>"vocation.name", "select_db"=>""}], "js_editor"=>[{"field_id"=>"", "field_class"=>"", "js_code"=>""}]}], "node_id"=>"65", "commit"=>"Submit", "action"=>"create", "controller"=>"data_entries"}
I tried to permit params as follows
def entry_params
params.require(:js_data_entry).permit([:node_id, :field_type, :name, :location, :rank, :multi, :must_fill, :update_db, :select_db, :role, :options_attributes])
end
but it's throwing
undefined method `permit' for #<Array:0xc603e78>
Please help me to fix this issue.
I fixed the issue as follows..
params.require(:js_data_entry)[0].permit(:data_entry=>[:node_id, :field_type, :name, :location, :rank, :multi, :must_fill, :update_db, :select_db, :role=>[], :options_attributes=>[:option_value]], :js_editor=>[:field_id,:field_class,:js_code])

undefined method merge_wrapper_options

I am trying to use a fake input for Simple form as documented here: https://github.com/plataformatec/simple_form/wiki/Create-a-fake-input-that-does-NOT-read-attributes.
f.input :address, as: :fake
However, I get an error "undefined method `merge_wrapper_options' for #". I get this error even after restarting the rails server.
Please help me solve this.
Thanks.
Summary
The instance method merge_wrapper_options is defined on the SimpleForm::Inputs::Base class, but not until version 3.1.0.rc1.
Here's the relevant source code for version 3.0.2 (no merge_wrapper_options):
https://github.com/plataformatec/simple_form/blob/v3.0.2/lib/simple_form/inputs/base.rb
Contrast this with version 3.1.0.rc1:
https://github.com/plataformatec/simple_form/blob/v3.1.0.rc1/lib/simple_form/inputs/base.rb
So if you're at v3.0.2 or prior, you won't have it. But, no big deal, just define the method yourself:
Code
/app/inputs/fake_string_input.rb
class FakeStringInput < SimpleForm::Inputs::StringInput
# Creates a basic input without reading any value from object
def input(wrapper_options = nil)
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
template.text_field_tag(attribute_name, nil, merged_input_options)
end # method
def merge_wrapper_options(options, wrapper_options)
if wrapper_options
options.merge(wrapper_options) do |_, oldval, newval|
if Array === oldval
oldval + Array(newval)
end
end
else
options
end
end # method
end # class
/app/views/some_form.html.haml
= f.input :some_parameter,
label: false,
as: :fake_string,
input_html: { value: 'some-value' }
The POST request will contain:
Parameters: {"utf8"=>"✓", "some_parameter"=>"some-value" }

undefined method `text?' for nil:NilClass Validate uniqness rails3 ruby 187

I just updated from rails 2.3 to 3, i'm trying to replace this old method with something cleaner, because it's outputting the model and field name, wtf!
However I get the above error when calling validates_uniqueness_of (the presence works fine). I passed in the primary id scope, and still get it. Any help is welcome.
def validate
if org_name.blank?
errors.add(:org_name, :blank, :default => nil)
else
if (org = Organization.find_by_org_name(org_name)) && org != self
errors.add(:org_name, :taken, :default => nil, :value => org_name)
end
end
end
to
validates :org_name, :presence => true
validates_uniqueness_of :org_name, :scope => :org_id
Ths is the Rails 3 syntax for uniqueness validtion:
validates :org_name, uniqueness: {scope: :org_id}
This is easy to fix.
Firstly, analyse the error message:
Org name translation missing:
en.activerecord.errors.models.user.attributes.org_name.blank
This is caused by the following line of code:
errors.add(:org_name, :blank, :default => nil)
When you call the above, you are telling rails to look for a translation whose key is :blank. You probably didn't set that up yet, so to do that, just go into your locales file (config/locales/en.yml), and add the following:
en:
hello: "Hello world"
activerecord:
errors:
models:
organization:
attributes:
org_name:
blank: "can't be blank."
Hopefully that will fix it for you.

Resources