Devise session_controller ArgumentError - ruby-on-rails

I created a session_controller to handle token authentication, but I got this error:
Started POST "/users/sign_in" for 127.0.0.1 at 2013-04-29 20:29:44 +0200
Processing by SessionsController#create as */*
Parameters: {"user_login"=>{"login"=>"admin#example.com.", "password"=>"[FILTERED]"}}
Completed 500 Internal Server Error in 1ms
ArgumentError (wrong number of arguments (2 for 1)):
activerecord (3.2.13) lib/active_record/attribute_methods.rb:24:in `[]'
.../bundler/gems/devise-dceb788c6b53/lib/devise.rb:436:in `block (2 levels) in configure_warden!'
warden (1.2.1) lib/warden/session_serializer.rb:35:in `fetch'
warden (1.2.1) lib/warden/proxy.rb:212:in `user'
warden (1.2.1) lib/warden/proxy.rb:318:in `_perform_authentication'
warden (1.2.1) lib/warden/proxy.rb:104:in `authenticate'
warden (1.2.1) lib/warden/proxy.rb:114:in `authenticate?'
.../bundler/gems/devise-dceb788c6b53/app/controllers/devise_controller.rb:124:in `require_no_authentication'
My controller looks like:
class SessionsController < DeviseController
before_filter :ensure_params_exist
respond_to :json
def create
build_resource
resource = User.find_for_database_authentication(:email => params[:user_login][:login])
return invalid_login_attempt unless resource
if resource.valid_password?(params[:user_login][:password])
sign_in("user", resource)
render :json => {:success => true, :auth_token => resource.authentication_token, :email => resource.email}
return
end
invalid_login_attempt
end
And my route file:
devise_for :users, :controllers => {:sessions => "sessions"}
Whats wrong?

Your class declaration does not match Devise namespace:
class SessionsController < Devise::SessionsController

Related

Rails Integration testing with rspec and fixture_file_upload

