I'm running through the Ruby on Rails Tutorial 3, and having a ball doing it, but I've just come across some problems that aren't getting solved. When I run my specs, two tests fail.
Failures:
1) UsersController PUT 'update' failure should render the 'edit' page
Failure/Error: put :update, :id => #user, :user => #attr
undefined local variable or method `object' for #<#<Class:0x00000102c861c8>:0x00000101d25558>
# ./app/views/shared/_error_messages.html.erb:3:in `_app_views_shared__error_messages_html_erb___3390867530789228804_2170854120__2806434579894406668'
# ./app/views/users/edit.html.erb:4:in `block in _app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416'
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416'
# ./app/controllers/users_controller.rb:47:in `update'
# ./spec/controllers/users_controller_spec.rb:158:in `block (4 levels) in <top (required)>'
2) UsersController PUT 'update' failure should have the right title
Failure/Error: put :update, :id => #user, :user => #attr
undefined local variable or method `object' for #<#<Class:0x00000102c861c8>:0x00000101b211f8>
# ./app/views/shared/_error_messages.html.erb:3:in `_app_views_shared__error_messages_html_erb___3390867530789228804_2170854120__2806434579894406668'
# ./app/views/users/edit.html.erb:4:in `block in _app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416'
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416'
# ./app/controllers/users_controller.rb:47:in `update'
# ./spec/controllers/users_controller_spec.rb:163:in `block (4 levels) in <top (required)>'
I've searched through my code as best I can comparing it against the code in the book, and I've come up with nothing. I'm sure it's one stupid little thing that I've missed, and I would greatly appreciate a second pair (or more ;) of eyes.
Here are my tests:
describe "failure" do
before(:each) do
#attr = { :email => "", :name => "", :password => "", :password_confirmation => "" }
end
it "should render the 'edit' page" do
put :update, :id => #user, :user => #attr
response.should render_template('edit')
end
it "should have the right title" do
put :update, :id => #user, :user => #attr
response.should have_selector("title", :content => "Edit User")
end
end
And here is the update methods from the users_controller:
def update
#user = User.find(params[:id])
if #user.update_attributes(params[:user])
flash[:success] = "Profile updated"
redirect_to #user
else
#title = "Edit User"
render 'edit'
end
end
Any thoughts on where I should look are greatly appreciated.
The source of the problem is not in spec files even though the problem manifests itself in those spec files. The instructor, Michael Hartl, changes the following statement:
<%= render 'shared/error_messages' %
to
<%= render 'shared/error_messages', :object => f.object %>
statement. In other words, he adds “, :object => f.object” to the first statement. And you must look in all files that have the original statement and change them to the second one. If you miss any of them you will have these errors. Specifically look in the following files (and any other that may have the original statement):
app/views/users/edit.html.erb
app/views/users/fields.html.erb
app/views/users/new.html.erb
app/views/shared/micropost_form.html.erb
Related
I'm now making Rspec test for users_controller.rb. However I'm in trouble the error NoMethodError: undefined method 'user_url' as follow.
FF
Failures:
1) UsersController PUT update user update does not succeed
Failure/Error: put :update, {:id => user.to_param}, valid_session, :user_route => user
NoMethodError:
undefined method `user_url' for #<UsersController:0x52e40e0>
# ./app/controllers/users_controller.rb:21:in `block (2 levels) in update'
# ./app/controllers/users_controller.rb:18:in `update'
# ./spec/controllers/users_controller_spec.rb:64:in `block (3 levels) in <top (required)>'
2) UsersController PUT update user update succeeds
Failure/Error: put :update, {:id => user.to_param}, valid_session, :user_route => user
NoMethodError:
undefined method `user_url' for #<UsersController:0x53bc560>
# ./app/controllers/users_controller.rb:21:in `block (2 levels) in update'
# ./app/controllers/users_controller.rb:18:in `update'
# ./spec/controllers/users_controller_spec.rb:58:in `block (3 levels) in <top (required)>'
Finished in 0.679 seconds
2 examples, 2 failures
Failed examples:
rspec ./spec/controllers/users_controller_spec.rb:61 # UsersController PUT update user update does not succeed
rspec ./spec/controllers/users_controller_spec.rb:56 # UsersController PUT update user update succeeds
Randomized with seed 33412
users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #user }
end
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update_attributes(params[:user])
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "user#edit" }
format.json { render json: #idea.errors, status: :unprocessable_entity }
end
end
end
end
Also here is my Rspec users_controller_spec.rb. I made two tests about "POST update". One is for being updated successfully. Another is for not being updated. (About the latter, I put the stub User.stub(:update_attribute).and_return(false) which I expect that "update_attribute" returns "false" so that process proceeds to "else".)
require 'spec_helper'
describe UsersController do
let(:valid_attributes) { {
"email" => "hoge#hogehoge.com",
"password" => "12345678"
} }
def valid_session
{}
end
describe "PUT update" do
it "user update succeeds" do
user = User.create! valid_attributes
put :update, {:id => user.to_param}, valid_session
assigns(:user).should eq(user)
end
it "user update does not succeed" do
user = User.create! valid_attributes
User.stub(:update_attribute).and_return(false)
put :update, {:id => user.to_param}, valid_session
assigns(:user).should eq(user)
response.should render_template("edit")
end
end
end
I have no idea to solve this, because I cannot understand where user_url did come. So I would like to have your help.
When you use redirect_to #user, rails sends that request to UsersController#show, but it does so by calling user_url(#user). If I had to guess, you probably don't have the line that defines user_url:
resources :users
in your routes.rb file. This would automatically create the named route user_url that your controller is referencing with redirect_to #user
Alternatively, you could define the route yourself in your routes.rb file like so:
get "/users/show" => "users#show", as: :user
But that's not really the 'Rails-y' way to do it. At any time, you can run the command rake routes in the terminal to see all the named routes you have defined in your routes.rb file. If user isn't there, then you need to define it like I mentioned above.
More info on named routes here: http://guides.rubyonrails.org/routing.html#singular-resources
If you are using devise then check if the following method returns anything.
def after_sign_in_path_for(resource)
in application_controller.rb
If the method returns nothing you will receive the error:
undefined method `user_url' for #
I also ended up removing
stored_location_for(resource)
in after_sign_in_path_for(resource) because it was causing an endless loop. Refer to this answer for details.
rails:3 Devise signup Filter chain halted as :require_no_authentication rendered or redirected
I run an RSpec testcase that fills a form and submits it. I get the following error:
1) Sign Up Advertiser after adding valid information should create a user
Failure/Error: expect { click_button submit }.to change(User, :user_key)
NoMethodError:
undefined method `model_name' for Fixnum:Class
# /mnt/hgfs/Projekte/adserve.example.de/app/controllers/advertisers_controller.rb:31:in `tryToCreateUser'
# /mnt/hgfs/Projekte/adserve.example.de/app/controllers/advertisers_controller.rb:14:in `create'
# ./sign_up_advertiser_spec.rb:32:in `block (4 levels) in <top (required)>'
# ./sign_up_advertiser_spec.rb:32:in `block (3 levels) in <top (required)>'
This is the code for the controller:
class AdvertisersController < ApplicationController
...
def home
#menuindex = 0
end
def create
#user = Advertiser.new (params[:advertiser])
tryToCreateUser
end
def tryToCreateUser
if #user.save
#user = Advertiser.retrieve(#user.id)
redirect_to home, :notice => "You successfully signed up " + #user.full_name
else
render :action => "/users/new", :layout => 'application'
end
end
end
And this is what the routes.rb looks like
match "signup_advertiser" => "advertisers#new", :as => "signup_advertiser"
match "signup_publisher" => "publishers#new", :as => "signup_publisher"
get "advertisers_home" => "advertisers#home"
resources :advertisers
So I guess the mistake is in the redirect_to part. But I can't figure it out. I fiddled around with rendering a custom action in 'home' and some other stuff. I think it's something pretty basic so help would be very appreciated. Thanks.
Yes, the problem is there. You should use home as a symbol on the redirect_to method:
def tryToCreateUser
if #user.save
#user = Advertiser.retrieve(#user.id)
redirect_to :home, :notice => "You successfully signed up " + #user.full_name
else
render :action => "/users/new", :layout => 'application'
end
end
What you do now is: redirect_to 0 since you are actually calling the controllers method "home".
Here is the error for update in rspec:
4) CustomersController GET customer page 'update' should be successful
Failure/Error: post 'update', customer
NoMethodError:
undefined method `symbolize_keys' for "1":String
# ./spec/controllers/customers_controller_spec.rb:38:in `block (3 levels) in <top (required)>'
The rspec code:
it "'update' should be successful" do
customer = Factory(:customer)
post 'update', customer
response.should be_success
end
The update in customers controller:
def update
#customer = Customer.find(params[:id])
if #customer.update_attributes(params[:customer], :as => :roles_new_update)
if #customer.changed
#message = 'The following info have been changed\n' + #customer.changes.to_s
#subject ='Customer info was changed BY' + session[:user_name]
notify_all_in_sales_eng(#message,#subject)
end
redirect_to session[('page'+session[:page_step].to_s).to_sym], :notice => 'Customer was updated successfaully!'
else
render 'edit', :notice => 'Customer was not updated!'
end
end
Any thoughts about the error? Thanks.
I will not enter into details on RSpec, but I just met the same error and this is how I would correct it for your code:
it "'update' should be successful" do
customer = Factory(:customer)
post 'update', :id => customer.id
response.should be_success
end
I think you can't provide your object directly to the post method, you must pass it's id as in a Hash instead.
(Please note too that this code assumes your customer exists in the test database, so your Factory must create it.)
Here is the error in rspec:
CategoriesController GET 'update' should be successful
Failure/Error: get 'update'
ActiveRecord::RecordNotFound:
Couldn't find Category without an ID
# c:in `find'
# ./app/controllers/categories_controller.rb:45:in `update'
# ./spec/controllers/categories_controller_spec.rb:35:in `block (3 levels) in <top (required)>'
Here is the code in controller:
def edit
#category = Category.find(params[:id])
end
def update
#category = Category.find(params[:id])
##category.reload. caused nil.reload error
if #category.update_attributes(params[:category], :as => :roles_update)
#category = Category.find(params[:id])
redirect_to #category, :notice => 'Category was successfully updated'
else
#categories = Category.all
render 'index'
end
end
Here is the rspec code:
describe "GET 'update'" do
it "should be successful" do
get 'update'
response.should be_success
end
end
Any thoughts? Thanks.
You pasted the create action instead of the update action. Also, you are trying to test the update action with a get request.. it should be with a put request if you are following the conventions.
If you had, say, the update action implemented... you would test more or less like:
describe CategoriesController do
let(:category) { mock_model(Category).as_null_object }
describe "PUT update" do
before do
Category.should_receive(:find).with(5).and_return(category)
end
context "when a category updates succesfully" do
before do
category.stub(:update_attributes).and_return(true)
end
it "redirects to the categories page" do
put :update, :id => 5, :category => { :some_val => 4 }
response.should redirect_to(categories_path)
end
it "sets the flash message" do
put :update, :id => 5, :category => { :some_val => 4 }
flash[:notice].should eq("Category was succesfully updated")
end
end
context "when a category does not update successfully" do
before do
category.stub(:update_attributes).and_return(false)
end
it "sets the flash message"
it "redirects to the categories page"
# etc etc
end
end
end
To get to this point (meaning the addition of mock models, stubs, what have you) you would normally start "fresh" so to speak and work your way up TDD style. Hope it helps
I'm working on the exercises from Chapter 10 of the Rails Tutorial and ran in to a snag with the exercise that has me ensure that an admin user can't delete themselves. My initial idea was to simply check the id of the current user and compare it against params[:id] to make sure that they're not equal. My destroy action in my Users controller looked like this:
def destroy
if current_user.id == params[:id].to_i
flash[:notice] = "You cannot delete yourself."
else
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
end
redirect_to users_path
end
This works perfectly when I test it manually in the app but 3 of my RSpec tests fail with the same "undefined method 'to_i'" error (as seen below):
1) UsersController DELETE 'destroy' as an admin user should destory the user
Failure/Error: delete :destroy, :id => #user
NoMethodError:
undefined method `to_i' for #<User:0x000001032de188>
# ./app/controllers/users_controller.rb:48:in `destroy'
# ./spec/controllers/users_controller_spec.rb:310:in `block (5 levels) in <top (required)>'
# ./spec/controllers/users_controller_spec.rb:309:in `block (4 levels) in <top (required)>'
2) UsersController DELETE 'destroy' as an admin user should redirect to the users page
Failure/Error: delete :destroy, :id => #user
NoMethodError:
undefined method `to_i' for #<User:0x000001032b5850>
# ./app/controllers/users_controller.rb:48:in `destroy'
# ./spec/controllers/users_controller_spec.rb:315:in `block (4 levels) in <top (required)>'
3) UsersController DELETE 'destroy' as an admin user should not allow you to destroy self
Failure/Error: delete :destroy, :id => #admin
NoMethodError:
undefined method `to_i' for #<User:0x0000010327e350>
# ./app/controllers/users_controller.rb:48:in `destroy'
# ./spec/controllers/users_controller_spec.rb:321:in `block (5 levels) in <top (required)>'
# ./spec/controllers/users_controller_spec.rb:320:in `block (4 levels) in <top (required)>'
If I use the params[:id] to find the user and compare it to the current_user like I have below then it works both in the app and in RSpec.
def destroy
if current_user == User.find(params[:id])
flash[:notice] = "You cannot delete yourself."
else
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
end
redirect_to users_path
end
Why would there be a problem in RSpec with the "to_i" method? If anyone is wondering I was leaning toward that approach because I thought it would best to simply compare the current user id to the id of the user targeted for deletion (via the params[:id]) instead of hitting the db to "find" the user.
For reference this is my RSpec test:
describe "DELETE 'destroy'" do
before(:each) do
#user = Factory(:user)
end
...
describe "as an admin user" do
before(:each) do
#admin = Factory(:user, :email => "admin#example.com", :admin => true)
test_sign_in(#admin)
end
it "should destory the user" do
lambda do
delete :destroy, :id => #user
end.should change(User, :count).by(-1)
end
it "should redirect to the users page" do
delete :destroy, :id => #user
response.should redirect_to(users_path)
end
it "should not allow you to destroy self" do
lambda do
delete :destroy, :id => #admin
end.should change(User, :count).by(0)
response.should redirect_to(users_path)
flash[:notice].should =~ /cannot delete yourself/
end
end
end
Any help would be appreciated!
In your specs, try using #user.id instead of #user on your :id parameter (I realize the Tutorial says to just use #user, but something may be going on where the id isn't being properly extracted):
delete :destroy, :id => #user.id
But you may consider restructuring to something like this:
#user = User.find(params[:id])
if current_user == #user
flash[:notice] = "You cannot delete yourself."
else
#user.destroy
flash[:success] = "User destroyed."
end