No route matches - missing required key - ruby-on-rails

trying to create a videogame review website for practice. ran into this problem of missing key / no route matches.
below are my Games, and Reviews contrller. Review is nested inside Games.
Many thanks in advance :)
edit:
the error is:
No route matches {:action=>"update", :controller=>"reviews", :game_id=>3, :id=>nil} missing required keys: [:id]
full error code below:
No route matches {:action=>"update", :controller=>"reviews", :game_id=>3, :id=>nil} missing required keys: [:id]
actionpack (4.2.1) lib/action_dispatch/journey/formatter.rb:46:in `generate'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:727:in `generate'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:758:in `generate'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:801:in `url_for'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:280:in `call'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:223:in `call'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:345:in `block (2 levels) in define_url_helper'
app/views/games/show.html.erb:22:in `block in _app_views_games_show_html_erb___707335474_75793908'
c:in `each'
c:in `each'
app/views/games/show.html.erb:20:in `_app_views_games_show_html_erb___707335474_75793908'
actionview (4.2.1) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.1) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.1) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.1) lib/action_view/template.rb:143:in `render'
actionview (4.2.1) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.1) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.1) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.1) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.1) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.1) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.1) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.1) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.1) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.1) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.1) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.1) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.1) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.1) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.1) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.1) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.1) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.2.1) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.1) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (4.2.1) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.1) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.1) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.1) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.1) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.1) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.1) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.1) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:74:in `call'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:819:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in `call'
activerecord (4.2.1) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.1) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_call_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.1.3) lib/web_console/middleware.rb:37:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.1) lib/rails/engine.rb:518:in `call'
railties (4.2.1) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
** also having problems with the Show view. i think its the delete action
*** also can you take a look at my form partial for the review submission? i'm basically trying to render have the text area rendered in the Show view, then once submited, appear below the text area.
I can add reviews and save them, but once submitted, I get an error. i go to rails console, and see that the reviews are saved -- but just curious if my logic is any good.
Routes.rb
resources :games do
resources :reviews, except: [:show, :index]
end
Games_controller.rb
class GamesController < ApplicationController
before_action :set_game, only: [:show, :edit, :update, :destroy]
def index
#games = Game.all
end
def show
#reviews = Review.where(game_id: #game.id)
end
def create
#game = Game.new(game_params)
#game.save
redirect_to #game
end
def new
#game = Game.new
set_platforms
set_genres
end
def edit
#platforms = Platform.order(:system)
set_platforms
set_genres
end
def update
#game.update(game_params)
redirect_to #game
end
def destroy
#game.destroy
redirect_to action: :index
end
private
def game_params
params.require(:game).permit(:title, :image, :release_date, :genre_id, :platform_id)
end
def set_game
#game = Game.find(params[:id])
end
def set_platforms
#platforms = Platform.order(:system)
end
def set_genres
#genres = Genre.order(:category)
end
end
Reviews_controller.rb
class ReviewsController < ApplicationController
before_action :authenticate_user!, :set_review, only: [:show, :update, :edit, :destroy]
before_action :set_game
def index
end
def show
end
def new
#review = Review.new
end
def create
#review = Review.new(review_params)
#review.user_id = current_user.id
#review.game_id = #game.id
#review.save
end
def edit
#review.update(review.params)
end
def destroy
#review.destroy
redirect_to games_path
end
private
def review_params
params.require(:review).permit(:comment)
end
def set_review
#review = Review.find(params[:id])
end
def set_game
#game = Game.find(params[:game_id])
end
end
show.html.erb
<p><%= link_to "<< Home", games_path %></p>
<span><%= link_to "Edit", edit_game_path %></span>
<span><%= link_to "Delete", game_path(#game), method: :delete %></span>
<p><%= link_to "Add Steelbook", new_steelbook_path %></p>
<div class="game_summary">
<h2><%= #game.title %></h2>
<%= image_tag #game.image %>
<p>Release Date: <%= #game.release_date %> </p>
<p>Genre: <%= #game.genre_id %> </p>
<p>Platforms: <%= #game.platform_id %></p>
</div>
<div class="game_review submit">
<%= render "review" %>
</div>
<% #reviews.each do |review| %>
<p><%= review.comment %></p>
<p><%= link_to "delete comment", game_review_path(review.game_id), method: :delete %></p>
<p><%= review.game_id %></p>
<% end %>
Form partial
<%= form_for [#game, #reviews.new] do |r| %>
<h3>Review this game</h3>
<p>
<%= r.text_area :comment %>
</p>
<p>
<%= r.hidden_field :game_id, value: #game.id %>
<p>
<%= r.submit %>
<% end %>

I think the error is happening here: edit_game_path
Edit path requires a model id to work, so you need to pass it:
edit_game_path(#game)

You have to change this line in your show.html.erb
<%= link_to "delete comment", game_review_path(review.game_id), method: :delete %>
to
<%= link_to "delete comment", game_review_path(review, review.game_id), method: :delete %>

Related

Devise allow admins to delete users

This seems to be a common issue, I have tried a couple solutions and am getting an unexpected issue. The goal is to create a page that admins can edit the permissions on and delete user accounts. routes:
devise_for :users, controllers: { registrations: 'users/registrations'}
get '/login', to: 'sessions#new'
match '/users', to: 'users#index', via: 'get'
match '/users/makeAdmin', to: 'users#makeAdmin', via: 'get', :as => :users_makeAdmin
match '/users/revokeAdmin', to: 'users#revokeAdmin', via: 'get'
match 'users/:id' => 'users#destroy', :via => :delete, :as => :admin_destroy_user
match 'users/:id' => 'users#show', via: 'get', as: :user
resources :users
users_controller.rb
class UsersController < ApplicationController
def index
end
def makeAdmin
#user = User.find(params[:id])
#user.update(admin: true)
redirect_to users_index_path
end
def revokeAdmin
#user = User.find(params[:id])
#user.update(admin: false)
redirect_to users_index_path
end
def show
#user = User.find(params[:id])
end
def destroy
#user = User.find(params[:id])
#user.destroy
if #user.destroy
redirect_to users_index_path, notice: "User deleted."
end
end
end
users/index.html.erb
<style>
.button_to {
display: inline;
}
.evenrow {
background-color: #eee;
}
</style>
<script type="text/javascript">
function alert() {
alert("The paragraph was clicked.");
}
</script>
<h1>All Users</h1>
<ul class="list-group" style="padding: 0em 10% 0em 10%;">
<li class="list-group-item" style="background-color: #428bca; border-color: #357ebd;; color: #fff;">
<div class="row">
<div class="col-md-3">
<b>User Email</b>
</div>
<div class="col-md-3">
<b>User Name</b>
</div>
<div class="col-md-1">
<b>Admin</b>
</div>
<div class="col-md-5">
<b>Controls</b>
</div>
</div>
</li>
<%#users = User.all%>
<% #users.each do |user| %>
<li class="list-group-item">
<div class="row">
<div class="col-md-3">
<%= user.email %>
</div>
<div class="col-md-3">
<%= user.name %>
</div>
<div class="col-md-1">
<%= user.admin %>
</div>
<div class="col-md-5">
<% if user.admin %><%= link_to 'Revoke Admin', users_revokeAdmin_path(id: user.id), class: "btn btn-primary btn-md" %><% else %><%= link_to 'Make Admin', users_makeAdmin_path(id: user.id), class: "btn btn-primary btn-md" %><% end %>
| <%= link_to "Destroy", admin_destroy_user_path(user), method: :delete, data: { confirm: "You sure?" }, class: "btn btn-primary btn-md" %>
</div>
</div>
</li>
<% end %>
</ul>
When I hit the destroy button for a user I get an error that says
Template is missing
Missing template users/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/student/SLP/rca/app/views" * "/home/student/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.2.0/app/views"
I haven't seen this mentioned in any other questions dealing with this, any help is appreciated.
Edit: here is the stack since someone asked.
actionview (4.2.6) lib/action_view/path_set.rb:46:in `find'
actionview (4.2.6) lib/action_view/lookup_context.rb:121:in `find'
actionview (4.2.6) lib/action_view/renderer/abstract_renderer.rb:18:in `find_template'
actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:40:in `determine_template'
actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:8:in `render'
actionview (4.2.6) lib/action_view/renderer/renderer.rb:46:in `render_template'
actionview (4.2.6) lib/action_view/renderer/renderer.rb:27:in `render'
actionview (4.2.6) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.6) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.6) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.6) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.6) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.6) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.6) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.6) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/home/student/.rbenv/versions/2.3.1/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
activesupport (4.2.6) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.6) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.6) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.6) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.6) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.6) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.6) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.6) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:817:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.6) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.6) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.6) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.6) lib/rails/engine.rb:518:in `call'
railties (4.2.6) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/home/student/.rbenv/versions/2.3.1/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
/home/student/.rbenv/versions/2.3.1/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
/home/student/.rbenv/versions/2.3.1/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'
The reason you're hitting a no template error is because if #user.destroy is false since you've already destroyed the user in the line above. Therefore redirect_to users_index_path never happens and it's looking for a destroy template.
Change your code to:
def destroy
#user = User.find(params[:id])
if #user.destroy
redirect_to users_index_path, notice: "User deleted."
else
redirect_to users_index_path, flash: { error: "User could not be deleted." }
end
end

Form_for Error: undefined method `id' for nil:NilClass

