0

I'm learning to render forms from different controllers, but when I try to save data, it says that I get

NoMethodError in ProfilesController#create
undefined method `stringify_keys' for "2":String

My routes file:

resources :users do
  member do
   get 'profile'
  end
end

Profile model

belongs_to :user

User model

has_one :profile

views/profiles/_form.html.erb

<%= form_for [@user, @profile] do |f| %>
  ..
<% end %>

views/users/_form.html.erb

<%= render :partial => "profiles/form" %>

Also, to mention, when I tried to save the data, I get redirected to http://localhost:3000/users/2/profiles where the error occurs, instead of http://localhost:3000/users/2/profile notice the s in profile, it changes on me?

Thanks!

3
  • It's indicating that you have a nested resources route for profiles, but that's not showing in your routes. Have you posted the entire contents of your routes.rb? Commented Jun 17, 2013 at 5:01
  • In my experience every time I get the stringify keys error it has to do with a hash I'm passing into a method being constructed improperly. Commented Jun 18, 2013 at 4:42
  • @andrewliu, if you are rendering a partial from a partial, you should probably think about refactoring your code. Commented Jun 18, 2013 at 4:49

1 Answer 1

1

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 %>
Sign up to request clarification or add additional context in comments.

7 Comments

in which action do I put that code: user = User.find(params[:user_id])?
Doesn't matter. Presumably, you are in either your new or update actions, but in either scenario, you'll be able to access user_id via params[:user_id].
I get undefined method 'profiles' in my index action. I have this in it @user = User.find(params[:user_id]) and @profiles = @user.profiles
Because User has_one Profile, you should use the singular of "profiles" when doing this lookup, i.e., @user.profile. I've amended my answer above to show an example.
ahh good catch! but darn! I'm getting this error now: undefined method 'model_name' for NilClass:Class and its referring to the form in my views page, any idea what that is about? Thanks for the help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.