Shaundavin13
Shaundavin13

Reputation: 69

Undefined method `remember_me' in nested form for

I'm following Michael Hartl's tutorial but I'm getting this error when I run the bundle exec rake test:

1) Error:
UsersControllerTest#test_should_get_new:
ActionView::Template::Error: undefined method `remember_me' for #<User:0x000000058544b8>
    app/views/users/new.html.erb:25:in `block (2 levels) in _app_views_users_new_html_erb__1810619291483938060_46219160'
    app/views/users/new.html.erb:24:in `block in _app_views_users_new_html_erb__1810619291483938060_46219160'
    app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__1810619291483938060_46219160'
    test/controllers/users_controller_test.rb:6:in `block in <class:UsersControllerTest>'


  2) Error:
UsersSignupTest#test_invalid_signup_information:
ActionView::Template::Error: undefined method `remember_me' for #<User:0x00000006acef08>
    app/views/users/new.html.erb:25:in `block (2 levels) in _app_views_users_new_html_erb__1810619291483938060_46219160'
    app/views/users/new.html.erb:24:in `block in _app_views_users_new_html_erb__1810619291483938060_46219160'
    app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__1810619291483938060_46219160'
    test/integration/users_signup_test.rb:6:in `block in <class:UsersSignupTest>'


  3) Error:
UsersSignupTest#test_valid_signup_information:
ActionView::Template::Error: undefined method `remember_me' for #<User:0x000000074f6ff8>
    app/views/users/new.html.erb:25:in `block (2 levels) in _app_views_users_new_html_erb__1810619291483938060_46219160'
    app/views/users/new.html.erb:24:in `block in _app_views_users_new_html_erb__1810619291483938060_46219160'
    app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__1810619291483938060_46219160'
    test/integration/users_signup_test.rb:17:in `block in <class:UsersSignupTest>'

This is the new.html.erb file:

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@user) do |f| %>
      <%= render 'shared/error_messages' %>

      <%= f.label :name %>
      <%= f.text_field :name, class: 'form-control' %>

      <%= f.label :email %>
      <%= f.email_field :email, class: 'form-control' %>

      <%= f.label :password %>
      <%= f.password_field :password, class: 'form-control' %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation, class: 'form-control' %>

      <%= f.label :remember_me, class: "checkbox inline" do %>
        <%= f.check_box :remember_me %>
        <span>Remember me on this computer</span>
      <% end %>

      <%= f.submit "Create my account", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

^Just like listing 8.47 in the rails tutorial https://www.railstutorial.org/book/log_in_log_out

It seems rails thinks :remember_me is a method, but why??? Why is :remember_me thought to be a method when all the other labels aren't??? I don't fkcing get it.

Background: I've went through half a ruby tutorial and entered this rails tutorial. i have very basic html and css abilities. Never touched ruby before. And I don't really understand half of the concepts in this chapter. I mostly just followed the instructions. So please, explain in noob terms :)

Upvotes: 0

Views: 795

Answers (2)

Anton K
Anton K

Reputation: 279

You're placing the 'check box' code into the app/views/users/new.html.erb, which is a view template for the Sign Up, not Login.

To make the code working, place the check box code into the Login view template, i.e., app/views/sessions/new.html.erb.

Upvotes: 1

aguynamedloren
aguynamedloren

Reputation: 2273

The form in Listing 8.47 is for a session:

<%= form_for(:session, url: login_path) do |f| %>

but your form is for a user:

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

The form_for helper binds a form to an object. When that object is an instance of a model, like @user, rails expects each label and field to be an attribute of that model. Since remember_me is not an attribute on User, you get an error. You can fix this by adding a virtual attribute to your User model:

class User < ActiveRecord::Base
  attr_accessor :remember_me
  ...

This allows you to use remember_me in your user form, and sends params[:user][:remember_me] to your users controller when the form is submitted.

Upvotes: 4

Related Questions