1

I have been working on PHP. Presently trying to learn Ruby on Rails. I am learning Rails online, for now I am badly stuck on Sign-up or can say a form submission page. Sorry if it's too silly.

Error is:

undefined method new for nil:NilClass

Here is the code:

users_controller.rb

class UsersController < ApplicationController
  def new   
    @user= User.new
  end

  def create
    @user.new(params[:user])
    if @user.save
        flash[:notice]= "you signed up successfully"
        flash[:color]= "valid"
    else
        flash[:notice]= "failed"
        flash[:color]="invalid"
    end
    render "new"
  end
end

new.html.erb

<% page_title="Signup" %>
<div class="Sign_Form">
    <h1>Sign up</h1>
    <%= form_for(:user, :url => {:controller => 'users', :action => 'create'}) do |f| %>
    <p> Username:</br> <%= f.text_field :username%> </p>
    <p> Email:</br> <%= f.text_field :email%> </p>
    <p> Password:</br> <%= f.password_field :password%></p>
    <p> Password Confirmation:</br> <%= f.password_field :password_confirmation%> </p>
    <%= f.submit :Signup %>
  <% end %>
  <% if @user.errors.any? %>
    <ul class="Signup_Errors">
    <% for message_error in @user.errors.full_messages %>
      <li>* <%= message_error %></li>
    <% end %>
    </ul>
  <% end %>
</div>

user.rb

class User < ActiveRecord::Base

  attr_accessor :password
  EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
  validates :email, :presence => true, :uniqueness => true #:format => EMAIL_REGEX
  validates :password, `enter code here`:presence =>true #:confirmation => true #password_confirmation attr
  validates_length_of :password, :in => 6..20, :on => :create
end
3
  • What errors are you getting? Commented Aug 19, 2015 at 6:08
  • @LuisMenjivar updated the question Commented Aug 19, 2015 at 6:19
  • @akash see the answer below @user is nil because you are trying to create a new object on @user Commented Aug 19, 2015 at 6:23

4 Answers 4

1

In your users_controller > create, you put capital letter on User param.

For your case, it should be all lower case params[:user]. Side note, it actually depends on your attribute name you set on the form in the first place.

Edit: In addition of that you should put @user = User.new(params[:user])

Sign up to request clarification or add additional context in comments.

Comments

1

First thing you should create new object of User class Second pass correct params key

change first line in create method to

@user = User.new(params[:user])

So the changed code will look like this:

class UsersController < ApplicationController
  def new   
    @user= User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
        flash[:notice]= "you signed up successfully"
        flash[:color]= "valid"
    else
        flash[:notice]= "failed"
        flash[:color]="invalid"
    end
    render "new"
  end
end

2 Comments

it gives ActiveModel::ForbiddenAttributesError
0

change @user.new(params[:user]) to @user = User.new(params[:user]) I creates @user but it is not saved to database yet. On the line below @user.save that is when it gets saved. And remove render new because it will render the template with out setting the variables that the template needs. instead use redirect_to :new that will send the user to new and also set the variables needed

Comments

0

I guess you need to allow the params of User model in the User controller so as to avoid the forbidden error message as mentioned here. Please note that this is Rails feature as mentioned

Rails has several security features that help you write secure applications, and you're running into one of them now. This one is called strong parameters, which requires us to tell Rails exactly which parameters are allowed into our controller actions.

Thanks

Comments

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.