After installing Rails 4.0.2 from 4.0.0, my tests are failing. It works in the browser (development). This also happens with Rails 4.0.3.
20) Error:
UserFlowsTest#test_login_and_browse_site:
ActionView::Template::Error: No route matches {:id=>nil} missing required keys: [:id]
app/views/posts/_post.haml:10:in `_app_views_posts__post_haml__439576154_44475312'
app/views/posts/index.haml:1:in `_app_views_posts_index_haml__194952016_42560292'
app/controllers/posts_controller.rb:22:in `block (2 levels) in index'
app/controllers/posts_controller.rb:21:in `index'
test/integration/user_flows_test.rb:14:in `block in <class:UserFlowsTest>'
Here is line user_flows_test:14
post_via_redirect "/users/login", username: users(:chloe).username, password: 'passpasspass'
Here is line _post.haml:10
%a{href: user_path(post.user)}= post.user.username if post.user
The documentation is little help: http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-post_via_redirect
http://guides.rubyonrails.org/testing.html#helpers-available-for-integration-tests
Test output:
-----------------------------------------
UserFlowsTest: test_login_and_browse_site
-----------------------------------------
Started GET "/users/login" for 127.0.0.1 at 2014-02-14 21:35:13 -0500
Processing by UsersController#login as HTML
Rendered users/_login.haml (0.0ms)
Rendered users/_new.haml (41.0ms)
Rendered layouts/_header.haml (0.0ms)
Rendered layouts/_right_bar.haml (1.0ms)
Completed 200 OK in 47ms (Views: 47.0ms | ActiveRecord: 0.0ms)
Started POST "/users/login" for 127.0.0.1 at 2014-02-14 21:35:13 -0500
Processing by UsersController#loginCreate as HTML
Parameters: {"username"=>"chloe", "password"=>"[FILTERED]"}
"*********************************************************************** 1"
Redirected to https://www.example.com/
Completed 302 Found in 160ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2014-02-14 21:35:13 -0500
Processing by PostsController#index as HTML
Rendered posts/_vote.haml (7.0ms)
Rendered posts/_post.haml (18.0ms)
Completed 500 Internal Server Error in 45ms
E-------------------------------
This is the end of UsersController#loginCreate
session[:user_id] = #user.id
p "*********************************************************************** #{session[:user_id]}"
redirect_to root_url, :notice => "Logged in! Last seen from #{lastIP}."
This is from the Rails console
irb(main):009:0> p = Post.first
irb(main):011:0> app.user_path(p.user)
=> "/users/1"
Here is the simplest error
21) Error:
PostsControllerTest#test_should_get_index:
ActionView::Template::Error: No route matches {:id=>nil} missing required keys: [:id]
app/views/posts/_post.haml:10:in `_app_views_posts__post_haml__1028637779_43998912'
app/views/posts/index.haml:1:in `_app_views_posts_index_haml__48584214_43731948'
app/controllers/posts_controller.rb:22:in `block (2 levels) in index'
app/controllers/posts_controller.rb:21:in `index'
test/controllers/posts_controller_test.rb:26:in `block in <class:PostsControllerTest>'
So simple...
test "should get index" do
get :index ###### LINE 26
assert_response :success
assert_not_nil assigns(:posts)
end
More info
$ RAILS_ENV=test rails console
DL is deprecated, please use Fiddle
Loading test environment (Rails 4.0.3)
irb(main):001:0> Post.pluck(:user_id)
=> [0, 1, 2]
Maybe your user has not id:
post.user.id => id
This would mean post user is not persisted?
I had to change a user id from 0 to 1 in the post fixtures. That set of a chain reactions of changes. I also put the if on a different line in the HAML instead of at the end of the line, just in case. The HAML properties seem to be evaluated before the if statement.
Your test is testing a collection route, :index, but the test is showing an error on a member route, as indicated by it looking for an :id.
You're either instantiating your partial (app/views/posts/_post.haml) that has a link_to in it with a nil id or your routes file is the problem.
Related
I have written a rails-api with two resources users and books. When I make a GET request for books it works properly, but for users it fails with the following message:
Started GET "/users/" for 127.0.0.1 at 2015-11-18 23:45:52 +0530
Processing by UsersController#index as HTML
User Load (0.5ms) SELECT `users`.* FROM `users`
Completed 500 Internal Server Error in 63ms (ActiveRecord: 4.2ms)
ArgumentError (wrong number of arguments (0 for 1)):
app/controllers/users_controller.rb:8:in `index'
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb (17.6ms)
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.1ms)
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (69.4ms)
Here is the error I get on the browser:
ArgumentError in UsersController#index
wrong number of arguments (0 for 1)
Extracted source (around line #8):
...
# GET /users
def index
#users = User.all
render json: #users # this is line 8
end
...
config/routes.rb
Rails.application.routes.draw do
resources :users
resources :books
end
Any clue on what might be going wrong?
The error was with my model that was being passed to the serializer. I had a column in my database with the name method. method is a rails reserved word (read: ruby-and-rails-reserved-words) which was creating the problem. I renamed my column to registration_method which fixed the issue. Thanks everyone for the help :)
could try render #users.to_json
Is User.all returning any users?
When these partials are loaded in the browser I am unable to navigate away from the page via the browser back button. The page stays the same but the url changes.
What could be causing this?
match '/signup', to: 'users#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'get'
# /users/new
def new
#user = User.new
render partial: "new", layout: false
end
# /sessions/new
def new
render partial: "new", layout: false
end
Here's my stack. This is from just clicking the signin link once.
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Processing by SessionsController#new as */*
Processing by SessionsController#new as */*
Rendered sessions/_new.html.erb (0.8ms)
Rendered sessions/_new.html.erb (0.8ms)
Completed 200 OK in 4ms (Views: 3.3ms | ActiveRecord: 0.0ms)
Completed 200 OK in 4ms (Views: 3.3ms | ActiveRecord: 0.0ms)
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Processing by SessionsController#new as HTML
Processing by SessionsController#new as HTML
Rendered sessions/_new.html.erb (0.8ms)
Rendered sessions/_new.html.erb (0.8ms)
Completed 200 OK in 3ms (Views: 1.9ms | ActiveRecord: 0.0ms)
Completed 200 OK in 3ms (Views: 1.9ms | ActiveRecord: 0.0ms)
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Processing by SessionsController#new as HTML
Processing by SessionsController#new as HTML
Rendered sessions/_new.html.erb (0.8ms)
Rendered sessions/_new.html.erb (0.8ms)
Completed 200 OK in 2ms (Views: 1.6ms | ActiveRecord: 0.0ms)
Completed 200 OK in 2ms (Views: 1.6ms | ActiveRecord: 0.0ms)
Do you have a before filter redirecting public access to sessions#new? Have you defined /signin as a route?
I might be wrong, but as I wrote in the comment while ago I had a customer that used Turbolinks on Rails 3.2.12 and a group of customized JS libraries. The Turbolinks was causing a lot of issues such as double submitting and one of them was a similar thing to what you have mentioned. I had 3 choices:
1- Take out Turbolinks (my temp solution, until I showed the team TL was causing it)
2- Change the JS lib dependencies
3- Update the Rails App to 4.x latest. (I did this finally, it wasn't too bad)
In case if I was wrong and your case and setup was not similar to mine, here is a good thread on Github that some guy tries to solve it and still seems to be open.
https://github.com/rails/turbolinks/issues/256
Check your parameters :
Processing SESSION::SESSION#new (for 127.0.0.1 at 2014-04-04 13:50:03) [POST]
Parameters: {"authenticity_token"=>"XXXXX=", "param1"=>"1"}
Well in your log you will see what params you are sending if it is not sending what you want create params according to your columns or what you want to keep.
If you want create a back button you could try this:
link_to_function "Back", "history.back()"
Check your action form maybe you are sending other url
<% form_tag :controller=>"sessions",:action=>"new" do %>
<% #user.each do |user| %>
<%= user.name %>
<%= user.lastname %>
<% end %>
<% end %>
Here is my code for select_tag
<%= select_tag :tp, options_for_select([["movable",1],["fixed",2]]), :prompt => "select type of asset" %>
it works if i replace by:
<%= f.text_area :tp %>
If this in not enough i will give more code as you desire:
here is the log after i use select_tag
Started POST "/assets" for 127.0.0.1 at 2014-03-07 23:40:34 +0530
Processing by AssetsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Gib6rSODjXgXLZ9+5sDcq0ZatkA7144hU+Em2X7KONU=", "asset"=>{"name"=>"", "location"=>"", "cost"=>""}, "tp"=>"1","commit"=>"Save"}
(1.0ms) BEGIN
(1.0ms) ROLLBACK
Redirected to http://localhost:3000new
Completed 302 Found in 5ms (ActiveRecord: 2.0ms)
[2014-03-07 23:40:34] ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000new (or bad hostname?)
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/uri/generic.rb:1203:in `rescue in merge'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/uri/generic.rb:1200:in `merge'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpresponse.rb:275:in `setup_header'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpresponse.rb:205:in `send_response'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:110:in'run'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
Started GET "/assets/logo.png" for 127.0.0.1 at 2014-03-07 23:40:36 +0530
Error after changing to `real_assets_path
Started GET "/real_assets/new" for 127.0.0.1 at 2014-03-09 13:56:47 +0530
ActiveRecord::SchemaMigrationLoad(1.0ms)SELECT"schema_migrations".* FROM"schema_migrations"
Processing by AssetsController#new as HTML
Rendered assets/new.html.erb within layouts/application (30.0ms)
Completed 500 Internal Server Error in 98ms
ActionView::Template::Error (undefined method `assets_path' for #<# <Class:0x545bb78>:0x545aeb8>):
1: <div class = "container">
2: <div id = "assetnew">
3: <%= form_for(#asset) do |f| %>
4: <center><h1>Add Asset</h1></center>
5: <div class = "well">
6: <a class ="red">
app/views/assets/new.html.erb:3:in`_app_views_assets_new_html_erb___457532114_11608392'
Rendered
F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered
F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered
F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (18.0ms)
Assets
This is the error Rails is returning:
ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000new (or bad hostname?)
Without seeing your form, it seems you've got an issue with calling your resource /assets (as mentioned in the comments)
Basically, Rails treats the assets pipeline as a base directory - localhost:3000/your_asset_file.png -- it looks like this is what's happening here
Because your app is referencing /assets, I would surmise it's confusing it with the assets dir. Try doing this:
#config/routes.rb
resources :assets, as: "real_assets", path: "real_assets" #-> real_assets_path - /real_assets
Select
The select problem is likely caused by your assets path problem
select_tag is a standalone helper method (doesn't need any FormBuilder to help it). This means you should be able to just use it in an .html.erb file without any problems
The syntax for the select looks okay, so I'd recommend if you get your assets path fixed, you should be able to use it
I'm new to Rails (I use Rails 4.0.0). I want to write user management from scratch, without using popular solutions. So, here is my problem.
I have activate action in UsersController:
# GET /users/activate/:code
def activate
if Activation.activate(params[:code])
redirect_to root_path, notice: t('users.activation.success')
else
redirect_to root_path, alert: t('users.activation.failure')
end
end
Here is activate method in Activation model:
def self.activate(activation_code)
if a = Activation.find_by_code(activation_code)
a.destroy
return true
else
return false
end
end
My problem is that after passing valid code to activate action i'm getting my error message (users.activation.failure), but in fact record with activation code disappears from database. I noticed something strange in logs, when leaving a.destroy uncommented:
Started GET "/users/activate/f6dc4cccea5880d77821f0455ec9adf8" for 127.0.0.1 at 2013-09-25 18:53:54 +0200
...
Redirected to http://localhost:3000/
Completed 302 Found in 37ms (ActiveRecord: 30.5ms)
Started GET "/" for 127.0.0.1 at 2013-09-25 18:53:54 +0200
...
Completed 200 OK in 22ms (Views: 21.1ms | ActiveRecord: 0.0ms)
Started GET "/users/activate/f6dc4cccea5880d77821f0455ec9adf8" for 127.0.0.1 at 2013-09-25 18:53:55 +0200
...
Redirected to http://localhost:3000/
Completed 302 Found in 4ms (ActiveRecord: 0.7ms)
Started GET "/" for 127.0.0.1 at 2013-09-25 18:53:55 +0200
...
Completed 200 OK in 16ms (Views: 15.2ms | ActiveRecord: 0.0ms)
There are two /users/activate/f6dc4cccea5880d77821f0455ec9adf8 requests for one actual request. But when I tried to comment u.destroy it seemed to working just fine (without double request).
My question: what am I doing wrong?
Why do you need a separate model for activation? Just put activation code and activation date into users table.
I think that doing variable assignment instead of evaluating a condition is the problem. This will work:
def self.activate(activation_code)
a = Activation.find_by_code(activation_code)
if a.present?
a.destroy
return true
else
return false
end
end
I'm using rails 3 + devise. When a user signs up and there is an error. The app is redirecting to:
http://localhost:3000/users
Showing the error messages, but in the log I see the following:
Started POST "/users" for 127.0.0.1 at Mon Jul 11 20:22:19 -0700 2011
Processing by RegistrationsController#create as HTML
Parameters: {"commit"=>"Create my account", "fb_access_token"=>"XXXXX", "authenticity_token"=>"fWd/XXXX=", "utf8"=>"✓", "user"=>{"remember_me"=>"0", "lname"=>"XXXX", "fname"=>"XXXX", "password"=>"[FILTERED]", "email"=>"XXXX-inc.com"}, "fb_uuid"=>"312312"}
SQL (0.1ms) BEGIN
User Load (0.2ms) SELECT "users"."id" FROM "users" WHERE ("users"."email" = 'brett#companyline-inc.com') LIMIT 1
SQL (0.2ms) ROLLBACK
Rendered layouts/_header.html.erb (1.1ms)
Rendered registrations/new.html.erb within layouts/unauthorized (11.3ms)
Completed 200 OK in 45ms (Views: 22.6ms | ActiveRecord: 2.7ms)
Started GET "/users" for 127.0.0.1 at Mon Jul 11 20:22:20 -0700 2011
ActionController::RoutingError (No route matches "/users"):
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)
routes.rb:
# OmniAuth - for FB Connect
match '/users/auth/facebook/callback' => 'authentications#create'
# Devise
devise_for :users, :controllers => {:registrations => "registrations"}
Why the actionController error?
What does rake routes show you? The order in which you place your routes matters also, since the Rails router picks the first route it matches.
Hope this helps!