ActiveRecord::RecordNotFound in TodosController#index - ruby-on-rails

The full text of the error message is:
ActiveRecord::RecordNotFound in TodosController#index
Couldn't find User with id=2
Rails.root: /home/randy/rubystack-1.9.3-29/projects/chap14
app/controllers/application_controller.rb:11:in current_user
app/views/todos/index.html.erb:21:in _app_views_todos_index_html_erb___949818655437808348_39324440
app/controllers/todos_controller.rb:8:in index
Here's the code for my application_controller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
private
def current_user
if session[:user_id]
#current_user ||= User.find(session[:user_id])
else
#current_user = nil
end
end
def check_login
unless authorized?
redirect_to "/auth/identity"
end
end
def logged_in?
if session[:user_id]
return true
else
return false
end
end
protected
def authorized?
logged_in? && (request.get? || current_user.admin?)
end
end."
And here's my todos_controller.rb:
class TodosController < ApplicationController
before_filter :check_login
# GET /todos
# GET /todos.json
def index
#todos = Todo.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #todos }
end
end
# GET /todos/1
# GET /todos/1.json
def show
#todo = Todo.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #todo }
end
end
# GET /todos/new
# GET /todos/new.json
def new
#todo = Todo.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #todo }
end
end
# GET /todos/1/edit
def edit
#todo = Todo.find(params[:id])
end
# POST /todos
# POST /todos.json
def create
#todo = Todo.new(params[:todo])
respond_to do |format|
if #todo.save
format.html { redirect_to #todo, notice: 'Todo was successfully created.' }
format.json { render json: #todo, status: :created, location: #todo }
else
format.html { render action: "new" }
format.json { render json: #todo.errors, status: :unprocessable_entity }
end
end
end
# PUT /todos/1
# PUT /todos/1.json
def update
#todo = Todo.find(params[:id])
respond_to do |format|
if #todo.update_attributes(params[:todo])
format.html { redirect_to #todo, notice: 'Todo was successfully updated.' }
format.json { head :no_content }
#todo = Todo.find(params[:id])
if #todo.completed == true
#todo.user_who_completed = current_user.email
#todo.save
end
else
format.html { render action: "edit" }
format.json { render json: #todo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /todos/1
# DELETE /todos/1.json
def destroy
#todo = Todo.find(params[:id])
#todo.destroy
respond_to do |format|
format.html { redirect_to todos_url }
format.json { head :no_content }
end
end
end."
And my app/views/todos/index.html.erb file:
"<h1>Listing todos</h1>
<table>
<tr>
<th>Name</th>
<th>Completed</th>
<th>Completed date</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>
<% for todo in #todos %>
<tr>
<td><%= todo.name %></td>
<td><%= todo.completed %></td>
<td><%= todo.completed_date %></td>
<td><%= todo.user %></td>
<% end %>
<% if current_user.admin? %>
<td><%= link_to 'Show', todo %></td>
<td><%= link_to 'Edit', edit_todo_path(todo) %></td>
<td><%= link_to 'Destroy', todo, method: :delete, data: { confirm: 'Are you sure?' } %></td>S
</tr>
<% end %>
</table>
<br />
<% if current_user.admin? %>
<%= link_to 'New Todo', new_todo_path %>
<% end %>
I'm new to rails and have no clue about what the error message means, nor its cause. I want the visitor to land on the todos/index.html page. I have my root route set accordingly. Would appreciate some help.

That error is coming from User.find. It's described in the Documentation for find.
It's telling you that there is no User that has that id. Somehow you have saved a user id into your session that corresponds to a user that doesn't currently exist.
You should probably examine how you are storing user ids to the session and under what circumstances that user could be getting deleted.

The problem is with your loop. If conditional it is outside the cycle.
Check solution view:
<h1>Listing todos</h1>
<table>
<tr>
<th>Name</th>
<th>Completed</th>
<th>Completed date</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>
<% for todo in #todos %>
<tr>
<td><%= todo.name %></td>
<td><%= todo.completed %></td>
<td><%= todo.completed_date %></td>
<td><%= todo.user %></td>
<% if current_user.admin? %>
<td><%= link_to 'Show', todo %></td>
<td><%= link_to 'Edit', edit_todo_path(todo) %></td>
<td><%= link_to 'Destroy', todo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
</table>
Recommendations
Check your question. You have the code view with the controller code.
Be careful with indentation.

Related

NoMethodError (undefined method `persist' for #<PersonalTrainer:0x000055625dbfd7a8> when trying to create a table entry

I am trying to add items to a table in rails but getting the error it the title. It works for me locally but on heroku it gives me this error. I have seen a couple of solution but none seem to work. This is my controller:
require './app/policies/personal_trainer_policy'
class PersonalTrainersController < ApplicationController
before_action :set_personal_trainer, only: [:show, :edit, :update, :destroy]
# GET /personal_trainers
# GET /personal_trainers.json
def index
#personal_trainers = PersonalTrainer.all
end
# GET /personal_trainers/1
# GET /personal_trainers/1.json
def show
end
# GET /personal_trainers/new
def new
#personal_trainer = PersonalTrainer.new
end
# GET /personal_trainers/1/edit
def edit
end
def personal_trainer_policy
#_personal_trainer_policy ||= PersonalTrainerPolicy.new(personal_trainer)
end
# POST /personal_trainers
# POST /personal_trainers.json
def create
#personal_trainer = PersonalTrainer.new(personal_trainer_params)
authorize #personal_trainer
if #personal_trainer.persist
render json: #personal_trainer.record
else
render json: #personal_trainer.errors, status: :unpocessably_entity
end
respond_to do |format|
if #personal_trainer.save
format.html { redirect_to #personal_trainer, notice: 'Personal trainer was successfully created.' }
format.json { render :show, status: :created, location: #personal_trainer }
else
format.html { render :new }
format.json { render json: #personal_trainer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /personal_trainers/1
# PATCH/PUT /personal_trainers/1.json
def update
authorize #personal_trainer
respond_to do |format|
if #personal_trainer.update(personal_trainer_params)
format.html { redirect_to #personal_trainer, notice: 'Personal trainer was successfully updated.' }
format.json { render :show, status: :ok, location: #personal_trainer }
else
format.html { render :edit }
format.json { render json: #personal_trainer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /personal_trainers/1
# DELETE /personal_trainers/1.json
def destroy
authorize #personal_trainer
#personal_trainer.destroy
respond_to do |format|
format.html { redirect_to personal_trainers_url, notice: 'Personal trainer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_personal_trainer
#personal_trainer = PersonalTrainer.find(params[:id])
end
# Only allow a list of trusted parameters through.
def personal_trainer_params
params.require(:personal_trainer).permit(:firstName, :secondName, :desription, :amountOfClients)
end
end
The controller was generated using a scaffold. This is my erb file:
<p id="notice"><%= notice %></p>
<h1 class = "title">Personal Trainers</h1>
<div class = "page-conatiner">
<table class = "table">
<thead>
<%= stylesheet_link_tag 'gym_classes', media: 'all', 'data-turbolinks-track': 'reload' %>
<tr>
<th>First Name</th>
<th>Second Name</th>
<th>Desription</th>
<th>Amount of Clients</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #personal_trainers.each do |personal_trainer| %>
<tr>
<td><%= personal_trainer.firstName %></td>
<td><%= personal_trainer.secondName %></td>
<td><%= personal_trainer.desription %></td>
<td><%= personal_trainer.amountOfClients %></td>
<td><%= link_to 'Show', personal_trainer %></td>
<td><%= link_to 'Edit', edit_personal_trainer_path(personal_trainer) %></td>
<td><%= link_to 'Destroy', personal_trainer, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if current_user.admin == true %>
<%= link_to 'New Personal Trainer', new_personal_trainer_path %>
<% end %>
</div>
and this is my model:
class PersonalTrainer < ApplicationRecord
has_many :gym_class_final
has_many :pt_client
end
I have it set so that admins can only create new items using a policy and adding authorise #personal_trainer. I wonder if this has something to do with the error. I just dont understand how it would work locally but not when its deployed. Any suggestions.
persist is not a valid method on an active record object, you're looking for save:
if #personal_trainer.save
render json: #personal_trainer.record
else
render json: #personal_trainer.errors, status: :unpocessably_entity
end
For more information on what methods are available for active record persistence, see https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html (Rails 6)

ArgumentError in Shakes#index , nil is not a valid asset source

Hi guys when iam trying to pull image using image tag its giving nil is not a valid asset source ive image file in my assests , strangely this problem is only occuring when iam trying use decorator pattern else without it image is being properly displaye ,Iam fairly new to this can any of the experts shred some light what am i doing wrong ?
Thanks
require 'shake_decorator'
class ShakesController < ApplicationController
before_action :set_shake, only: [:show, :edit, :update, :destroy]
# GET /shakes
# GET /shakes.json
def index
#shakes = Shake.all
end
# GET /shakes/1
# GET /shakes/1.json
def show
end
# GET /shakes/new
def new
#shake = Shake.new
end
# GET /shakes/1/edit
def edit
end
# POST /shakes
# POST /shakes.json
def create
#shake = Shake.new()
#shake.Name=params[:shake][:Name]
#shake.Cost=params[:shake][:Cost]
#shake.Calories=params[:shake][:Calories]
# create an instance/object of a BasicCar
myShake=BasicShake.new(#shake.Cost,#shake.Calories)
#add the extra features to the new car
if params[:shake][:caramel].to_s.length > 0 then
myShake = CaramelDecorator.new(myShake)
end
if params[:shake][:pbutter].to_s.length > 0 then
myShake = PeanutbutterDecorator.new(myShake)
end
if params[:shake][:cream].to_s.length > 0 then
myShake = CreamDecorator.new(myShake)
end
#populate the cost and the description details
#shake.Cost = myShake.cost
#shake.description = myShake.details
#retrieve the instance/object of the MyLogger class
respond_to do |format|
if #shake.save
format.html { redirect_to #shake, notice: 'Shake was successfully created.' }
format.json { render :show, status: :created, location: #shake }
else
format.html { render :new }
format.json { render json: #shake.errors, status: :unprocessable_entity }
end
end
end
# DELETE /shakes/1
# DELETE /shakes/1.json
def destroy
#shake.destroy
respond_to do |format|
format.html { redirect_to shakes_url, notice: 'Shake was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_shake
#shake = Shake.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def shake_params
params.require(:shake).permit(:Name, :Cost, :Calories, :image_url)
end
end
<p id="notice"><%= notice %></p>
<h1>Shakes</h1>
<table border="2">
<thead>
<tr>
<th>Name</th>
<th>Cost</th>
<th>Calories</th>
<th>Image url</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #shakes.each do |shake| %>
<tr>
<td><%= shake.Name %></td>
<td><%= shake.Cost %></td>
<td><%= shake.Calories %></td>
<td><%= image_tag(shake.image_url, :class => 'list_image' ,:style => "height:100px") %></td>
<td><%= link_to 'Show', shake %></td>
<td><%= link_to 'Edit', edit_shake_path(shake) %></td>
<td><%= link_to 'Destroy', shake, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Shake', new_shake_path %>

undefined method `each' for nil:NilClass. The views and the controller match up

I have looked all over on stack overflow to try to fix this problem and I can't seem to find a fix. I can include more code if needed.
I tried changing in the controller to comment to comments and that does not change anything.
I tried changing to comments to comment and that does not change anything.
I am legit stuck on this and I want to know why this is happening. Thanks
Comments_controller
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
# GET /comments
# GET /comments.json
# POST /comments
# POST /comments.json
def create
#pin = Pin.find(params[:pin_id])
#comments = #pin.comments.new(comment_params)
#comment.user = current_user
respond_to do |format|
if #comment.save
format.html { redirect_to #comment, notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: #comment }
else
format.html { render :new }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
#comment.destroy
respond_to do |format|
format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
#comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:pin_id, :body, :user_id)
end
end
My index.html.erb
<h1>Listing comments</h1>
<table>
<thead>
<tr>
<th>Link</th>
<th>Body</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #comments.each do |comment| %>
<tr>
<td><%= comment.link_id %></td>
<td><%= comment.body %></td>
<td><%= comment.user %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Comment', new_comment_path %>
Add an index method to the controller:
def index
#comments = Comment.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #comments }
end
end

Why is only part of my code not rendering in view

While I was working trying to code my views, I noticed that my code that was previously rendering on the index view is now only showing the first two lines of code on my local server, and I don't understand why.
Here is my index.html.erb code:
<h1>All Bookmarks</h1>
<%= link_to 'Create a New Bookmark', new_bookmark_path %>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<div class="row">
<div class="col-md-8">
<tbody>
<% #bookmarks.each do |bookmark| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<tr>
<td><%= link_to bookmark.url, "http://#{bookmark.url}" %></td>
<td><%= link_to 'Show', bookmark %></td>
<td><%= link_to 'Edit', edit_bookmark_path(bookmark) %></td>
<td><%= link_to 'Destroy', bookmark, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
And here is my bookmark controller code:
class BookmarksController < ApplicationController
before_action :set_bookmark, only: [:show, :edit, :update, :destroy]
def index
#bookmarks = Bookmark.all
end
def show
end
def new
#bookmark = Bookmark.new
end
def edit
end
def create
bookmark = Bookmark.where(url: params[:bookmark][:url]).first
#bookmark = bookmark.present? ? bookmark : Bookmark.new(bookmark_params)
if #bookmark.save
#bookmark.users << current_user
Rails.logger.info ">>>>>>>>>>>>> Bookmark: #{#bookmark.inspect}"
topic_names = params[:topic_names].split(' ')
topic_names.each do |topic_name|
name = topic_name.sub(/#/, '')
#bookmark.topics << Topic.find_or_create_by_name(name)
end
respond_to do |format|
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully created.' }
format.json { render action: 'show', status: :created, location: #bookmark }
end
else
respond_to do |format|
format.html { render action: 'new' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #bookmark.update(bookmark_params)
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def destroy
#bookmark.destroy
respond_to do |format|
format.html { redirect_to bookmarks_url }
format.json { head :no_content }
end
end
private
def set_bookmark
#bookmark = Bookmark.find(params[:id])
end
def bookmark_params
params.require(:bookmark).permit(:url)
end
end
Any thoughts?
It seems that your html is invalid. You are using div tags in rails loop, which is not being closed. The other thing is that non-table related html tags can be used only inside tags.
This might be the working solution.
<h1>All Bookmarks</h1>
<%= link_to 'Create a New Bookmark', new_bookmark_path %>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #bookmarks.each do |bookmark| %>
<tr>
<td><%= link_to bookmark.url, "http://#{bookmark.url}" %></td>
<td><%= link_to 'Show', bookmark %></td>
<td><%= link_to 'Edit', edit_bookmark_path(bookmark) %></td>
<td><%= link_to 'Destroy', bookmark, method: :delete, data: { confirm: 'Are you sure?' } %> </td>
</tr>
<% end %>
</tbody>
</table>
Hope that helps.

Scaffold Rails Application Removed Show Action, Now Edit/Destroy No Longer Works

Hi I generated a scaffold for my rails application. I put in resources for that model in the config file. I figure I can cut/modify what scaffold generated later. I removed the show method/view etc and now destroy/edit no longer works.
Turns out that destroy/edit need show to be there as well because of resource.
I want to push show back into my application to fix this issue, can someone help me pinpoint the issue?
trashes_controller.rb
class TrashesController < ApplicationController
# GET /trashes
# GET /trashes.json
def index
#trash = Trash.all
#json = #trash.to_gmaps4rails
end
end
# GET /trashes/1
# GET /trashes/1.json
def show
#trash = Trash.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #trash }
end
end
# GET /trashes/new
# GET /trashes/new.json
def new
#trash = Trash.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #trash }
end
end
# GET /trashes/1/edit
def edit
#trash = Trash.find(params[:id])
end
# POST /trashes
# POST /trashes.json
def create
#trash = Trash.new(params[:trash])
respond_to do |format|
if #trash.save
format.html { redirect_to #trash, notice: 'Trash was successfully created.' }
format.json { render json: #trash, status: :created, location: #trash }
else
format.html { render action: "new" }
format.json { render json: #trash.errors, status: :unprocessable_entity }
end
end
end
# PUT /trashes/1
# PUT /trashes/1.json
def update
#trash = Trash.find(params[:id])
respond_to do |format|
if #trash.update_attributes(params[:trash])
format.html { redirect_to #trash, notice: 'Trash was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #trash.errors, status: :unprocessable_entity }
end
end
end
# DELETE /trashes/1
# DELETE /trashes/1.json
def destroy
#trash = Trash.find(params[:id])
#trash.destroy
respond_to do |format|
format.html { redirect_to trashes_url }
format.json { head :no_content }
end
end
index.html.erb within trashes folder
<h1>Listing Boston/Cambridge trash bin locations</h1>
<%= gmaps4rails(#json) %>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Category</th>
<th></th>
<th></th>
</tr>
<% #trash.each do |trash| %>
<tr>
<td><%= trash.name %></td>
<td><%= trash.address %></td>
<td><%= trash.category %></td>
<td><%= link_to 'Show', trash %></td>
<td><%= link_to 'Edit', edit_trash_path(trash) %></td>
<td><%= link_to 'Destroy', trash, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Boston Solar Powered Trash Can Location', new_trash_path %>
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= #trash.name %>
</p>
<p>
<b>Address:</b>
<%= #trash.address %>
</p>
<p>
<b>Category:</b>
<%= #trash.category %>
</p>
<%= link_to 'Edit', edit_trash_path(#trash) %> |
<%= link_to 'Back', trashes_path %>
I keep on getting undefined method `name' for nil:NilClass when I click show in my index root page in local host.
Here is my rake routes pages, probably more useful.
trashes GET /trashes(.:format) trashes#index
POST /trashes(.:format) trashes#create
new_trash GET /trashes/new(.:format) trashes#new
edit_trash GET /trashes/:id/edit(.:format) trashes#edit
trash GET /trashes/:id(.:format) trashes#show
PUT /trashes/:id(.:format) trashes#update
DELETE /trashes/:id(.:format) trashes#destroy
root / trashes#index
page GET /pages/*id high_voltage/pages#show
If your error occur when you try to click 'show' link, than modify your link_to path like this :
td><%= link_to 'Show', trash_path(trash) %></td>
Hope this will help.

Resources