What Is 'stringify_Keys' in Rails and How to Solve It When This Error Comes

What is `stringify_keys' in rails and how to solve it when this error comes

1) stringify_keys is a method that is called on a hash to convert its keys from symbols to strings. It's added by Rails - it's not a standard Ruby method. Here it is in the docs.

{:a => 1, :b => 2}.stringify_keys # => {"a" => 1, "b" => 2}

2) This means that your code is passing "/users/sign_in" somewhere that is expecting a hash. Closer inspection reveals that you are mixing and matching two forms of link_to:

# specify link contents as an argument
link_to "The text in the link", "/path/to/link", some: "options"

# specify link contents in a block
link_to "/path/to/link", some: "options" do
"The text in the link"
end

As you can see you are trying to do both:

<%= link_to "Sign out", destroy_user_session_path, :method => 'delete' do %>
<i class=" icon-user icon-black"></i>
<% end %>

and Rails expects the second argument in the block form to be the options hash, so it is calling stringify_keys on it which is causing your error.

Change those links to look like this instead:

<%= link_to destroy_user_session_path, :method => 'delete' do %>
<i class=" icon-user icon-black"></i> Sign out
<% end %>

ruby on rails: undefined stringify_keys

I would take a slightly different approach. Rather than adding a GET member route for profile to the users resources route, I would nest a resource route for profile within your users route.

# config/routes.rb
resources :users do
resource :profiles // notice the singular resource
end

This will provide the routes you need to RESTfully route to the nested profile resource.

Then you can create a form precisely as you've indicated:

# app/views/profiles/_form.html.erb
<%= form_for [@user, @profile] do |f| %>
...
<% end %>

In your ProfilesController, you can access the user in the following fashion:

# app/controllers/profiles_controller.rb
user = User.find(params[:user_id])
profile = user.profile

I'm not certain whether this will definitely resolve the error message you're receiving, but it very well might.

EDIT:

Regarding the comment below mentioning undefined method 'model_name' for NilClass:Class in your form: you're receiving this error because no variables are being passed to your partial scope. When rendering a partial, you need to pass in whatever local variables you'd like the partial to have access to:

# app/views/users/_form.html.erb
<%= render :partial => "profiles/form", :locals => {:user => @user, :profile => @profile} %>

Be cognizant, however, that the variables you pass to partial will only be accessible as local variables, not instance ones:

# app/views/profiles/_form.html.erb
<%= form_for [user, profile] do |f| %>
...
<% end %>

Ruby on Rails undefined method `stringify_keys'

Update the checkin method as below:

def checkin
params = ActionController::Parameters.new({:event => {date: todays_date(current_merchant}})
@event = current_merchant.events.create(params.require(:event).permit(:date)))
end

You were trying to create an associated event record for current_merchant by passing an instance of Event model to create method. create method expects Hash as an argument which is why you get the error.

undefined method `stringify_keys' for create:String in ruby on rails again

Ah, I know. You have a name clash.

action is a reserved name. Along with controller, id and maybe others. Use another name for your form.

When you post to /actions/create, then params[:controller] should be 'posts' and params[:action] should be 'create'. These params are assigned by Rails.

Stringify keys error when building a hash

The key is symbol in your first piece of code, and you have to return test at last in your second piece of code.

def attributes
test = {}
test[:city] = @content[1..20].strip
test[:streetname] = @content[21..40].strip
test[:house_number] = @content[41..46].strip.to_i
test
end

undefined method `stringify_keys' Rails 4

Video.new will take a hash not a string as you are doing.

How can I fix {:multiple = true} causing undefined method stringify_keys for nil:NilClass?

I fixed the error, I ended up doing the following:

<%= check_box_tag "tag_ids[]", tag.id, false, :id => "tag_ids_#{tag.id}" %>

instead of doing the multiple => true this works instead.



Related Topics



Leave a reply



Submit