Undefined method 'status' for nil:nilclass - ruby-on-rails

Ok weird error. Everything was working fine.. and now its not.
Currently I have a simple many to one association.
Route is set up like this:
resources :apps do
resources :forms
end
App:
has_many :forms
Form:
belongs_to :app
Forms_controller index action:
def index
#app = App.find(params[:app_id])
#forms = #app.forms
respond_to do |format|
format.html # index.html.erb
format.json { render json: #forms }
end
end
I've taken every bit of code/html out of the forms.html.erb layout file so it should be rendering a blank page.
Instead I'm getting this error:
undefined method `status' for nil:NilClass
status isn't even defined anywhere in my app
help would be greatly appreciated.
EDIT:
Here is what is displayed in my development.log file
Started GET "/apps/4/forms" for 127.0.0.1 at 2011-09-05 23:14:16 -0700
Processing by FormsController#index as HTML
Parameters: {"app_id"=>"4"}
[1m[36mApp Load (0.1ms)[0m [1mSELECT "apps".* FROM "apps" WHERE "apps"."id" = ? LIMIT 1[0m [["id", "4"]]
[1m[35m (0.1ms)[0m SELECT COUNT(*) FROM "forms" WHERE "forms"."app_id" = 4
0
[1m[36mForm Load (0.1ms)[0m [1mSELECT "forms".* FROM "forms" WHERE "forms"."app_id" = 4[0m
Rendered forms/index.html.erb within layouts/forms (1.2ms)
Completed 500 Internal Server Error in 37ms
NoMethodError (undefined method `status' for nil:NilClass):

I had a similar issue - I had a method named 'response' that something internal to Rails was calling 'status' on and it similarly bailed without a stack trace to speak of.
With things named 'app' and 'forms' you might be running into something similar.

#app was not found, you can fetch forms within try
def index
#app = App.find(params[:app_id])
#forms = #app.try(:forms)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #forms }
end
end
If your template handles the #app, and it's important to have it, better to handle the exception:
def index
#app = App.find!(params[:app_id]) # raise an exception until find
#forms = #app.forms
rescue
flash[:error] = "App not found!"
end

First, please check log/development.log or on browser. It should help you where the error hapened.
Next, what is params value? Check log/development.log. It may look like the followings:
...
Processing FormsController#index (for 127.0.1.1 at YYYY-MM-DD hh:mm:ss) [GET]
Parameters: {...}
...

For internal server errors your application could not be started properly. You should check your server error log. It may give you insight as to what the problem is. If you, or a team member as the case may be, didn't make any changes that broke the application then you should check with your host. Perhaps they made changes to your environment that are causing an error.

Related

Can't generate pdf with wicked_pdf in rails ,using search results

Hi everybody I'm trying to use wicked pdf in rails.
Get a pdf based on the object generated by the query, #questions
When downloading the pdf, the query data is lost.
when I visit "/simulators.pdf" it shows me the following error in console
Started GET "/simulators.pdf" for 127.0.0.1 at 2018-11-01 12:38:19 -0500
Processing by SimulatorsController#index as PDF
Test Load (0.2ms) SELECT "tests".* FROM "tests" WHERE "tests"."name" IS NULL LIMIT $1 [["LIMIT", 1]]
CACHE Test Load (0.0ms) SELECT "tests".* FROM "tests" WHERE "tests"."name" IS NULL LIMIT $1 [["LIMIT", 1]]
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.2ms)
NoMethodError (undefined method `id' for nil:NilClass):
Controller:
def index
#test = Test.find_by(name: params[:test])
#questions = TestQuestion.joins(:test).where(test_id: #test.id).order('RANDOM()').limit(10)
respond_to do |format|
format.html
format.pdf { render template: 'simulators/pdf', pdf: 'pdf'}
end
end
URL:
http://localhost:3000/simulators?test=biology
This is url of index view
Can you help me?
Many thanks
that's because when you ask for the pdf, you need to send the query in the url too
http://localhost:3000/simulators.pdf?test=biology
if you don't send the variable in the url, the search that you are doing will be nil, that's the error you are having.

Why doesn't projects/12 show all fields for that project?

IMPORTANT NOTE: The id does not matter, it is same in all cases.
I am only seeing the name field, not description, hours, etc even though these aren't null.
I have declared all standard routes through resources (with default format json), not individually.
I have even tried creating a projects/show.json.jbuilder file:
json.name #project.name
json.description #project.description
json.hours #project.hours
json.ownername #project.ownername
My projects/show method:
#project = Project.find(params[:id])
render :json => #project
FIRST EDIT:
I added logger.debug right before defining #project in my show method.
Now in my command prompt window for the local server, I am seeing:
Started GET "/projects/12" for 127.0.0.1 at 2015-02-25 16:36:45 -0500
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ProjectsController#show as HTML
Parameters: {"id"=>"12"}
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1 [["id", 12]]
#<Project:0x007f95477e72f8>
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 51]]
Completed 200 OK in 42ms (Views: 15.2ms | ActiveRecord: 0.9ms)
I am wondering why I am seeing the "User Load" part, since my Project model does not belong to a User object or have any relationship with it (though in the past it may have had before that relationship was removed). Also, I don't think I saw
#<Project:0x007f95477e72f8>
earlier.
I solved my problem this way.
Basically, projects/12.json currently works and not projects/12. Since I am more interested in obtaining the data client side, it is ok for me. I would need a projects/show.html.erb page that calls all the project data through view helper methods, for the HTML to work.
def show
#project = Project.find(params[:id])
respond_to do |format|
format.html
format.json
end
end
projects/12.json works because I have a projects/show.json.jbuilder file (as I shared in my original question post):
json.name #project.name
json.description #project.description
json.hours #project.hours
json.ownername #project.ownername

Rails/Mailer - NoMethodError after sending email

I'm fairly new to Rails, so this will likely end up being something obvious; however I've just spent the better part of a day pulling my hair out over this issue.
I have a rails app which I've been working on for awhile, however I only started implementing mailers today. I followed the Rails ActionMailer tutorial here: http://guides.rubyonrails.org/v3.0.3/action_mailer_basics.html and the mailer works fine in a new example app. However, when I repeated those steps verbatim in my existing rails app (running in Development environment) I receive the below error. It creates the entry in the DB, correctly sends both the plain text & HTML emails and THEN generates the error. All I'm trying to do here is send a welcome email upon the creation of a new account, but I'm getting the same error when I try to send any email from any controller.
The specific error I'm seeing after it sends the welcome email is:
Completed 500 Internal Server Error in 280ms
NoMethodError (undefined method `error' for true:TrueClass):
app/controllers/musers_controller.rb:52:in `block in create'
app/controllers/musers_controller.rb:50:in `create'
Note that to not mess up my existing User table, I created a temporary scaffold & mailer called Muser which I plan on deleting once I'm confident this will work correctly on my user table.
Code
Error in log:
Started POST "/musers" for 127.0.0.1 at 2013-07-10 20:32:34 -0400
Processing by MusersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"OuoEmsjkAVBHZwqPO5b/O4eKw6iZBaLP6vUT6f9WCOI=", "muser"=>{"name"=>"New User", "email"=>"User#email.com"}, "commit"=>"Create"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (0.6ms) INSERT INTO "musers" ("created_at", "email", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Thu, 11 Jul 2013 00:32:34 UTC +00:00], ["email", "User#email.com"], ["name", "New User"], ["updated_at", Thu, 11 Jul 2013 00:32:34 UTC +00:00]]
(1.7ms) commit transaction
Rendered muser_mailer/registration_confirmation.html.erb (0.1ms)
Rendered muser_mailer/registration_confirmation.text.erb (0.0ms)
Completed 500 Internal Server Error in 280ms
NoMethodError (undefined method `error' for true:TrueClass):
app/controllers/musers_controller.rb:52:in `block in create'
app/controllers/musers_controller.rb:50:in `create'
Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.6ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.5ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (15.3ms)
--I should note that lines 50 & 52 of the musers_controller (where this error is being generated) correspond to the 'respond_to do' & 'MuserMailer.... .deliver' lines in the controller code below.--
The controller action:
# POST /musers
# POST /musers.json
def create
#muser = Muser.new(params[:muser])
respond_to do |format|
if #muser.save
MuserMailer.registration_confirmation(#muser).deliver
format.html { redirect_to #muser, notice: 'Muser was successfully created.' }
format.json { render json: #muser, status: :created, location: #muser }
else
format.html { render action: "new" }
format.json { render json: #muser.errors, status: :unprocessable_entity }
end
end
end
Mailer:
class MuserMailer < ActionMailer::Base
default from: "EmailAddress#Inter.net"
def registration_confirmation(muser)
#muser = muser
mail(:to => muser.email, :subject => "Registered")
end
end
I don't think that the issue is with my smtp, mail setup, or variables since it does actually add to the DB & send the emails correctly. If I comment out the line in the controller which calls the mail action the error disappears, so I don't think the problem is with my muser routes. This undefined method 'error' for true:TrueClass is driving me nuts. I did recently install Devise on my Users table, so I don't know if that could be causing the issue?
For lack of a better term, it feels like the issue is with how Rails wants to route after sending the emails; as if I need to put a Return or specify a route at the end of my mailer action telling the server to head back to the controller action. In other words, I'm lost!
Update
Below are the two mailer view files I'm using.
registration_confirmation.html.erb
<h3><%= #muser.name %>! You sweet sweet fool!</h3>
<p>Thank you for registering!</p>
registration_confirmation.text.erb
Thank you for registering!
Update 2
Here's my model for Muser:
class Muser < ActiveRecord::Base
attr_accessible :email, :name
end
I solved this issue - there was the errant line config.action_mailer.logger = true in my config/environments/development.rb file that was causing the issues. Once removed everything worked perfectly.

Rails Completed 406 Not Acceptable in 64ms

I have a form on my website with a page where I can edit / remove / add mailboxes:
http://example.com/settings/mailboxes
http://example.com/settings/mailboxes/3/edit
http://example.com/settings/mailboxes/3
etc.
Whenever I do something to the mailbox (update, destroy) I'm getting this error:
Redirected to http://example.com/
Completed 406 Not Acceptable in 64ms
But the data gets updated.
Here's the controller code:
# PUT /mailboxes/1
def update
#mailbox = Mailbox.find(params[:id])
if #mailbox.update_attributes(params[:mailbox])
redirect_to(root_path, :notice => 'Mailbox was successfully updated.')
else
render :action => "edit"
end
end
# DELETE /mailboxes/1
def destroy
#mailbox = Mailbox.find(params[:id])
#mailbox.destroy
redirect_to(root_path)
end
Here's routes.rb info:
match 'settings.js' => 'settings#javascript', :via => :get, :format => :js
scope '/settings' do
# Directs /settings/mailboxes/* to Settings::MailboxesController
# (app/controllers/settings/mailboxes_controller.rb)
resources :mailboxes
end
What am I doing wrong? Here's what log shows:
if #mailbox.update_attributes(params[:mailbox])
(rdb:2) response.status
200
(rdb:2) next
/Users/Fallen/Projects/support-app/trunk/app/controllers/mailboxes_controller.rb:65
redirect_to(mailboxes_path, :notice => 'Mailbox was successfully updated.')
(rdb:2) response.status
200
(rdb:2) next
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.0/lib/action_controller/metal/implicit_render.rb:5
default_render unless response_body
(rdb:2) response_body
[" "]
(rdb:2) response.status
406
(rdb:2) cont
Started PUT "/settings/mailboxes/5" for 127.0.0.1 at 2011-10-14 12:54:38 +0200
Status Load (0.4ms) SELECT `statuses`.* FROM `statuses` WHERE `statuses`.`name` = 'Incoming emails fetching' LIMIT 1
(0.1ms) BEGIN
(0.4ms) UPDATE `statuses` SET `last_action_at` = '2011-10-14 10:54:38' WHERE `statuses`.`id` = 1
(39.6ms) COMMIT
Processing by MailboxesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0cip2dsYre9anfy/8rEgtuYcgrgC3si6aSuppjzxuHU=", "mailbox"=>{"name"=>"Dev Support #4", "sender_name"=>"example.com Support #4", "email_address"=>"support_dev4#example.com", "color"=>"B2EB3D"}, "commit"=>"Update Mailbox", "id"=>"5"}
Mailbox Load (0.5ms) SELECT `mailboxes`.* FROM `mailboxes` WHERE `mailboxes`.`id` = 5 LIMIT 1
Status Load (0.5ms) SELECT `statuses`.* FROM `statuses` WHERE `statuses`.`name` = 'Incoming emails fetching' LIMIT 1
(0.1ms) BEGIN
(0.3ms) UPDATE `statuses` SET `last_action_at` = '2011-10-14 10:54:47' WHERE `statuses`.`id` = 1
(0.4ms) COMMIT
(0.2ms) BEGIN
(0.6ms) UPDATE `mailboxes` SET `color` = 'B2EB3D', `updated_at` = '2011-10-14 10:54:48' WHERE `mailboxes`.`id` = 5
Mailbox Load (0.7ms) SELECT `mailboxes`.* FROM `mailboxes`
(1.2ms) COMMIT
Mailbox Load (0.4ms) SELECT id, name, open_tickets_count FROM `mailboxes`
(0.3ms) SELECT COUNT(*) FROM `tickets` WHERE `tickets`.`closed` = 0
CACHE (0.0ms) SELECT COUNT(*) FROM `tickets` WHERE `tickets`.`closed` = 0
Redirected to http://localhost:3000/settings/mailboxes
Completed 406 Not Acceptable in 29008ms
406 happens when the content type that you are requesting is not one that an action knows how to respond with. For example if an action responds to html and json, but you request js, then it won't know how to respond.
If I'm not mistaken, it will do the work, but when it comes time to respond it will send back a 406.
I don't see any formats specified in your controller, but maybe you have them specified at the top with respond_to, or maybe I'm forgetting something about default behavior.
It looks like you are requesting js -- as an experiment, try wrapping this around your entire action:
respond_to do |format|
format.js do
# all your action code goes here...
end
end
Test adding the defaults format: :json in your routes.rb
# config/routes.rb
defaults format: :json do
# Your json routes here
# resources :example ...
end
Also, if you are using the responders gem, ensure that in your application_controller.rb you have a respond_to :json at the top.
I had this error and ended up here after googling. In my case I was missing the template but did not receive the usual Missing Template Error. I believe it was because I had respond_to :html at the top of the controller.

xml format needed in rails 3.0.1??? answer should be NO

what the heck am I doing wrong.
I changed nothing, but now that it is 2011 this call doesn't work as it used to.
Using Active Resource, I can make the following call:
>> Member.find(:all, :params => {:code => "1stb48024957856464d436e79ad9d7bc0c2d46d5a02"})
GET http://localhost:3000/members.xml?code=1stb48024957856464d436e79ad9d7bc0c2d46d5a02
--> 500 Internal Server Error (1601 371ms)
ActiveResource::ServerError: Failed with 500 Internal Server Error
As you can see, I get a 500 error. So i go to my server to where i'm making the call.
The code is here in the controller:
def index
#store = Store.first
#members = #store.members.find_by_code(params[:code])
respond_to do |format|
format.html # index.html.erb
format.xml { render #members.to_xml } # i also tried :xml => #members but that screws up too the same way.
end
end
But below is the stack trace. It's doing my head in why this just stopped working.
Started GET "/members.xml?code=1stb48024957856464d436e79ad9d7bc0c2d46d5a02" for 127.0.0.1 at 2011-01-21 13:58:00 -0500
Processing by MembersController#index as XML
Parameters: {"code"=>"1stb48024957856464d436e79ad9d7bc0c2d46d5a02"}
SQL (0.8ms) SHOW TABLES
Store Load (0.4ms) SELECT `stores`.* FROM `stores` LIMIT 1
Member Load (0.8ms) SELECT `members`.* FROM `members` WHERE (`members`.store_id = 1) AND (`members`.`code` = '1stb48024957856464d436e79ad9d7bc0c2d46d5a02') LIMIT 1
Completed in 268ms
ActionView::MissingTemplate (Missing template
4
1stb48024957856464d436e79ad9d7bc0c2d46d5a02
2010-12-02T20:14:54Z
3
70.2
10
http://fnd.nu/7
1
2010-12-09T15:37:42Z
with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:xml], :locale=>[:en, :en]} in view paths "/Users/nerb/current_project/fund/app/views", "/Users/nerb/.rvm/gems/ruby-1.9.2-p0/gems/devise-1.1.3/app/views", "/Users/nerb/current_project/fund", "/"):
app/controllers/members_controller.rb:16:in `block (2 levels) in index'
app/controllers/members_controller.rb:14:in `index'
Rendered /Users/nerb/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (31.6ms)
Any thoughts are appreciated. I thought xml formats were expempt from render? Jose said as much in this lighthouse ticket, but i can't find what he is talking about.
UPDATE:
Andy that worked as the Server seems to be sending a 200 so thanks, but now i get this error:
Member.find(:all, :params => {:code => "1stb48024957856464d436e79ad9d7bc0c2d46d5a02"})
GET http://localhost:3000/members.xml?code=1stb48024957856464d436e79ad9d7bc0c2d46d5a02
--> 200 OK (472 399ms)
NoMethodError: undefined method `collect!' for #
from /Users/nerb/.rvm/gems/ruby-1.8.7-p249/gems/activeresource-2.3.5/lib/active_resource/base.rb:662:in `instantiate_collection'
from /Users/nerb/.rvm/gems/ruby-1.8.7-p249/gems/activeresource-2.3.5/lib/active_resource/base.rb:639:in `find_every'
from /Users/nerb/.rvm/gems/ruby-1.8.7-p249/gems/activeresource-2.3.5/lib/active_resource/base.rb:582:in `find'
from (irb):1
Thoughts? should it just be an array coming back, even if it's ONE record?
render :xml => #members is the way to go.
Make sure you reload/restart your server after making this change if in production.

Resources