I recently ran into an issue with an integration test using rspec.
RSpec.describe "Images", type: :request do
before(:each) do
#user = User.create(username: "sam", password: "password")
#tag = #user.tags.create(name: "outdoors")
post login_path, params: { username: "sam", password: "password" }
expect(session[:user_id]).to eql #user.id
#file = fixture_file_upload("/dingo.jpeg", "image/jpeg")
end
it "can show images" do
#image = #user.images.create(caption: "hello", image_file: #image)
get user_image_path(#user, #image)
expect(response).to be_success
end
end
The before each creates a valid user, tag and logs the user in. Then it loads an image file.
The test it's self creates a new image for the #user and assigns it a value for caption and the loaded file for its :image_file.
The Image model uses has_one_attached :image_file to associate an uploaded image with an image record
class Image < ApplicationRecord
belongs_to :user
has_and_belongs_to_many :tags
has_one_attached :image_file
end
the controller and action as follows:
class ImageController < ApplicationController
before_action :unauthorized_redirect
def show
#user = current_user
#image = Image.find(params[:id])
#tags = #user.tags.all
authorize #image, :show?
end
private
def image_params
params.require(:image).permit(:caption, :image_file, tag_ids: [])
end
end
When I run the test I get an error:
Failures:
1) Images can show images
Failure/Error: <%= image_tag #image.image_file, class: "img-fluid show-image" %>
ActionView::Template::Error:
Can't resolve image into URL: undefined method `persisted?' for nil:NilClass
# ./app/views/image/show.html.erb:7:in `_app_views_image_show_html_erb__3727703739275822003_20580'
# <internal:kernel>:90:in `tap'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/actiontext-6.1.4.1/lib/action_text/rendering.rb:20:in `with_renderer'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/actiontext-6.1.4.1/lib/action_text/engine.rb:59:in `block (4 levels) in <class:Engine>'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/tempfile_reaper.rb:15:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/etag.rb:27:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/conditional_get.rb:27:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/head.rb:12:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:266:in `context'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:260:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:37:in `call_app'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:26:in `block in call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:26:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/runtime.rb:22:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/sendfile.rb:110:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/engine.rb:539:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:119:in `request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rails-controller-testing-1.0.5/lib/rails/controller/testing/integration.rb:16:in `block (2 levels) in <module:Integration>'
# ./spec/requests/images_spec.rb:38:in `block (2 levels) in <main>'
# ------------------
# --- Caused by: ---
# NoMethodError:
# undefined method `persisted?' for nil:NilClass
# ./app/views/image/show.html.erb:7:in `_app_views_image_show_html_erb__3727703739275822003_20580'
Finished in 0.79372 seconds (files took 0.46552 seconds to load)
30 examples, 1 failure
Failed examples:
rspec ./spec/requests/images_spec.rb:36 # Images can show images
I have debugged this issue with breakpoints everywhere and I just don't understand why it's not working in test when it works in production. I hope I've provided enough information this bug has been driving me crazy for the past two days.
Thank you!
Versions:
rails: 6.1.4.1
rspec: 5.0.2
Changed one line and fixed it
it "can show images" do
#image = #user.images.create(caption: "hello", image_file: #image)
get user_image_path(#user, #image)
*expect(response).to be_success ----REMOVE*
expect(response.successful?).to eql true
end

ActiveRecord::RecordNotFound - Couldn't find User with id= in Rails

I am following this tutorial link for JQuery validations.
In the link they gave the routes as:
in routes.rb
map.check_email “users/check_email”, :controller => “users”, :action => “check_email”
map.resources :users
As it is throwing error, I changed:
match '/check_email' => 'users#check_email', :as => 'check_email'
devise_for :users, :controllers => {:sessions => 'sessions'}, :path => '', :path_names => { :sign_in => "login", :sign_out => "logout" }
Added jquery.validate to assets/javascripts.
users_controller.rb:
def check_email
Rails.logger.debug "check_email..................."
#user = User.find_by_email(params[:user][:email])
respond_to do |format|
format.json { render :json => !#user }
end
end
def show
#user = User.find(params[:id])
#hide_logo_section = Rails.application.config.custom.force_report_logo_accounts.include?(#user.account.name)
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #user }
end
end
In sessions/new,the login form is followed by Forgot password.
app/views/sessions/new.html.erb:
<%= semantic_form_for(resource_name, :url => password_path(resource_name), :remote => true, :format => :json, :html => { :id => 'password_reset' }) do |f| %>
<%= f.inputs do %>
<%= f.input :email, :label => 'Your email address', :input_html => { :id => 'user_email_field',:placeholder => "Enter your email..."}%>
<label id="error_explanation1" class="error errorExplanation" style="margin-top:-10px;"></label>
<% end %>
<%= f.buttons do %>
<%= f.commit_button :label => 'Send me that link', :button_html => {:class => 'submit button', :id => 'forgot_password_button', :disable_with => 'Wait...' }%>
<% end %>
<% end %>
<script type="text/javascript">
$(document).ready(function () {
alert('hellllllll');
$('#password_reset').validate({
debug: true,
rules: {
'user[email]': {required: true, email: true,
remote:'/users/check_email' }
}
});
});
</script>
I am calling controller in the above script with remote.
The required and email validations are fine but it is throwing error when checking the method. Infact it is not going to the method check_email and showing id=check_email in the log.
This is my log:
Started GET "/login" for 127.0.0.1 at 2016-11-04 15:36:22 +0530
Processing by SessionsController#new as HTML Rendered
sessions/new.html.erb within layouts/home (1152.6ms) Rendered
home/_header.html.erb (0.5ms) Rendered home/_footer.html.erb (0.4ms)
Completed 200 OK in 1718ms (Views: 1315.0ms | ActiveRecord: 0.0ms)
Started GET "/assets/application.js" for 127.0.0.1 at 2016-11-04
15:36:24 +0530 RailsDevTweaks: Skipping ActionDispatch::Reloader hooks
for this request.
Started GET "/users/check_email?user%5Bemail%5D=test1%40gmail.com" for
127.0.0.1 at 2016-11-04 15:36:38 +0530 RailsDevTweaks: Skipping ActionDispatch::Reloader hooks for this request. Processing by
UsersController#show as JSON Parameters:
{"user"=>{"email"=>"test1#gmail.com"}, "id"=>"check_email"} User
Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."account_id"
IS NULL AND "users"."id" = ? AND (users.deleted_at IS NULL) LIMIT 1
[["id", "check_email"]] Completed 404 Not Found in 331ms
ActiveRecord::RecordNotFound - Couldn't find User with id=check_email
[WHERE "users"."account_id" IS NULL AND (users.deleted_at IS NULL)]:
(gem)
activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:343:in
find_one' (gem)
activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:314:in
find_with_ids' (gem)
activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:107:in
find' (gem)
activerecord-3.2.13/lib/active_record/associations/collection_association.rb:95:infind' (gem)
activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:46:in
find' (gem)
inherited_resources-1.3.1/lib/inherited_resources/base_helpers.rb:44:in
resource' (gem) cancan-1.6.9/lib/cancan/inherited_resource.rb:12:in
load_resource_instance' (gem)
cancan-1.6.9/lib/cancan/controller_resource.rb:32:inload_resource'
(gem) cancan-1.6.9/lib/cancan/controller_resource.rb:25:in
load_and_authorize_resource' (gem)
cancan-1.6.9/lib/cancan/controller_resource.rb:10:inblock in
add_before_filter' (gem)
activesupport-3.2.13/lib/active_support/callbacks.rb:429:in
_run__917766069841795016__process_action__2108608744963441679__callbacks'
(gem) activesupport-3.2.13/lib/active_support/callbacks.rb:405:in
__run_callback' (gem)
activesupport-3.2.13/lib/active_support/callbacks.rb:385:in
_run_process_action_callbacks' (gem)
activesupport-3.2.13/lib/active_support/callbacks.rb:81:in
run_callbacks' (gem)
actionpack-3.2.13/lib/abstract_controller/callbacks.rb:17:in
process_action' (gem)
actionpack-3.2.13/lib/action_controller/metal/rescue.rb:29:in
process_action' (gem)
actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:30:in
block in process_action' (gem)
activesupport-3.2.13/lib/active_support/notifications.rb:123:inblock
in instrument' (gem)
activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in
instrument' (gem)
activesupport-3.2.13/lib/active_support/notifications.rb:123:in
instrument' (gem)
actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:29:in
process_action' (gem)
actionpack-3.2.13/lib/action_controller/metal/params_wrapper.rb:207:in
process_action' (gem)
activerecord-3.2.13/lib/active_record/railties/controller_runtime.rb:18:in
process_action' (gem)
actionpack-3.2.13/lib/abstract_controller/base.rb:121:inprocess'
(gem) actionpack-3.2.13/lib/abstract_controller/rendering.rb:45:in
process' (gem)
actionpack-3.2.13/lib/action_controller/metal.rb:203:indispatch'
(gem)
actionpack-3.2.13/lib/action_controller/metal/rack_delegation.rb:14:in
dispatch' (gem)
actionpack-3.2.13/lib/action_controller/metal.rb:246:inblock in
action' (gem)
actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:73:in
call' (gem)
actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:73:in
dispatch' (gem)
actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:36:in
call' (gem) journey-1.0.4/lib/journey/router.rb:68:inblock in
call' (gem) journey-1.0.4/lib/journey/router.rb:56:in each' (gem)
journey-1.0.4/lib/journey/router.rb:56:incall' (gem)
actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:612:in
call' (gem)
meta_request-0.2.1/lib/meta_request/middlewares/app_request_handler.rb:11:in
call'
Please helpActiveRecord::RecordNotFound - Couldn't find User with id=
The problem is on your routes. Check what rails docs says:
Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.
When you call the URL: /users/check_email actually you're executing the users#show action.
Change your check_email route to something like
get '/users/validation/check_email', to: 'users#check_email', as: :check_email
and it will work.
ps: don't forget to update the url on your javascript.

Using Rails URL helpers within a module

I'm versioning my API with Versionist, so right now my routes file looks like
Rails.application.routes.draw do
api_version(:module => "V1", :path => {:value => "v1"}) do
resources :concepts, except: [:new, :edit]
end
end
And my controller is at app/controllers/v1/concepts_controller.rb.
And inside spec/controllers/v1/concepts_controller_spec.rb I have
...
describe "POST #create" do
context "with valid params" do
it "creates a new Concept" do
expect {
post :create, {:concept => valid_attributes}, valid_session
}.to change(Concept, :count).by(1)
end
...
end
end
...
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) { {description: 'Sample description.'} }
it "updates the requested concept" do
concept = Concept.create! valid_attributes
put :update, {:id => concept.to_param, :concept => new_attributes}, valid_session
concept.reload
expect(concept.description).to eq('Sample description.')
end
...
The PUT #update tests work fine, but all the POST #create ones give me
1) V1::ConceptsController POST #create with valid params creates a new Concept
Failure/Error: post :create, {:concept => valid_attributes}, valid_session
NoMethodError:
undefined method `concept_url' for #<V1::ConceptsController:0x007fd6e6d8ed38>
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:268:in `handle_model_call'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_dispatch/routing/url_for.rb:167:in `url_for'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:95:in `_process_options'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:43:in `block in _render_to_body_with_renderer'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:41:in `_render_to_body_with_renderer'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:37:in `render_to_body'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/rendering.rb:25:in `render'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:16:in `render'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `ms'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
# /usr/local/bundle/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:43:in `render'
# ./app/controllers/v1/concepts_controller.rb:24:in `create'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:198:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `block in instrument'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `instrument'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
# /usr/local/bundle/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:137:in `process'
# /usr/local/bundle/gems/actionview-4.2.2/lib/action_view/rendering.rb:30:in `process'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:632:in `process'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:65:in `process'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:514:in `post'
# ./spec/controllers/v1/concepts_controller_spec.rb:38:in `block (5 levels) in <top (required)>'
# ./spec/controllers/v1/concepts_controller_spec.rb:37:in `block (4 levels) in <top (required)>'
My rake routes:
Prefix Verb URI Pattern Controller#Action
v1_concepts GET /v1/concepts(.:format) v1/concepts#index
POST /v1/concepts(.:format) v1/concepts#create
v1_concept GET /v1/concepts/:id(.:format) v1/concepts#show
PATCH /v1/concepts/:id(.:format) v1/concepts#update
PUT /v1/concepts/:id(.:format) v1/concepts#update
DELETE /v1/concepts/:id(.:format) v1/concepts#destroy
How do I get it to use v1_concept_url instead? And why is it even calling concept_url when it isn't even a GET request?'
EDIT My controller:
class V1::ConceptsController < V1::BaseController
before_action :set_concept, only: [:show, :update, :destroy]
# GET /concepts
# GET /concepts.json
def index
#concepts = Concept.all
render json: #concepts
end
# GET /concepts/1
# GET /concepts/1.json
def show
render json: #concept
end
# POST /concepts
# POST /concepts.json
def create
#concept = Concept.new(concept_params)
if #concept.save
render json: #concept, status: :created, location: #concept
else
render json: #concept.errors, status: :unprocessable_entity
end
end
...
Apparently the location header is the culprit. Passing in the instance variable #concept causes Rails to invoke the concept_url helper.
You can override this by invoking the helper yourself:
location: v1_concept_url(#concept)

RSPEC test fails to POST a "create" action for nested resource

I have a resource called "parameters" that is nested under "tests" in my API. There relation is as follows: "A test has many parameters". I've structured my routes as follows
tests/:test_id/parameters/:parameter_id
and my rake routes gives:
test_parameters GET /tests/:test_id/parameters(.:format) parameters#index {:format=>:json}
POST /tests/:test_id/parameters(.:format) parameters#create {:format=>:json}
new_test_parameter GET /tests/:test_id/parameters/new(.:format) parameters#new {:format=>:json}
edit_test_parameter GET /tests/:test_id/parameters/:id/edit(.:format) parameters#edit {:format=>:json}
test_parameter GET /tests/:test_id/parameters/:id(.:format) parameters#show {:format=>:json}
PATCH /tests/:test_id/parameters/:id(.:format) parameters#update {:format=>:json}
PUT /tests/:test_id/parameters/:id(.:format) parameters#update {:format=>:json}
DELETE /tests/:test_id/parameters/:id(.:format) parameters#destroy {:format=>:json}
I'm writing simple rspec tests to test all the typical actions of my API. Right now, my POST/CREATE is giving me a very puzzling error.
Failures:
1) ParametersController POST create a new parameter with valid attributes successfully with name and times
Failure/Error: post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
NoMethodError:
undefined method `parameter_url' for #<ParametersController:0x000001072b4280>
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:220:in `polymorphic_method'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:114:in `polymorphic_url'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/url_for.rb:163:in `url_for'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:95:in `_process_options'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/streaming.rb:200:in `_process_options'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:43:in `block in _render_to_body_with_renderer'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:41:in `_render_to_body_with_renderer'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:37:in `render_to_body'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/rendering.rb:25:in `render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:16:in `render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `ms'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:43:in `render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:258:in `display'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:214:in `api_behavior'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:191:in `rescue in to_format'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:185:in `to_format'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:163:in `respond'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:156:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/respond_with.rb:203:in `respond_with'
# ./app/controllers/parameters_controller.rb:13:in `create'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:198:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `block in instrument'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `instrument'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:137:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionview-4.2.2/lib/action_view/rendering.rb:30:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:632:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:65:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:514:in `post'
# ./spec/controllers/parameters_controller_spec.rb:34:in `block (5 levels) in <top (required)>'
# ./spec/controllers/parameters_controller_spec.rb:33:in `block (4 levels) in <top (required)>'
I've tried changing the formats in how I'm submitting the POST but that hadn't changed anything. For now, my rspec test looks like this:
"spec/controllers/parameters_controller_spec.rb"
require 'spec_helper'
describe ParametersController do
before :each do
#test = FactoryGirl.create(:test, id: 1, name: 'Eccentric Tech')
#test.save
end
# ...
describe 'POST create a new parameter' do
context 'with valid attributes' do
it 'successfully with name and times' do
parameter = FactoryGirl.build(:parameter)
expect {
post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
}.to change(Parameter, :count).by(1)
end
end
context 'with invalid attributes' do
it 'fails without a name' do
parameter = FactoryGirl.build(:no_name_parameter)
expect {
post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
}.to change(Parameter, :count).by(0)
end
it 'fails without belonging to a test' do
parameter = FactoryGirl.build(:no_test_parameter)
expect {
post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
}.to change(Parameter, :count).by(0)
end
end
end
# ...
end
My Factory:
"spec/factories/parameters.rb"
require 'faker'
FactoryGirl.define do
factory :parameter do |f|
f.name { "#{Faker::Hacker.noun}" }
f.value { Faker::Number.number(2) }
f.test_id { 1 }
end
factory :no_name_parameter, parent: :parameter do |f|
f.name nil
f.value { Faker::Number.number(2) }
f.test_id { 1 }
end
factory :no_test_parameter, parent: :parameter do |f|
f.name { "#{Faker::Hacker.noun}" }
f.value { Faker::Number.number(2) }
f.test_id nil
end
end
And finally, my controller:
class ParametersController < ApplicationController
respond_to :json
def index
respond_with Parameter.all
end
def show
respond_with Parameter.find(params[:id])
end
def create
respond_with Parameter.create(parameter_params)
end
def update
respond_with Parameter.update(params[:id], parameter_params)
end
def destroy
respond_with Parameter.destroy(params[:id])
end
private
def parameter_params
params.require(:parameter).permit(:name, :value, :test_id)
end
end
routes.rb:
Rails.application.routes.draw do
scope :defaults => { :format => :json } do
resources :services do
resources :tests
end
resources :tests do
resources :parameters
resources :singular_results
resources :dataset_results
end
end
end
I solved this myself but I have nooooo idea why it works. If someone could explain it please do :)
All I did was make this small change to my controller:
def create
respond_with Parameter.create(parameter_params), location: nil
end
Found a good explanation: Rails: NoMethodError (Undefined method _url for _controller. I can't seem to respond_with json properly after my create. Why?

Rails: Action error in production but not in development

Can anyone explain why I keep getting an error in production but not in development? Relevant parts:
get: /user/logout
ActionController::RoutingError (uninitialized constant User::SessionController):
activesupport/lib/active_support/inflector/methods.rb:229:in `block in constantize'
activesupport/lib/active_support/inflector/methods.rb:228:in `each'
activesupport/lib/active_support/inflector/methods.rb:228:in `constantize'
actionpack/lib/action_dispatch/routing/route_set.rb:69:in `controller_reference'
actionpack/lib/action_dispatch/routing/route_set.rb:54:in `controller'
actionpack/lib/action_dispatch/routing/route_set.rb:32:in `call'
journey/lib/journey/router.rb:68:in `block in call'
journey/lib/journey/router.rb:56:in `each'
journey/lib/journey/router.rb:56:in `call'
actionpack/lib/action_dispatch/routing /route_set.rb:600:in `call'
omniauth/lib/omniauth/strategy.rb:177:in `call!'
omniauth/lib/omniauth/strategy.rb:157:in `call'
omniauth/lib/omniauth/builder.rb:48:in `call'
airbrake/lib/airbrake/rack.rb:27:in `call'
Routes:
Application1::Application.routes.draw do
match('/auth/:provider/callback' => 'session#create', :format => false)
root(:to => 'blog/archives#index', :format => false)
namespace(:user) do
match('/logout' => 'session#destroy', :format => false)
end
namespace(:blog) do
match('/archive/:slug' => 'archive#show', :format => false)
constraints(:page => /page\d+/) do
match('/archives/:page' => 'archives#index', :format => false)
end
end
end
I am using Rails 3.2.3 with the latest Omniauth.
You have created a namespace user and therefore you should place the session controller that defines the destroy action in this path:
/app/controllers/user/session_controller.rb
Then you can do stuff like:
Create a file in /app/controller/user/base_controller.rb that defines this:
class User::BaseController < ApplicationController
# Whatever you want here
end
And define the sessions controller located in /app/controllers/user/session_controller.rb as:
class Users::SessionsController < User::BaseController
def destroy
# whatever you want destroy to do..
end
end
Read this for more documentation about namespaces and routing.

Resources