Reputation: 335
I'm working through Michael Hartl's Rails tutorial, and I'm running into an issue in section 7.3.3. I receive this error message:
ArgumentError in Users#new
Showing /Users/Anuraag/rails_projects/sample_app/app/views/users/new.html.erb where line #6 raised:
First argument in form cannot contain nil or be empty
when I run the following rspec:
bundle exec rspec spec/requests/user_pages_spec.rb -e "signup with invalid information"
at the following line:
<%= form_for(@user) do |f| %>
Here is my code. Please also let me know how I should improve this question if I'm not framing it well: I'm pretty new to StackOverflow as well
app/views/users/new.html.erb:
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
app/assets/controllers/users_controller.rb:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def create
@user = User.new(user_params)
if @user.save
# Handle a successful save.
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
spec/requests/user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "[email protected]"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
end
Upvotes: 3
Views: 12508
Reputation: 849
The order of actions in controller file matters. I had the same error until I figure out the problem on order of action methods. Example. Controller file
def edit
@post = Post.find(params[:id])
end
def update
end
private
def post_params
params.require(:post).permit(:title, :body)
end
I was working on edit action and was getting same error as you. The problem was I have written the edit action under private action. once I changed the order of this actions. It worked like a charm.
What I was trying to achieve ? I was trying to take the values of post instance variable and use it into html form.
Upvotes: 1
Reputation: 53038
In your new.html.erb
you have specified form_for(@user)
that means you need to have some value in instance variable @user
before calling the new
action.
You can do it in two ways :
one way is define an action new
in your controller which would be called before rendering your new.html.erb
layout. For eg:
def new
@user = User.new
end
other way is that in your form itself you could specify something like
<%= form_for(User.new) do |f| %>
Defining it in your new
action is more standard way of doing it.
Upvotes: 13
Reputation: 2121
I dont see a new action on you controller.
def new
@user = User.new
end
I maybe made indirectly, but it couldn't hurt defining it yourself. From you title that may be why the @user variable is nil, it wasn't defined.
Upvotes: 0