Nick Res
Nick Res

Reputation: 2244

Can't get this form to work

I have a form that needs to update a user objects attributes.

Here are the three steps that I've boiled down to:

  1. the action 'new' needs to provide the form with an object to manipulate

  2. The form needs to know which object to collect values for

  3. when the form is submitted it needs to send these params to a function that updates the user object

If all this is kosher here's what I've done to try and make this happen.

Within the 'new' action I've attempted to provide the user object like so:

      @current_user  = current_user

Then, in the view this is what the form looks like:

<%= form_for (@current_user) do |f| %>
   <%= f.label :target_bf_pct %>
   <%= f.text_field :target_bf_pct %>

   <%= f.label :activity_factor %>
   <%= options_for_select([['Sedentary (Desk Job)', 1.2],
                                           ['Light Activity (1-3 day a week)', 1.35],
                                           ['Moderate Activity (3-5 days a week)', 1.55],
                                           ['Very Active ( 6-7 days a week)', 1.75],
                                           ['Extremely Active (Atheletic Endurance)', 1.95]])
%>

  <%# f.submit "Post", class:"btn btn-large btn-primary" %> 
<% end %> 

The error I'm getting at this point is this:

undefined method `user_path' for #<#<Class:0x007fd943d00d08>:0x007fd944e5b6c0>

which is referring to the forms @current_user object that's supposed to correspond to the @current_user instance within the new action...

Important note:

  1. I'm using devise for my sessions controller to create new users and handle sessions.

  2. I don't believe that my use of the form helper options_for_select is being used properly since I've yet to assign it a value it's supposed to represent once selected like "activity_factor"

Upvotes: 0

Views: 246

Answers (2)

Valery Kvon
Valery Kvon

Reputation: 4496

<%= form_for(@current_user) do |f| %>
            ^ no space please :)

This is not a joke.

<%= form_for (@current_user) do |f| %>

will work, but, for example:

<%= form_for (@current_user, :class => "user_form") do |f| %>

will not. With space you're on the way to do mistakes like that.

The second one. Your environment doesn't know user_path

Add to routes.rb (even if devise's statements exist):

resources :users

Upvotes: 1

Matchu
Matchu

Reputation: 85792

This error is probably coming from the form_for helper. It's trying to figure out the action URL for the form, so it observes that @current_user is a User, so calls user_path(@current_user). If the method is not defined, that usually means that there is no route defined for users#update.

Try running rake routes and confirm that the users#update route is present. If not, add it and try again.

Also, a few notes:

  • If you're looking to update an existing user, the form should probably be in the edit action. The new action is usually reserved for creating brand new users.
  • The current_user helper is accessible in both the controller and the view. So, you could use current_user directly in the view; setting it in the controller is a bit redundant. Just a small thing.
  • Devise offers its own users#edit form and users#update action for changing passwords and the like. I'm not sure on the details, but it seems likely that Devise makes it easy to override the existing form, so long as all the fields you want to edit are marked as attr_accessible. If it would be appropriate to merge these fields into the existing Devise form, that might be worth some research.

Upvotes: 3

Related Questions