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)
Related
I'm new to Ruby on Rails. Just getting my head around modelling relationships and how to build CRUD with them. I'd like advice or suggestions on how to fix this problem.
In my system, a support user (student) has private support sessions (like classes) with their support worker (tutor). Here's how the system will be used: a support worker will log in, select a service user they work with, and record a support session, which includes things like how long the session lasted for and where it took place. Eventually, the system will produce time-sheets that can be used to invoice for the tutor's work in these private support sessions.
I'm not quite sure how to interact with this at a controller level. So far I've built my model and scaffolds - now I'm customising them.
I've been using the official tutorial to help me get started at: https://guides.rubyonrails.org/v3.2.8/getting_started.html but I think I've gone beyond that now.
# Models
# Not entirely sure inverse_of is appropriate but several tutorials suggested it's worth putting in
class ServiceUser < ApplicationRecord
has_many :support_sessions, inverse_of: :service_user
has_many :support_workers, through: :support_sessions
end
class SupportWorker < ApplicationRecord
has_many :support_sessions, inverse_of: :support_worker
has_many :service_users, through: :support_sessions
class SupportSession < ApplicationRecord
belongs_to :support_worker, inverse_of: :support_sessions
belongs_to :service_user, inverse_of: :support_sessions
end
# Controller
class SupportSessionsController < ApplicationController
# POST /support_sessions
# POST /support_sessions.json
def create
# Auto-generated line:
#support_session = SupportSession.new(support_session_params)
# Get service_user_id from query string
#service_user = ServiceUser.find(params[:service_user_id])
# Lifted from RoR 'Getting started' article
#support_session = #service_user.support_sessions.create(params[:service_user])
respond_to do |format|
if #support_session.save
#format.html { redirect_to #support_session, notice: 'Support session was successfully created.' }
format.html { redirect_to service_users_url, notice: 'Support session was successfully created.' }
format.json { render :show, status: :created, location: #support_session }
else
format.html { render :new }
format.json { render json: #support_session.errors, status: :unprocessable_entity }
end
end
end
end
# Service user view/HTML form
# This is rendered as a partial on the service user's show.html.erb view
# select_support_workers() helper retrieves a list of ALL support workers in the system and the control value is the support_worker_id
<%= form_for([#service_user, #service_user.support_sessions.build]) do |f| %>
<tr>
<td>
<%= select_support_workers(f) %>
</td>
<td>
<%= f.text_field :location %>
</td>
<td>
<%= f.text_field :mode_of_delivery %>
</td>
<td>
<%= f.text_field :started_at %>
</td>
<td>
<%= f.text_field :ended_at %>
</td>
<td>
<%= f.text_field :total_breaks %>
</td>
<td>
<%= f.submit 'Save session' %>
</td>
</tr>
<% end %>
When I submit the form (which is being rendered as a partial on the service_user view show page), I seem to be ending up in the 'else' part of the if #support_session.save logic.
Error shown in my web browser is:
NameError in SupportSessions#create
Showing /Users/chris/git-local/timesheets/app/views/support_sessions/new.html.erb where line #5 raised:
undefined local variable or method `support_sessions_path' for #<#<Class:0x00007feb3412d028>:0x00007feb34163268>
Did you mean? #support_session
Extracted source (around line #5):
3 <%= render 'form', support_session: #support_session %>
4
5 <%= link_to 'Back', support_sessions_path %>
Rails.root: /Users/chris/git-local/timesheets
Application Trace | Framework Trace | Full Trace
app/views/support_sessions/new.html.erb:5:in `_app_views_support_sessions_new_html_erb__485411543567831194_70324083953220'
app/controllers/support_sessions_controller.rb:46:in `block (2 levels) in create'
app/controllers/support_sessions_controller.rb:40:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"/xd16JX3LBjA7+9DVfnHwnJziucJkaICaWg/Sd6TK2rDOrugMcZu0r5qr6SO0TfqvXzzhBqvkydMIQqXp2NtrQ==",
"support_session"=>{"support_worker_id"=>"3", "location"=>"", "mode_of_delivery"=>"", "started_at"=>"", "ended_at"=>"", "total_breaks"=>""},
"commit"=>"Save session",
"service_user_id"=>"1"}
Console output
Started GET "/service_users/" for ::1 at 2019-05-31 07:58:29 +0100
(0.5ms) SET NAMES utf8, ##SESSION.sql_mode = CONCAT(CONCAT(##sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), ##SESSION.sql_auto_is_null = 0, ##SESSION.wait_timeout = 2147483
↳ /Users/chris/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
Processing by ServiceUsersController#index as HTML
Started GET "/service_users/4" for ::1 at 2019-05-31 07:58:29 +0100
Rendering service_users/index.html.erb within layouts/application
(0.6ms) SET NAMES utf8, ##SESSION.sql_mode = CONCAT(CONCAT(##sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), ##SESSION.sql_auto_is_null = 0, ##SESSION.wait_timeout = 2147483
↳ /Users/chris/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
ServiceUser Load (0.7ms) SELECT `service_users`.* FROM `service_users`
Processing by ServiceUsersController#show as HTML
↳ app/views/service_users/index.html.erb:22
Parameters: {"id"=>"4"}
Rendered service_users/index.html.erb within layouts/application (7.6ms)
ServiceUser Load (2.4ms) SELECT `service_users`.* FROM `service_users` WHERE `service_users`.`id` = 4 LIMIT 1
↳ app/controllers/service_users_controller.rb:69
Rendering service_users/show.html.erb within layouts/application
SupportSession Load (0.9ms) SELECT `support_sessions`.* FROM `support_sessions` WHERE `support_sessions`.`service_user_id` = 4
↳ app/views/service_users/show.html.erb:18
Rendered collection of templates [0 times] (0.0ms)
SupportWorker Load (1.2ms) SELECT `support_workers`.* FROM `support_workers` ORDER BY `support_workers`.`given_name` ASC
↳ app/helpers/support_sessions_helper.rb:15
Rendered support_sessions/_form.html.erb (7.5ms)
Rendered service_users/show.html.erb within layouts/application (15.6ms)
Completed 200 OK in 94ms (Views: 91.2ms | ActiveRecord: 0.7ms)
Completed 200 OK in 115ms (Views: 105.7ms | ActiveRecord: 4.5ms)
Started GET "/service_users/" for ::1 at 2019-05-31 07:58:30 +0100
Processing by ServiceUsersController#index as HTML
Started GET "/service_users/4" for ::1 at 2019-05-31 07:58:30 +0100
Rendering service_users/index.html.erb within layouts/application
Processing by ServiceUsersController#show as HTML
ServiceUser Load (1.5ms) SELECT `service_users`.* FROM `service_users`
Parameters: {"id"=>"4"}
↳ app/views/service_users/index.html.erb:22
Rendered service_users/index.html.erb within layouts/application (5.9ms)
ServiceUser Load (2.2ms) SELECT `service_users`.* FROM `service_users` WHERE `service_users`.`id` = 4 LIMIT 1
↳ app/controllers/service_users_controller.rb:69
Rendering service_users/show.html.erb within layouts/application
SupportSession Load (0.8ms) SELECT `support_sessions`.* FROM `support_sessions` WHERE `support_sessions`.`service_user_id` = 4
↳ app/views/service_users/show.html.erb:18
Rendered collection of templates [0 times] (0.0ms)
SupportWorker Load (1.3ms) SELECT `support_workers`.* FROM `support_workers` ORDER BY `support_workers`.`given_name` ASC
↳ app/helpers/support_sessions_helper.rb:15
Rendered support_sessions/_form.html.erb (5.5ms)
Rendered service_users/show.html.erb within layouts/application (12.1ms)
Completed 200 OK in 126ms (Views: 122.0ms | ActiveRecord: 1.5ms)
Completed 200 OK in 108ms (Views: 100.0ms | ActiveRecord: 4.3ms)
Started GET "/service_users/4" for ::1 at 2019-05-31 07:58:34 +0100
Started GET "/service_users/4" for ::1 at 2019-05-31 07:58:34 +0100
Processing by ServiceUsersController#show as HTML
Parameters: {"id"=>"4"}
Processing by ServiceUsersController#show as HTML
Parameters: {"id"=>"4"}
ServiceUser Load (1.3ms) SELECT `service_users`.* FROM `service_users` WHERE `service_users`.`id` = 4 LIMIT 1
↳ app/controllers/service_users_controller.rb:69
ServiceUser Load (1.5ms) SELECT `service_users`.* FROM `service_users` WHERE `service_users`.`id` = 4 LIMIT 1
↳ app/controllers/service_users_controller.rb:69
Rendering service_users/show.html.erb within layouts/application
Rendering service_users/show.html.erb within layouts/application
SupportSession Load (1.5ms) SELECT `support_sessions`.* FROM `support_sessions` WHERE `support_sessions`.`service_user_id` = 4
↳ app/views/service_users/show.html.erb:18
SupportSession Load (0.7ms) SELECT `support_sessions`.* FROM `support_sessions` WHERE `support_sessions`.`service_user_id` = 4
Rendered collection of templates [0 times] (0.0ms)
↳ app/views/service_users/show.html.erb:18
Rendered collection of templates [0 times] (0.0ms)
SupportWorker Load (1.4ms) SELECT `support_workers`.* FROM `support_workers` ORDER BY `support_workers`.`given_name` ASC
↳ app/helpers/support_sessions_helper.rb:15
SupportWorker Load (0.7ms) SELECT `support_workers`.* FROM `support_workers` ORDER BY `support_workers`.`given_name` ASC
↳ app/helpers/support_sessions_helper.rb:15
Rendered support_sessions/_form.html.erb (5.7ms)
Rendered service_users/show.html.erb within layouts/application (11.7ms)
Rendered support_sessions/_form.html.erb (5.8ms)
Rendered service_users/show.html.erb within layouts/application (12.6ms)
Completed 200 OK in 80ms (Views: 71.4ms | ActiveRecord: 4.2ms)
Completed 200 OK in 82ms (Views: 74.2ms | ActiveRecord: 2.9ms)
Started POST "/service_users/4/support_sessions" for ::1 at 2019-05-31 07:58:35 +0100
Processing by SupportSessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"KZGBtom/9t6vYu5FyPRq3xA3Xmb6hkzG8LEA7c/qmOkUZGmtslduK+l2as5chNz7w4ikC+5fHRYh3Ak2IZwIvA==", "support_session"=>{"support_worker_id"=>"3", "location"=>"", "mode_of_delivery"=>"", "started_at"=>"", "ended_at"=>"", "total_breaks"=>""}, "commit"=>"Save session", "service_user_id"=>"4"}
ServiceUser Load (0.5ms) SELECT `service_users`.* FROM `service_users` WHERE `service_users`.`id` = 4 LIMIT 1
↳ app/controllers/support_sessions_controller.rb:33
(0.3ms) BEGIN
↳ app/controllers/support_sessions_controller.rb:36
(0.3ms) ROLLBACK
↳ app/controllers/support_sessions_controller.rb:36
(0.2ms) BEGIN
↳ app/controllers/support_sessions_controller.rb:41
(0.2ms) ROLLBACK
↳ app/controllers/support_sessions_controller.rb:41
Rendering support_sessions/new.html.erb within layouts/application
SupportWorker Load (0.5ms) SELECT `support_workers`.* FROM `support_workers` ORDER BY `support_workers`.`given_name` ASC
↳ app/helpers/support_sessions_helper.rb:15
Rendered support_sessions/_form.html.erb (3.9ms)
Rendered support_sessions/new.html.erb within layouts/application (324.5ms)
Completed 500 Internal Server Error in 336ms (ActiveRecord: 2.0ms)
ActionView::Template::Error (undefined local variable or method `support_sessions_path' for #<#<Class:0x00007ffb0925c658>:0x00007ffb083fa100>
Did you mean? #support_session):
2:
3: <%= render 'form', support_session: #support_session %>
4:
5: <%= link_to 'Back', support_sessions_path %>
app/views/support_sessions/new.html.erb:5:in `_app_views_support_sessions_new_html_erb___2840892112036112501_70358075950960'
app/controllers/support_sessions_controller.rb:46:in `block (2 levels) in create'
app/controllers/support_sessions_controller.rb:40:in `create'
# Crebs:timesheets chris$ rake routes -c support_session
Prefix Verb URI Pattern Controller#Action
service_user_support_sessions GET /service_users/:service_user_id/support_sessions(.:format) support_sessions#index
POST /service_users/:service_user_id/support_sessions(.:format) support_sessions#create
new_service_user_support_session GET /service_users/:service_user_id/support_sessions/new(.:format) support_sessions#new
edit_service_user_support_session GET /service_users/:service_user_id/support_sessions/:id/edit(.:format) support_sessions#edit
service_user_support_session GET /service_users/:service_user_id/support_sessions/:id(.:format) support_sessions#show
PATCH /service_users/:service_user_id/support_sessions/:id(.:format) support_sessions#update
PUT /service_users/:service_user_id/support_sessions/:id(.:format) support_sessions#update
DELETE /service_users/:service_user_id/support_sessions/:id(.:format) support_sessions#destroy
You have nested routes for support_sessions resource, so instead of
<%= link_to 'Back', support_sessions_path %>
you should use code like this to build correct path:
<%= link_to 'Back', [#service_user, :support_sessions] %>
or maybe like this one:
<%= link_to 'Back', service_user_support_sessions_path(#service_user) %>
Here's the code that got things working. Doesn't seem to work very well with validation but the data submits.
def create
#render plain: params[:support_session].inspect
# Auto-generated line:
##support_session = SupportSession.new(support_session_params)
# Get service_user_id from query string
# This is so each new record submitted can get the id of the service_user in question
#service_user = ServiceUser.find(params[:service_user_id])
# Copied from RoR 'Getting started' article: https://guides.rubyonrails.org/getting_started.html
# This calls the create method on #service_user.support_session (rather than #service_user alone)
# so the link is made between the service_user and the support_session
# Calls private method below to safeguard input parameters (only permitted ones)
#support_session = #service_user.support_sessions.create(support_session_params)
#redirect_to service_user_path(#service_user)
respond_to do |format|
if #support_session.save
#if #support_session
#if #service_user.support_sessions.create(support_session_params)
#if #service_user.support_sessions.new(support_session_params)
#format.html { redirect_to #support_session, notice: 'Support session was successfully created.' }
format.html { redirect_to #service_user, notice: 'Support session was successfully created.' }
format.json { render :show, status: :created, location: #support_session }
else
#format.html { render :show }
format.html { redirect_to service_user_path(#service_user), notice: 'There was an error!' }
format.json { render json: #support_session.errors, status: :unprocessable_entity }
end
end
end
I have this defined in my application_mailer.rb
def request_tutor(contact_name, contact_hp, contact_email, postal_code, contact_level, contact_subject, contact_lessons, contact_hours, contact_others)
#contact_name = contact_name
#contact_hp = contact_hp
#contact_email = contact_email
#contact_postal = postal_code
#contact_level = contact_level
#contact_subject = contact_subject
#contact_lesson = contact_lessons
#contact_hours = contact_hours
#contact_others = contact_others
mail(to:'example#example.com', subject: 'Tutor Request')
end
and in the respective controller where the view is supposed to be rendered
def request_tutor
contact_name = params[:request_tutor][:contact_name]
contact_email = params[:request_tutor][:contact_email]
contact_hp = params[:request_tutor][:contact_hp]
contact_postal = params[:request_tutor][:postal_code]
contact_level = params[:request_tutor][:contact_level]
contact_subject = params[:request_tutor][:contact_subject]
contact_lesson = params[:request_tutor][:contact_lessons]
contact_hours = params[:request_tutor][:contact_hours]
contact_others = params[:request_tutor][:contact_others]
ApplicationMailer.request_tutor(contact_name, contact_hp, contact_email, postal_code, contact_level, contact_subject, contact_lessons, contact_hours, contact_others).deliver
flash[:success] = "We have received your request and will be in touch with you shortly!"
redirect_to root_path
end
I have a similar method setup in action mailer too and in the respective controller i'm doing name = params[:checkout][:your_name] and it doesn't return the error undefined method '[]' for nil:NilClass. What am i missing?
Errors from development.log
Started GET "/" for 116.87.14.150 at 2016-10-10 23:02:37 +0000
Cannot render console from 116.87.14.150! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
[1m[36mActiveRecord::SchemaMigration Load (0.5ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
Processing by WelcomeController#index as HTML
Rendered welcome/index.html.erb within layouts/application (390.7ms)
Rendered layouts/_nav.html.erb (5.9ms)
Rendered layouts/_messages.html.erb (0.6ms)
Rendered layouts/_footer.html.erb (0.8ms)
Completed 200 OK in 641ms (Views: 608.3ms | ActiveRecord: 0.0ms)
Started GET "/welcome/request" for 116.87.14.150 at 2016-10-10 23:02:43 +0000
Cannot render console from 116.87.14.150! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by WelcomeController#request_tutor as HTML
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/welcome_controller.rb:16:in `request_tutor'
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (6.4ms)
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.8ms)
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.5ms)
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (110.0ms)
ok, so you are using the same action to both set up the form, and also to create the email. This is where the problem comes in... because when you first click on the form - it will run the request_tutor action, but there is no params[:request_tutor] yet (because you haven't submitted the form yet)... so you need to check for that to exist before trying to create one.
eg this is possible:
def request_tutor
if params[:request_tutor].present?
contact_name = params[:request_tutor][:contact_name]
# ... lines skipped for brevity
# you still need the ones in your original action
ApplicationMailer.request_tutor(contact_name, contact_hp, contact_email, postal_code, contact_level, contact_subject, contact_lessons, contact_hours, contact_others).deliver
flash[:success] = "We have received your request and will be in touch with you shortly!"
redirect_to root_path
end
# If the code gets to here, there were no params, and the page
# will just render the template named the same as the action
end
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
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!
I'm trying to use the split class to split up an array. I keep getting this error:
MethodError (undefined method `split' for nil:NilClass):
This is what I'm trying to do:
def create
#listing.landlord = current_landlord
if #listing.save
photo_id_array = params[:images].split(/,/)
photo_id_array.each do |image|
#image = Photo.find_by_id("photo.id")
#image.listing_id = #listing.id
end
render :show, :status => 200
else
render :status => 403, :json => {:errors => #listing.errors}
end
end
I'm running Rails 3.2.9. Any ideas into what may be causing this?
Edit:
Here is the full post:
Started POST "/listings" for 127.0.0.1 at 2012-12-12 01:04:52 -0800
Processing by ListingsController#create as application/json; charset=utf-8
Parameters: {"listing"=>{"street_address"=>"123 main st", "city"=>"los angeles", "state"=>"ca", "availability"=>"12 Dec 2012", "price"=>"2000", "period"=>"2", "category"=>"1", "cats"=>"false", "dogs"=>"false", "square_footage"=>"0", "bedrooms"=>"3", "bathrooms"=>"2", "short_description"=>"tree-lined street", "long_description"=>"big yard", "images"=>["70621", "70622", "70620"]}, "auth_token"=>"6489Cn7KeTmejSxaWsws"}
WARNING: Can't verify CSRF token authenticity
User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."authentication_token" = '6489Cn7KeTmejSxaWsws' LIMIT 1
Landlord Load (0.6ms) SELECT "landlords".* FROM "landlords" WHERE "landlords"."authentication_token" = '6489Cn7KeTmejSxaWsws' LIMIT 1
Role Load (0.5ms) SELECT "roles".* FROM "roles" WHERE "roles"."name" = 'admin' LIMIT 1
Role Exists (0.7ms) SELECT 1 AS one FROM "roles" INNER JOIN "landlords_roles" ON "roles"."id" = "landlords_roles"."role_id" WHERE "landlords_roles"."landlord_id" = 5867 AND "roles"."id" = 1 LIMIT 1
Completed 500 Internal Server Error in 275ms
NoMethodError (undefined method `split' for nil:NilClass):
app/controllers/listings_controller.rb:8:in `create'
Rendered /Users/rigelstpierre/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.9ms)
Rendered /Users/rigelstpierre/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.1ms)
Rendered /Users/rigelstpierre/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.9ms)
Use:
params[:listing][:images]
And it's already an Array, no need to split. You can directly do: params[:listing][:images].each.
params[:images] is nil, you're after params[:listing][:images] I assume.
Your params are structured like: "listing"=>{"images"=>["70621", "70622", "70620"]}.
Plus, you needn't split it, it's an array. Access each item of the array with: params[:listing][:images].each do |i|