NoMethodError (undefined method `fetch_value' for nil:NilClass) in RAILS - ruby-on-rails

Table Name in SQL : road_ratings
Route:
get '/roadRatings', to: "road_ratings#index"
Controller: road_ratings_controller.rb
class RoadRatingsController < ApplicationController
def index
#data = RoadRating.all
render json: #data
end
end
Model: road_rating.rb
class RoadRating < ActiveRecord::Base
end
Upon running the above API, I get the error in rails server logs
NoMethodError (undefined method `fetch_value' for nil:NilClass):
And following error on browser
NoMethodError in RoadRatingsController#index
However when I run the query SELECTroad_ratings.* FROMroad_ratingsLIMIT 1 generated in rails logs on SQL, I get the correct result
Rails Logs:
Started GET "/roadRatings" for ::1 at 2016-10-04 23:55:57 -0400
Processing by RoadRatingsController#index as HTML
RoadRating Load (0.4ms) SELECT `road_ratings`.* FROM `road_ratings` LIMIT 1
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.4ms)
NoMethodError (undefined method `fetch_value' for nil:NilClass):
app/controllers/road_ratings_controller.rb:4:in `index'
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (23.1ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.8ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (16.7ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.7ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (39.4ms)
Rake Routes:
Kartiks-MacBook-Air:Mayor_project kartik$ rake routes
Prefix Verb URI Pattern Controller#Action
GET / home#default
streets GET /streets(.:format) streets#index
GET /street/:id(.:format) streets#show
potholes GET /potholes(.:format) potholes#index
roadRatings GET /roadRatings(.:format) road_ratings#index

Related

How to solve no method error in ruby on rails?

I defined my route and controller actions correctly. Still I'm getting a no method error.
Here is my code:
Route:
get "/restaurants", to: "restaurants#index"
Controller action:
def index
resto = Restaurant.all
render json: resto,status: :ok
end
here is my error in the browser:
NoMethodError in RestaurantsController#index
undefined method `type' for #<Restaurant:0x00007fb711a07628>
Extracted source (around line #4):
def index
resto=Restaurant.all
( render json: resto,status: :ok) # this is the line throwing error
end
end
And here is the problem in the console
Use Ctrl-C to stop
Started GET "/restaurants" for ::1 at 2022-08-16 22:26:25 +0530
(0.7ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by RestaurantsController#index as HTML
Restaurant Load (1.2ms) SELECT "restaurants".* FROM "restaurants"
↳ app/controllers/restaurants_controller.rb:4:in `index'
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::Attributes (12.1ms)
Completed 500 Internal Server Error in 38ms (ActiveRecord: 7.5ms | Allocations: 17269)

Rails, "undefined method `variant' for nil:NilClass" on render json

I am trying to do something really simple, render a json with some stuff.
I do have a view views/api/initialize.html.erb if this counts as useful info.
I have no idea what causes this but the variables filled alright, I checked
My controller:
class ApiController < ApplicationController
def initialize
#articles = Article.all
#areas = Area.all
#languages = Language.all
data_json = { articles: #articles, areas: #areas, languages: #languages }
render json: data_json
end
def index
end
end
My route:
get '/init', controller: 'api', action: 'initialize'
Here is my full error trace from development.log:
NoMethodError (undefined method `variant' for nil:NilClass):
app/controllers/api_controller.rb:9:in `initialize'
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.9ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (14.1ms)
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (44.1ms)
DEPRECATION WARNING: Accessing mime types via constants is deprecated. Please change `Mime::HTML` to `Mime[:html]`. (called from const_missing at /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/http/mime_type.rb:52)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.4ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.4ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
DEPRECATION WARNING: Accessing mime types via constants is deprecated. Please change `Mime::WEB_CONSOLE_V2` to `Mime[:web_console_v2]`. (called from const_missing at /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/http/mime_type.rb:52)
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (36.5ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (59.3ms)
Can you locate where the problem is here?
Thank you.
Initailize probably reserved already by Ruby. Just change something not reserved names like test_api_initialize or better api_init.
def api_inits
end
routes:
get '/init' => 'api#api_inits', as: :init

500 (Internal Server Error) when i display the data from the variable in rails

I got this error.
GET http://localhost:3000/activities/undefined 500 (Internal Server Error)
In browser console log.
When i tried to display the data by this variable in my .erb template file.
<h1><%= #activity.name %></h1>
This is my controller method.
def activities_details
#activity = Activity.find_by(id: params[:id])
#temple = Temple.find_by(id: #activity.temple_id)
render :template => 'front_pages/activity_details'
end
This page still works fine but i still confusing why this error keeps happening.
#
I found the reason it seems like rails call
get request from my link twice.
First time is the correct request,but second time is the wrong one.
this is log from the server
Started GET "/activities/24" for ::1 at 2016-02-22 14:19:13 +0800
Processing by FrontPagesController#activities_details as HTML
Parameters: {"id"=>"24"}
Activity Load (0.4ms) SELECT "activities".* FROM "activities" WHERE "activities"."id" = $1 ORDER BY "activities"."created_at" DESC LIMIT 1 [["id", 24]]
Temple Load (0.4ms) SELECT "temples".* FROM "temples" WHERE "temples"."id" = $1 ORDER BY "temples"."created_at" DESC LIMIT 1 [["id", 7]]
Rendered front_pages/activity_details.html.erb within layouts/application (0.3ms)
Rendered shared/_header.html.erb (3.8ms)
Rendered shared/_footer.html.erb (0.1ms)
Completed 200 OK in 419ms (Views: 414.3ms | ActiveRecord: 0.8ms)
Started GET "/assets/shutterstock_96393731.jpg" for ::1 at 2016-02-22 14:19:13 +0800
Started GET "/activities/undefined" for ::1 at 2016-02-22 14:19:13 +0800
Processing by FrontPagesController#activities_details as HTML
Parameters: {"id"=>"undefined"}
Activity Load (0.6ms) SELECT "activities".* FROM "activities" WHERE "activities"."id" = $1 ORDER BY "activities"."created_at" DESC LIMIT 1 [["id", 0]]
Completed 500 Internal Server Error in 10ms (ActiveRecord: 0.6ms)
NoMethodError (undefined method `temple_id' for nil:NilClass):
app/controllers/front_pages_controller.rb:36:in `activities_details'
This is my link from the previous page.
<%= link_to "Details", activity_show_path(activity.id) ,class: "btn_1"%>
My routes
get '/activities/:id' => 'front_pages#activities_details', :as => 'activity_show'
Thanks!

Rails route skips action and directly renders view

I have a namespaced controller (Interviews)
namespace :recruitment do
resources :interviews
end
I created a route (outside of the namespace)
get "myinterviews", to: "recruitment/interviews#my", as: :myinterviews
SO that /myinterviews shows the user his own interviews
Whereas rails seems to find the right view, it totally skips the controller action
def my
puts "Hi there"
exit
#recruitment_interviews=current_user.interviews
puts #recruitment_interviews.inspect
end
This is ofcourse inside Recruitments::InterviewController.
The view(recruitment/interviews#my) is rendered (it results in an error since recruitment_interviews is not found
Here's the process log:
Started GET "/myinterviews" for 127.0.0.1 at 2015-07-03 18:54:48 +0530
Processing by Recruitment::InterviewsController#my as HTML
Rendered recruitment/interviews/my.html.slim within layouts/application (3.7ms)
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
Rendered layouts/_navbar.html.slim (2.4ms)
Completed 200 OK in 199ms (Views: 197.6ms | ActiveRecord: 0.2ms)

SyntaxError in ArticlesController#create on Ruby on Rails

i'm having some trouble with this code. Actually is pretty simple, but i can't find the problem. The page was working yesterday, but now throws me an
" SyntaxError in ArticlesController#new"
and
"/home/peyu/workspace/blog01/app/controllers/articles_controller.rb:30: syntax error, unexpected keyword_end, expecting end-of-input"
I think i'm missing an "end" or something, but i can't find it. Here's the code
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
def new
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
So... any idea where's my mistake? Thank you in advance!
And this is the console output:
Started GET "/articles/new" for 127.0.0.1 at 2015-05-10 06:34:10 -0300
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
/home/peyu/workspace/blog01/app/controllers/articles_controller.rb:22: warning: else without rescue is useless
SyntaxError (/home/peyu/workspace/blog01/app/controllers/articles_controller.rb:30: syntax error, unexpected keyword_end, expecting end-of-input):
app/controllers/articles_controller.rb:30: syntax error, unexpected keyword_end, expecting end-of-input
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.8ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.2ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (10.6ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (31.6ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (0.6ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (15.3ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.6ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (34.2ms)
Started GET "/articles/new" for 127.0.0.1 at 2015-05-10 06:34:10 -0300
/home/peyu/workspace/blog01/app/controllers/articles_controller.rb:22: warning: else without rescue is useless
SyntaxError (/home/peyu/workspace/blog01/app/controllers/articles_controller.rb:30: syntax error, unexpected keyword_end, expecting end-of-input):
app/controllers/articles_controller.rb:30: syntax error, unexpected keyword_end, expecting end-of-input
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.6ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.2ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.4ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (21.1ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (1.1ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (15.7ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.3ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
Rendered /home/peyu/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (31.9ms)
You can validate your Ruby file like so
ruby -c articles_controller.rb
I tried it and it is ok. Can you copy & paste the exact file content from the articles_controller.rb please? And which Ruby version are you using?
fyi, the private keyword does not require an end, so the correct indenting should be
private
def article_params
params.require(:article).permit(:title, :text)
end
other than that, the code looks ok. Did you check the code for the template? (probably something like new.html.erb)?

Resources