I'm building an app that features workouts models, each workout has_many exercises, and each exercise has_many reports.
My routes are as follows:
workouts GET /workouts(.:format) workouts#index
POST /workouts(.:format) workouts#create
new_workout GET /workouts/new(.:format) workouts#new
edit_workout GET /workouts/:id/edit(.:format) workouts#edit
workout GET /workouts/:id(.:format) workouts#show
PATCH /workouts/:id(.:format) workouts#update
PUT /workouts/:id(.:format) workouts#update
DELETE /workouts/:id(.:format) workouts#destroy
exercises GET /exercises(.:format) exercises#index
POST /exercises(.:format) exercises#create
new_exercise GET /exercises/new(.:format) exercises#new
edit_exercise GET /exercises/:id/edit(.:format) exercises#edit
exercise GET /exercises/:id(.:format) exercises#show
PATCH /exercises/:id(.:format) exercises#update
PUT /exercises/:id(.:format) exercises#update
DELETE /exercises/:id(.:format) exercises#destroy
reports POST /reports(.:format) reports#create
report DELETE /reports/:id(.:format) reports#destroy
I toyed with nesting the routes, but it caused more problems than it solved, so they remain like this:
resources :workouts
resources :exercises
resources :reports, only: [:create, :destroy]
On my workouts#show page I have a link to add exercises to that workout:
<%= link_to 'Add/Edit Exercises', exercises_path(#workout) %>
When I click the link, I get the following error:
undefined method `id' for nil:NilClass
When I click the link, it SHOULD go to my exercises#index page:
<h1>Current Exercises:</h1>
<% #exercises.each do |exercise| %>
<p><%= exercise.name %> (<%= link_to "Delete #{exercise.name}", exercise_path(exercise), method: :delete, data: { confirm: 'Are you sure?' } %>)</p>
<% end %>
<h1>Add New Exercises:</h1>
<%= render 'exercises/form' %>
It is on the exercises/_form.html.erb that the error is being called, on the indicated line:
<div class="row">
<div class="col-xs-10 col-xs-push-1">
<%= form_for #exercise,
:url => { :controller => "exercises",
:action => :create,
:workout_id => #workout.id } do |f| %> <!-- error called on this line -->
<div class="form-group">
<%= f.label :name, class: 'sr-only' %>
<%= f.text_field :name, class: 'form-control', placeholder: "Enter exercise name" %>
</div>
<div class="form-group col-xs-4">
<p><%= f.label :needs_seconds, class: 'sr-only' %>
<%= f.check_box :needs_seconds, class: 'check_box' %> Report seconds?</p>
</div>
<div class="form-group col-xs-4">
<p><%= f.label :needs_reps, class: 'sr-only' %>
<%= f.check_box :needs_reps, class: 'check_box' %> Report reps?</p>
</div>
<div class="form-group col-xs-4">
<p><%= f.label :needs_weight, class: 'sr-only' %>
<%= f.check_box :needs_weight, class: 'check_box' %> Report weight?</p>
</div>
<div class="text-center"><%= f.submit "Create Exercise", class: 'btn btn-primary' %></div>
<% end %>
</div>
</div>
Any insight as to what is causing this error? Here's my workouts controller:
class WorkoutsController < ApplicationController
def index
#workouts = Workout.all
end
def show
#workout = Workout.friendly.find(params[:id])
#exercise = Exercise.new
#report = Report.new
end
def new
#workout = Workout.new
#workout.user_id = current_user
end
def create
#workout = Workout.new(workout_params)
#workout.user = current_user
if #workout.save
flash[:notice] = "Workout was saved successfully."
redirect_to #workout
else
flash.now[:alert] = "Error creating workout. Please try again."
render :new
end
end
def edit
#workout = Workout.friendly.find(params[:id])
#workout.user_id = current_user
end
def update
#workout = Workout.friendly.find(params[:id])
#workout.name = params[:workout][:name]
#workout.workout_type = params[:workout][:workout_type]
#workout.teaser = params[:workout][:teaser]
#workout.description = params[:workout][:description]
#workout.video = params[:workout][:video]
#workout.difficulty = params[:workout][:difficulty]
#workout.trainer = params[:workout][:trainer]
#workout.user_id = params[:workout][:user_id]
if #workout.save
flash[:notice] = "Workout was updated successfully."
redirect_to #workout
else
flash.now[:alert] = "Error saving workout. Please try again."
render :edit
end
end
def destroy
#workout = Workout.friendly.find(params[:id])
if #workout.destroy
flash[:notice] = "\"#{#workout.name}\" was deleted successfully."
redirect_to action: :index
else
flash.now[:alert] = "There was an error deleting the workout."
render :show
end
end
private
def workout_params
params.require(:workout).permit(:name, :workout_type, :teaser, :description, :video, :difficulty, :trainer, :user_id)
end
end
And here's the exercises controller:
class ExercisesController < ApplicationController
before_action :authenticate_user!
def index
#exercises = Exercise.all
end
def new
#exercise = Exercise.new
end
def create
#workout = Workout.friendly.find(params[:workout_id])
exercise = #workout.exercises.new(exercise_params)
exercise.user = current_user
if exercise.save
flash[:notice] = "Results saved successfully."
redirect_to [#workout]
else
flash[:alert] = "Results failed to save."
redirect_to [#workout]
end
end
def destroy
#workout = Workout.friendly.find(params[:workout_id])
exercise = #workout.exercises.find(params[:id])
if exercise.destroy
flash[:notice] = "Exercise was deleted successfully."
redirect_to [#workout]
else
flash[:alert] = "Exercise couldn't be deleted. Try again."
redirect_to [#workout]
end
end
private
def exercise_params
params.require(:exercise).permit(:name, :needs_seconds, :needs_weight, :needs_reps)
end
def authorize_user
exercise = Exercise.find(params[:id])
unless current_user == current_user.admin?
flash[:alert] = "You do not have permission to create or delete an exercise."
redirect_to [exercise.workout]
end
end
end
And here's the server log:
Started GET "/exercises.abs-0002" for ::1 at 2016-06-01 13:10:33 -0700
Processing by ExercisesController#index as
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Workout Load (0.1ms) SELECT "workouts".* FROM "workouts" WHERE "workouts"."id" IS NULL LIMIT 1
Exercise Load (0.1ms) SELECT "exercises".* FROM "exercises"
Rendered exercises/_form.html.erb (9.6ms)
Rendered exercises/index.html.erb within layouts/application (18.4ms)
Completed 500 Internal Server Error in 33ms (ActiveRecord: 0.3ms)
NoMethodError - undefined method `id' for nil:NilClass:
app/views/exercises/_form.html.erb:4:in `block in _app_views_exercises__form_html_erb___1550785662008061170_70245396037240'
actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'
actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionview (4.2.5) lib/action_view/helpers/form_helper.rb:444:in `form_for'
app/views/exercises/_form.html.erb:3:in `_app_views_exercises__form_html_erb___1550785662008061170_70245396037240'
actionview (4.2.5) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.5) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.5) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.5) lib/action_view/template.rb:143:in `render'
actionview (4.2.5) lib/action_view/renderer/partial_renderer.rb:339:in `render_partial'
actionview (4.2.5) lib/action_view/renderer/partial_renderer.rb:310:in `block in render'
actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.5) lib/action_view/renderer/partial_renderer.rb:309:in `render'
actionview (4.2.5) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (4.2.5) lib/action_view/helpers/rendering_helper.rb:35:in `render'
app/views/exercises/index.html.erb:7:in `_app_views_exercises_index_html_erb___2271140629366515446_70245347113980'
actionview (4.2.5) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.5) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.5) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.5) lib/action_view/template.rb:143:in `render'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.5) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.5) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.5) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.5) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.5) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.5) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.5) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.5) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/elizabethbayardelle/.rbenv/versions/2.2.4/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.5) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:43:in `render'
meta-tags (2.1.0) lib/meta_tags/controller_helper.rb:26:in `render_with_meta_tags'
remotipart (1.2.1) lib/remotipart/render_overrides.rb:14:in `render_with_remotipart'
actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.5) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.5) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.5) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.5) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.5) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.5) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.5) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.5) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:45:in `serve'
actionpack (4.2.5) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:817:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
remotipart (1.2.1) lib/remotipart/middleware.rb:27:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.5) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.5) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.5) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.5) lib/rails/engine.rb:518:in `call'
railties (4.2.5) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
You link_to in workouts#show can be like below:
<%= link_to 'Add/Edit Exercises', exercises_path(workout_id: #workout.id) %>
exercises_controller.rb
def index
#workout = Workout.find_by_id(params[:workout_id])
#exercise = Exercise.new
#exercises = Exercise.all
end
You can pass the workout_id in hidden field of exercises/_form.html.erb as follows:
<%= form_for #exercise do |f| %>
<%= f.hidden_field :workout_id, #workout.id %>
<% end %>
And, include workout_id to your strong param as follows:
def exercise_params
params.require(:exercise).permit(:name, :needs_seconds, :needs_weight, :needs_reps, :workout_id)
end

Am I creating an undefined nil method?

Everything is working properly except for when I want to check if a post belongs to a user_id.
What I am basically trying to achieve is to hide the delete post button so that other users can't delete it.
I am still new to ruby on rails when I did my first project I added an if statement that checks whether it belongs to the user_id or not. Now I am trying to do the same but I keep on getting the NoMethodError
Profile code:
<div class="container">
<div class="row">
<% if user_signed_in? %>
<% if #post.user_id == current_user.id %>
<div class="col-md-5">
<%= render '/components/post_form' %>
</div>
<%end%>
<%end%>
</div>
</div>
Post controller:
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
#new post
def new
#post = Post.new
end
#create post
def create
#post = Post.new(posts_params)
#post.user_id = current_user.id
respond_to do |f|
if (#post.save)
f.html {redirect_to :back}
else
f.html {redirect_to :back, notice: "An error happened while submitting your post. Please try again."}
end
end
end
#show post
def show
if (User.find_by_username(params[:id]))
#post = params[:id]
end
end
#destroy post
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to :back
end
#private
private
def posts_params
params.require(:post).permit(:user_id, :content)
end
def find_post
#post = Post.find(params[:id])
end
end
Pages controller:
class PagesController < ApplicationController
def index
end
def home
#posts = Post.all
end
def profile
if (User.find_by_username(params[:id]))
#username = params[:id]
else
redirect_to root_path, :notice => "user not found"
end
#posts = Post.all.where( "user_id = ?", User.find_by_username(params[:id]) )
#newPost = Post.new
end
def explore
#posts = Post.all
end
end
Post schema:
create_table "posts", force: true do |t|
t.text "content"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Update
Error I am getting:
NoMethodError in Pages#profile
Full trace:
app/views/pages/profile.html.erb:4:in `_app_views_pages_profile_html_erb___203732305_53267256'
actionview (4.1.8) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.8) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:143:in `render'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionview (4.1.8) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.1.8) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.1.8) lib/action_view/rendering.rb:99:in `_render_template'
actionpack (4.1.8) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.1.8) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.1.8) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
actionpack (4.1.8) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.1.8) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.1.8) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (1.5.5) lib/rack/etag.rb:23:in `call'
rack (1.5.5) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.5) lib/rack/head.rb:11:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.5) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
rack (1.5.5) lib/rack/content_length.rb:14:in `call'
rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Update
Form partial:
<%= simple_form_for #newPost do |f| %>
<%= f.label :content %>
<%= f.text_field :content, autofocus: true %>
<%= f.button :submit %>
<% end %>
replace this:
<div class="container">
<div class="row">
<% if user_signed_in? %>
<% if #post.user_id == current_user.id %>
<div class="col-md-5">
<%= render '/components/post_form' %>
</div>
<%end%>
<%end%>
</div>
</div>
with this:
<div class="container">
<% if user_signed_in? && #newPost.present? && #newPost.user_id == current_user.id %>
<div class="row">
<div class="col-md-5">
<%= render '/components/post_form' %>
</div>
</div>
<%end%>
</div>
notice in your controller you defined #newPost, but your view is trying to use #post which doesnt exist.
Also you may want to pass #newPost into the partial, like so:
<%= render '/components/post_form', object: #newPost %>

