I have the following code in my "ProgramsController.rb " file where, Im using a class called "DataTableDelegate" which is in a separate file called: "datatable_delegate.rb"
# GET /programs
# GET /programs.json
def index
puts "Running Program/index"
puts "Model name = #{controller_name.classify}"
respond_to do |format|
format.html
#datatable_options = generate_datatable_hash(view_context, controller_name.classify, Program.data_table_attribute_array )
log_with_blue("============================================")
log_with_yellow("#{#datatable_options.inspect}")
log_with_blue("============================================")
>>>>>> format.json { render json: DataTableDelegate.new( #datatable_options) }
end
end
The file "datatable_delegate.rb" is located at
app/datatables/datatable_delegate.rb
When I load the Programs url in the browser I get the following in my log:
Completed 500 in 237ms
NameError - uninitialized constant ProgramsController::DataTableDelegate:
activesupport (4.0.0) lib/active_support/dependencies.rb:500:in `load_missing_constant'
activesupport (4.0.0) lib/active_support/dependencies.rb:183:in `const_missing'
app/controllers/programs_controller.rb:22:in `block (2 levels) in index'
actionpack (4.0.0) lib/action_controller/metal/mime_responds.rb:191:in `respond_to'
app/controllers/programs_controller.rb:13:in `index'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
......
I tried to put a require statement in my programs controller file but I still am getting the error.....
What should I do?
Thanks
You do not need the require statement in your ProgramsController as all files in the app/ directory are autoloaded by Rails.
The problem is the way you're accessing the DataTableDelegate. It is namespaced with Datatable, hence the placement of this file is in app/datatables/ directory.
Try with the following:
::Datatable::DatatableDelegate.new( #datatable_options)
Please note the case of characters in the module and class names above.
Rename your file to 'data_table_delegate.rb'. Also check whether the path app/datatables is in your autoload_paths.
Related
The following form intends to generate an XHR response
<% articlediscounts_for_article = #articlediscounts.where(article_id: article.id).first %>
<%= form_with(scope: articlediscounts_for_article, url: user_discount_users_path, local: false, method: :post) do |form| %>
the action processes
Processing by UsersController#user_discount as JS
and generates expected results, but fails in the rendering process, complaining about (with initial stack)
ActionController::UnknownFormat (UsersController#user_discount is missing a template for this request format and variant.
request.formats: ["text/javascript", "*/*"]
request.variant: []):
actionpack (6.1.3.2) lib/action_controller/metal/implicit_render.rb:42:in `default_render'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
Yet that template clearly exists in the proper path (its corresponding partial is also present in the directory) as shown here:
This is perplexing as the warning message is contrary to the reality. The only assumptions that come to mind are that of some handling of form_with cmobined with the scope or the question of variant
What is mistaken here?
I can do a very simple integration test using capybara to test that a page renders correctly. Unfortunately, the tests can pass even if there is an error, if the test happens to find the required content in the error page.
Example test:
scenario 'goes to "new booking" page' do
visit '/bookings/new'
expect(page).to have_content('Driver')
end
Here's an example of the output for a failed test, that only happened to fail because the tested string was not in the error page:
Failure/Error: expect(page).to have_content('Invited')
expected to find text "Invited" in "TypeError at /bookings/86703360-4d92-4b79-bdfd-bbfbf843143e =========================================================== > no implicit conversion of nil into String app/models/vehicle.rb, line 11 ------------------------------ ``` ruby 6 has_many :drivers, through: :workables 7 8 has_and_belongs_to_many :job_types 9 10 def name_with_user > 11 name + \" \" + name_of_user 12 end 13 14 def name_with_user_and_jobs 15 name + \" \" + name_of_user + \" jobs\" 16 end ``` App backtrace ------------- - app/models/vehicle.rb:11:in `name_with_user' - app/views/bookings/show.html.erb:17:in `_app_views_bookings_show_html_erb__3294924100850065400_70296497966640' - app/controllers/bookings_controller.rb:115:in `show' - spec/features/booking_spec.rb:18:in `block (2 levels) in ' Full backtrace -------------- - app/models/vehicle.rb:11:in `name_with_user' - app/views/bookings/show.html.erb:17:in `_app_views_bookings_show_html_erb__3294924100850065400_70296497966640' - actionpack (4.0.8) lib/action_view/template.rb:143:in `block in render' - activesupport (4.0.8) lib/active_support/notifications.rb:161:in `instrument' - actionpack (4.0.8) lib/action_view/template.rb:141:in `render' - actionpack (4.0.8) lib/action_view/renderer/template_renderer.rb:49:in `block (2 levels) in render_template' - actionpack (4.0.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument' - activesupport (4.0.8) lib/active_support/notifications.rb:159:in `block in instrument'
You can wrap the text in span/div and test for the text in selector, for eg:
have_css("#status", text: "Invited") #not tested
http://www.rubydoc.info/gems/capybara/0.4.0/Capybara/Node/Matchers
I think you may want to use within. Example:
within('.some-css-class') { expect(page).to have_content 'some content' }
This way you can narrow your search to a particular div or anything else using css selectors.
so how is that possible?
I have a Module, that puts a Ruby Object at the end of the renderd page in a nice structured HTML. So i recurse through the given object and build the HTML output. The following is an excerpt of the code where the error is thrown.
EDIT:(had a copy error in code)
o=some object
nicer=if o.respond_to?(:empty?) and o.empty?
add_class='empty'
'empty ' + class_name
else
case o
when TrueClass then
"TRUE"
when FalseClass then
"FALSE"
when Array
#some more when's
the error thrown: undefined method 'empty?' for #Journey::Routes:0x123456
the object (o) it self is ActionDispatch::Routing::RouteSet
again: how is that possible?
EDIT: Stack: (there is the bad one ...)
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:366:in `empty?'
lib/tech_draw.rb:90:in `format_nice'
lib/tech_draw.rb:101:in `block in format_nice'
lib/tech_draw.rb:100:in `each'
lib/tech_draw.rb:100:in `map'
lib/tech_draw.rb:100:in `format_nice'
lib/tech_draw.rb:124:in `block in format_nice'
lib/tech_draw.rb:123:in `map'
lib/tech_draw.rb:123:in `format_nice'
lib/tech_draw.rb:13:in `block in say'
lib/tech_draw.rb:13:in `map'
lib/tech_draw.rb:13:in `say'
lib/tech_draw.rb:13:in `map'
lib/tech_draw.rb:6:in `say'
app/controllers/home_controller.rb:131:in `any_page'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
...
How is that possible? Easy:
class Thing
def respond_to? *args
true
end
end
o = Thing.new
o.respond_to?(:empty?) and o.empty?
# => NoMethodError: undefined method `empty?' for #<Thing:0x00000100ae2558>
Although why is it happening in this case is another matter.
ActionDispatch::Routing::RouteSet#empty? appears to call empty? on routes object. Assuming this object is an instance of Journey::Routes that would explain the error, as Journey::Routes doesn’t have an empty? method. (In current Rails versions Journey is part of Rails itself, but in Rails 3.2 it is separate).
I don’t why this is happening in your case though.
I am currently working on the Ruby on Rails tutorial by Michael Hartl. I am trying to add a page for each user in my database by creating an HTML with embedded ruby page in the views directory. The code for show.html.erb is below:
<%= #user.name %>, <%= #user.email %>
When I add the user to the user_controller.rb file, it looks like this:
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
end
end
When I run the rails server and click on open up the users/1 URL, I get a NameError complaining about an uninitialized constant. The error and trace is below:
NameError in UsersController#show
uninitialized constant UsersController::User
Rails.root: /usr/sample_app
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:3:in `show'
actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.12) lib/active_support/callbacks.rb:414:in
.
.
.
.
Please let me know how to go about this because I cannot pass my spec tests with this error. If anyone has any suggestions or insight I would greatly appreciate them.Thank you.
Controller file name should be users_controller.rb
As you mentioned in comment you don't have any User model defined. To create model run rails g model user name:string email:string. This will create User model with attributes name and email.
Try Rails scaffolding and see what files it creates(in controller, model and views ignore other files for now) and see the contents of those files. To use scaffold for creating User is as below:
rails g scaffold user name:string email:string
rake db:migrate
Strange error in diagnostics.erb file about _set_controller_content_type.
Please help.
NoMethodError in Timelines#public_timeline
Showing /opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/templates/rescues/diagnostics.erb where line # raised:
undefined method `content_type' for nil:NilClass
Extracted source (around line #):
RAILS_ROOT: /Volumes/DATA/Source/Rails/tvider
Application Trace | Framework Trace | Full Trace
/opt/local/lib/ruby1.9/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/whiny_nil.rb:52:in `method_missing'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_view/base.rb:331:in `_set_controller_content_type'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_view/renderable.rb:32:in `block in render'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_view/base.rb:306:in `with_template'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_view/renderable.rb:30:in `render'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_view/template.rb:205:in `render_template'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_view/base.rb:265:in `render'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:134:in `rescue_action_locally'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:152:in `rescue_action_without_handler'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:74:in `rescue_action'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:162:in `rescue in perform_action_with_rescue'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:160:in `perform_action_with_rescue'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/flash.rb:146:in `perform_action_with_flash'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:in `process'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/filters.rb:606:in `process_with_filters'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/base.rb:391:in `process'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/base.rb:386:in `call'
/opt/local/lib/ruby1.9/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:437:in `call'
Request
Parameters:
None
Show session dump
Response
Headers:
{"Cache-Control"=>"no-cache",
"Content-Type"=>""}
I don't know if this is the cause of your error. But when I got this error, it was because I unthinkingly defined a "response" action in my controller. Renamed the action, and all was well.
Code Example:
class PagesController < ApplicationController
def request
end
def response
end
end
What action are you trying to call in the controller ?
check the action_name in the controller !