Keva161
Keva161

Reputation: 2693

Rails - ArgumentError in UsersController#create - too few arguments

This is probably really basic but I can't seem to figure it out.

Essentially, when I try to create a new user using a form, and the user details are already present and not unique, I receive he following error:

ArgumentError in UsersController#create

too few arguments

Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:61:in `format'
app/controllers/users_controller.rb:61:in `create'

Here's my create action in my user_controller.rb:

  # POST /users
  # POST /users.xml
  def create
      @user = User.new(params[:user])

        if @user.save
          flash[:notice] = 'User successfully created' and redirect_to :action=>"index"
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
        end
      end
    end

And here's my user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :username, :password, :password_confirmation, :remember_me

  validates :email, :username, :presence => true, :uniqueness => true
end

Here's also my form:

<%= simple_form_for(@user) do |f| %>
  <div class="field">
    <%= f.input :username %>
  </div>
  <div class="field">
    <%= f.input :email %>
  </div>
    <div class="field">
      <%= f.input :password %>
    </div>
    <div class="field">
      <%= f.input :password_confirmation %>
    </div>
  <div class="actions">
    <%= f.button :submit %>
  </div>
<% end %>

Upvotes: 2

Views: 6816

Answers (2)

Michael Durrant
Michael Durrant

Reputation: 96484

The exact layout of the code has changed a bit through different rails version (please post what version you are using - check the Gemfile).

This example is for rails 3+ (it was generated using 3.2.5 but should apply to all 3+ or at least 3.1+ versions)

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

  respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'Blob was successfully created.' }
      format.json { render json: @user, status: :created, location: @user}
    else
      format.html { render action: "new" }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

p.s. Good choice with simple_form, it makes life MUCH easier!

Upvotes: 4

Dave Newton
Dave Newton

Reputation: 160201

What's format in this case?

There's no respond_to block, put it back in! You're referencing some other format.

Upvotes: 6

Related Questions