Acts as follower installaion

I am trying to use acts_as_follower to work but I am surely doing something wrong in the routes.
Added the gem:
gem 'acts_as_follower
In the User model:
acts_as_follower
acts_as_followable
In the Post model:
acts_as_followable
In my page controller:
class PageController < ApplicationController
def index
end
def profile
#posts = Post.all.order("created_at desc").where( "user_id = ?", User.find_by_id(params[:id]) )
#post = Post.new
end
def feed
#posts = Post.all.order("RANDOM()")
end
def home
end
def follow
#user = User.find(params[:id])
current_user.follow(#user)
end
def unfollow
#user = User.find(params[:id])
current_user.stop_following(#user)
end
def block
#user = User.find(params[:id])
#post.block(#user)
end
end
In my routes:
Rails.application.routes.draw do
resources :posts
devise_for :users
root 'page#index'
get '/users/:id' => 'page#profile'
get 'page/feed'
get 'page/home'
resources :users do
get :follow
get :unfollow
end
end
The thing is, I always get an exception error with user_id set to nil even though the Users table (devise) doesn't have a user_id column?
Why is it searching for a user_id when I defined #user in my controller?
Is the issue inside the routes.rb?
Update
Full Post model:
class Post < ActiveRecord::Base
belongs_to :user
mount_uploader :avatar, AvatarUploader
acts_as_followable
end
Full User Model:
class User < ActiveRecord::Base
has_many :posts
acts_as_follower
acts_as_followable
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Full trace log:
app/views/page/profile.html.erb:3:in `_app_views_page_profile_html_erb__881455127_39547752'
actionview (4.1.8) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.8) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:143:in `render'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionview (4.1.8) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.1.8) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.1.8) lib/action_view/rendering.rb:99:in `_render_template'
actionpack (4.1.8) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.1.8) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.1.8) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
actionpack (4.1.8) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.1.8) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.1.8) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (1.5.5) lib/rack/etag.rb:23:in `call'
rack (1.5.5) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.5) lib/rack/head.rb:11:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.5) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
rack (1.5.5) lib/rack/content_length.rb:14:in `call'
rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Profile view:
<%= link_to "Back", root_path %>
<%= link_to "Follow", follow_user_path(#user) %>
<br>
<br>
<%= render 'posts/form' %>
<br>
<br>
<% #posts.each do |post| %>
<p><%= post.description %>, <%= time_ago_in_words(post.created_at) %> ago</p>
<%= image_tag post.avatar, :class => "tumbnial" %>
<br>
<% if user_signed_in? %>
<% if post.user_id == current_user.id %>
<%= link_to "Delete post", post_path(post), :method => :delete %>
<% end %>
<% end %>
<% end %>
#Raymond looks like you missed initialization of #user instance in your profile action:
def profile
#user = User.find(params[:id])
#posts = Post.all.order("created_at desc").where(user_id: #user.id)
#post = Post.new
end
Also note that when you use find_by_id(...) to find user you'll get nil, if user will not be found, so might be incorrect behavior in next code. Better to use find(...) which will raise an exception and you can handle it somehow, for example by redirecting to some page with flash message about it or you can do the same using find_by_id but make sure user was found.
And probably better extract this functionality to some before_action, e.g.:
class PageController < ApplicationController
before_action :load_user, only: [:profile, :follow, :unfollow, :block]
def profile
#posts = Post.all.order("created_at desc").where(user_id: #user.id)
#post = Post.new
end
def feed
#posts = Post.all.order("RANDOM()")
end
def home; end
def follow
current_user.follow(#user)
end
def unfollow
current_user.stop_following(#user)
end
def block
#post.block(#user)
end
private
def load_user
#user = User.find_by_id(params[:id])
# here you can redirect to some place with flash message if #user.blank?
end
end

NoMethod Error - Controller Actions fail to work randomly

I'm creating a ticket booking app as my sample project using Ruby on Rails 4.1. Three are three models - Events, Tickets and Bookings. Events have many tickets and bookings. Tickets have many bookings and they belong to events. Bookings belongs to events and tickets.
The bookings controller looks like:
class BookingsController < ApplicationController
before_action :authenticate_user!
def index
#event = Event.find(params[:event_id])
#bookings = #event.bookings.all
end
def new
#event = Event.find(params[:event_id])
#ticket = #event.tickets.find(params[:ticket_id])
#booking = Booking.new
end
def create
#event = Event.find(params[:event_id])
#ticket = #event.tickets.find(params[:ticket_id])
#booking = #event.bookings.create(booking_params)
#booking.ticket = #ticket
Stripe.api_key = Rails.configuration.stripe[:secret_key]
#token = params[:stripeToken]
#amount = #booking.total_amount
begin
customer = Stripe::Customer.create(
:email => #booking.email,
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => #amount,
:currency => "usd",
#:card => token
)
flash[:notice] = "Thanks for the order"
rescue Stripe::CardError => e
flash[:danger] = e.message
end
if #booking.save
BookingMailer.booking_confirmation_user(#booking).deliver
redirect_to [#event, #booking]
else
render 'new'
end
end
def show
#event = Event.find(params[:event_id])
#booking = #event.bookings.find(params[:id])
end
def destroy
#event = Event.find(params[:event_id])
#booking = #event.bookings.find(params[:id])
#booking.destroy
redirect_to event_bookings_path
end
private
def booking_params
params.require(:booking).permit(:buyer_name, :email, :mobile, :address, :order_quantity, :ticket_id)
end
end
The booking model:
class Booking < ActiveRecord::Base
#before_create :check_ticket_count
belongs_to :event
belongs_to :ticket
has_many :charges
def total_amount
ticket.ticket_price.to_i * order_quantity.to_i
end
def check_ticket_count
count = ticket.ticket_quantity.to_i - order_quantity.to_i
ticket.update_attribute(:ticket_quantity, count)
end
end
Ticket model:
class Ticket < ActiveRecord::Base
belongs_to :event
has_many :bookings
belongs_to :user
before_create :check_start_date
before_update :check_start_date
def check_start_date
if (self.booking_start_date >= DateTime.now) && (self.booking_end_date != DateTime.now)
self.status = 'Open'
else
self.status = 'Closed'
end
end
def maximum_tickets_allowed
(1..maximum_quantity.to_i).to_a
end
end
The Bookings Index Page:
<h2>All Bookings</h2>
<div class="row">
<div class="col-md-10">
<table class="table">
<thead>
<tr>
<th>Buyer Name</th>
<th>Email Address</th>
<th>Ticket Type</th>
<th>No. of Tickets</th>
<th>Amount</th>
<th></th>
</tr>
</thead>
<tbody>
<% #event.bookings.each do |booking| %>
<tr>
<td><%= booking.buyer_name %></td>
<td><%= booking.email %></td>
<td><%= booking.ticket.ticket_name %></td>
<td><%= booking.order_quantity %></td>
<td><%= number_to_currency(booking.total_amount) %></td>
<td><%= link_to "Delete", event_booking_path(#event, booking), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-link" %></td>
<% end %>
</tr>
</table>
</div>
</div>
Bookings show page:
Thanks for your purchase!
<div class="row">
<div class="col-md-8">
<b>Name:</b><p><%= #booking.buyer_name %></p>
<b>No. of Tickets</b><p><%= #booking.order_quantity %></p>
<b>Ticket Tier</b><p><%= #booking.ticket.ticket_name %></p>
<b>Amount Paid:</b><p><%= number_to_currency(#booking.total_amount) %></p>
<p><% #booking.check_ticket_count %></p>
</div>
Now, my problem is bookings work perfectly. All the details are fetched, displayed and model methods executed properly. However, occasionally without even making one single change to these files, I get the "Action controller exception caught error - NoMethodError in Bookings#index" and "Action controller exception caught error - NoMethodError in Bookings#show". The culprit is always the ticket_name in booking.ticket.ticket_name and #booking.ticket.ticket_name where the error states that it's an undefined method.
Full trace is here:
app/views/bookings/index.html.erb:21:in `block in _app_views_bookings_index_html_erb___3226472832114682596_70125337010980'
activerecord (4.1.1) lib/active_record/relation/delegation.rb:46:in `each'
activerecord (4.1.1) lib/active_record/relation/delegation.rb:46:in `each'
app/views/bookings/index.html.erb:17:in `_app_views_bookings_index_html_erb___3226472832114682596_70125337010980'
actionview (4.1.1) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.1) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.1) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.1) lib/action_view/template.rb:143:in `render'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
actionview (4.1.1) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.1) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionview (4.1.1) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.1.1) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.1.1) lib/action_view/rendering.rb:99:in `_render_template'
actionpack (4.1.1) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.1.1) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.1.1) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
actionpack (4.1.1) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.1.1) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.1.1) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.1) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.1) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:676:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.1) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.1) lib/rails/engine.rb:514:in `call'
railties (4.1.1) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
The problem is it's the right field name. Why would the actions work most of the times and fail randomly at times without a single change in the code?
booking.ticket is probably blank
You can do
<td><%= booking.ticket.ticket_name rescue nil %></td>
or
<td>
<% unless booking.ticket.blank? %>
<%= booking.ticket.ticket_name %>
<% else %>
No Ticket
</td>

Resources