akash
akash

Reputation: 29

Ruby on Rails - basic form submission

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

Upvotes: 1

Views: 73

Answers (4)

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

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

Upvotes: 1

Preetesh Patil
Preetesh Patil

Reputation: 1

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

Upvotes: 0

Luis Menjivar
Luis Menjivar

Reputation: 777

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

Upvotes: 0

fruqi
fruqi

Reputation: 5353

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])

Upvotes: 1

Related Questions