Data not deleted from the sqlite3 table - ruby-on-rails

I am new in ruby. I want to delete record. But unable to delete. Please check my code -
ruby_win_sources/index.html.erb
<table border="1" cellpadding="1" cellspacing="1" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Author</th>
<th>Url</th>
<th colspan="3">Action</th>
</tr>
</thead>
<tbody>
<% #ruby_win_sources.each do |ruby_win_source| %>
<tr>
<td><%= ruby_win_source.name %></td>
<td><%= ruby_win_source.author %></td>
<td><%= ruby_win_source.url %></td>
<td><%= link_to 'Show', ruby_win_source %></td>
<td><%= link_to 'Edit', edit_ruby_win_source_path(ruby_win_source) %></td>
<td><%= link_to 'Destroy', ruby_win_source, method: :destroy, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
ruby_win_sources_controller.rb
class RubyWinSourcesController < ApplicationController
before_action :set_ruby_win_source, only: [:show, :edit, :update, :destroy]
# GET /ruby_win_sources
# GET /ruby_win_sources.json
def index
#ruby_win_sources = RubyWinSource.all
end
# GET /ruby_win_sources/1
# GET /ruby_win_sources/1.json
def show
end
# GET /ruby_win_sources/new
def new
#ruby_win_source = RubyWinSource.new
end
# GET /ruby_win_sources/1/edit
def edit
end
# POST /ruby_win_sources
# POST /ruby_win_sources.json
def create
#ruby_win_source = RubyWinSource.new(ruby_win_source_params)
respond_to do |format|
if #ruby_win_source.save
format.html { redirect_to #ruby_win_source, notice: 'Ruby win source was successfully created.' }
format.json { render :show, status: :created, location: #ruby_win_source }
else
format.html { render :new }
format.json { render json: #ruby_win_source.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /ruby_win_sources/1
# PATCH/PUT /ruby_win_sources/1.json
def update
respond_to do |format|
if #ruby_win_source.update(ruby_win_source_params)
format.html { redirect_to #ruby_win_source, notice: 'Ruby win source was successfully updated.' }
format.json { render :show, status: :ok, location: #ruby_win_source }
else
format.html { render :edit }
format.json { render json: #ruby_win_source.errors, status: :unprocessable_entity }
end
end
end
# DELETE /ruby_win_sources/1
# DELETE /ruby_win_sources/1.json
def destroy
#ruby_win_source.destroy
respond_to do |format|
format.html { redirect_to ruby_win_sources_url, notice: 'Ruby win source was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_ruby_win_source
#ruby_win_source = RubyWinSource.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def ruby_win_source_params
params.require(:ruby_win_source).permit(:name, :author, :url)
end
end
Delete confirm box not opening.
Please help me
Edit -
routes.rb
Rails.application.routes.draw do
resources :ruby_win_sources
end

You drop your database rake db:drop and then rake db:migrate to create your tables

I think your link_to to delete is incorrect and should be like this:
<%= link_to 'Destroy', ruby_win_source, method: :delete, data: { confirm: 'Are you sure?' } %>
Then update your controller destroy method to:
# DELETE /ruby_win_sources/1
# DELETE /ruby_win_sources/1.json
def destroy
RubyWinSource.find_by_id(params[:id]).destroy
respond_to do |format|
format.html { redirect_to ruby_win_sources_url, notice: 'Ruby win source was successfully destroyed.' }
format.json { head :no_content }
end
end

Make a delete request while destroying an object:
<td><%= link_to 'Destroy', ruby_win_source, method: :delete, data: { confirm: 'Are you sure?' } %></td>
Your controller looks good since you are setting
before_action :set_ruby_win_source, only: [:show, :edit, :update, :destroy]
So you already have #ruby_win_source before destroy action gets executed.

Related

having a problem setting my a page as a homepage on RoR

I am currently getting this error while loading up the homepage. I have only create the first page which I's trying to make the homepage. TweeetsController#index is missing a template for request formats: text/html Below are my codes for the controller, view, and routes. Thank you for any help.
Controller:
class TweeetsController < ApplicationController
before_action :set_tweeet, only: [:show, :edit, :update, :destroy]
# GET /tweeets
# GET /tweeets.json
def index
#tweeets = Tweeet.all
end
# GET /tweeets/1
# GET /tweeets/1.json
def show
end
# GET /tweeets/new
def new
#tweeet = Tweeet.new
end
# GET /tweeets/1/edit
def edit
end
# POST /tweeets
# POST /tweeets.json
def create
#tweeet = Tweeet.new(tweeet_params)
respond_to do |format|
if #tweeet.save
format.html { redirect_to #tweeet, notice: 'Tweeet was successfully created.' }
format.json { render :show, status: :created, location: #tweeet }
else
format.html { render :new }
format.json { render json: #tweeet.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tweeets/1
# PATCH/PUT /tweeets/1.json
def update
respond_to do |format|
if #tweeet.update(tweeet_params)
format.html { redirect_to #tweeet, notice: 'Tweeet was successfully updated.' }
format.json { render :show, status: :ok, location: #tweeet }
else
format.html { render :edit }
format.json { render json: #tweeet.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tweeets/1
# DELETE /tweeets/1.json
def destroy
#tweeet.destroy
respond_to do |format|
format.html { redirect_to tweeets_url, notice: 'Tweeet was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tweeet
#tweeet = Tweeet.find(params[:id])
end
# Only allow a list of trusted parameters through.
def tweeet_params
params.require(:tweeet).permit(:tweeet)
end
end
view:
<p id="notice"><%= notice %></p>
<h1>Tweeets</h1>
<table>
<thead>
<tr>
<th>Tweeet</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tweeets.each do |tweeet| %>
<tr>
<td><%= tweeet.tweeet %></td>
<td><%= link_to 'Show', tweeet %></td>
<td><%= link_to 'Edit', edit_tweeet_path(tweeet) %></td>
<td><%= link_to 'Destroy', tweeet, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Tweeet', new_tweeet_path %>
routes:
resources :tweeets
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root "tweeets#index"
end
model:
class Tweeet < ApplicationRecord
end

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)

Undefined method error on a nested resource index page

As a follow up to this earlier question, which has been solved
I have made a beginning with a rails app where I can create Collections. Each Collections is able to have multiple Photos. I can now create these Collections and Photos. But whenever I try to visit /collections/1/photos it has a problem with this line in my photos index
undefined method `photo_path' for #<#<Class:0x007fb8c40f1b38>:0x007fb8c88673a0>
<td><%= link_to 'Show', photo %></td>
photos_controller.rb
class PhotosController < ApplicationController
before_action :set_photo, only: [:show, :edit, :update, :destroy]
# GET /photos
# GET /photos.json
def index
#photos = Photo.all
end
# GET /photos/1
# GET /photos/1.json
def show
end
# GET /photos/new
def new
#photo = Photo.new
end
# GET /photos/1/edit
def edit
end
# POST /photos
# POST /photos.json
def create
#photo = Photo.new(photo_params)
respond_to do |format|
if #photo.save
redirect_to #photo
format.html { redirect_to #photo, notice: 'Photo was successfully created.' }
format.json { render :show, status: :created, location: #photo }
else
format.html { render :new }
format.json { render json: #photo.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /photos/1
# PATCH/PUT /photos/1.json
def update
respond_to do |format|
if #photo.update(photo_params)
format.html { redirect_to #photo, notice: 'Photo was successfully updated.' }
format.json { render :show, status: :ok, location: #photo }
else
format.html { render :edit }
format.json { render json: #photo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /photos/1
# DELETE /photos/1.json
def destroy
#photo.destroy
respond_to do |format|
format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_photo
#photo = Photo.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def photo_params
params.require(:photo).permit(:name, :collection_id)
end
end
routes.rb
Rails.application.routes.draw do
resources :collections do
resources :photos
end
end
/photos/index.html
<p id="notice"><%= notice %></p>
<h1>Photos</h1>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #photos.each do |photo| %>
<tr>
<td><%= photo.name %></td>
<td><%= link_to 'Show', photo %></td>
<td><%= link_to 'Edit', edit_photo_path(photo) %></td>
<td><%= link_to 'Destroy', photo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Photo', new_collection_photo_path %>
collection.rb
class Collection < ApplicationRecord
has_many :photos
end
photo.rb
class Photo < ApplicationRecord
belongs_to :collection
end
I use Rails 5.0.0.1 and ruby 2.3.0
Use this your show and delete link path is wrong
<td><%= link_to 'Show', collection_photo_path(#collection, photo) %></td>
<td><%= link_to 'Edit', edit_collection_photo_path(#collection, photo) %></td>
<td><%= link_to 'Destroy',
collection_photo_path(#collection, photo), method: :delete, data: { confirm: 'Are you sure?' } %></td>
Links should be made this , from controller you should intialize #collection
def index
#collction = Collection.find(params[:collection_id])
#photos = #collction.photos
end

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

Ruby on Rails does not delete using scaffolding

i am new in RoR and have a problem with Ruby on Rails because this doesn't delete records when i clicked the button Destroy.
I create a new project and using the command scaffold to create it.
My view:
<h1>CARTELERA</h1>
<table>
<thead>
<tr>
<th>Titulo</th>
<th>Genero</th>
<th>Director</th>
<th>Duracion</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #peliculas.each do |pelicula| %>
<tr>
<td><%= pelicula.titulo %></td>
<td><%= pelicula.genero %></td>
<td><%= pelicula.director %></td>
<td><%= pelicula.duracion %></td>
<td><%= link_to 'Mostrar', pelicula %></td>
<td><%= link_to 'Editar', edit_pelicula_path(pelicula) %></td>
<td><%= link_to 'Eliminar', pelicula, method: :delete, data: { confirm: 'Estás seguro?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'Nueva Película', new_pelicula_path %>
My method desploy in controller:
def destroy
#pelicula.destroy
respond_to do |format|
format.html { redirect_to peliculas_url, notice: 'Pelicula was successfully destroyed.' }
format.json { head :no_content }
end
end
Output Rake routes
C:\Cine>rake routes
Prefix Verb URI Pattern Controller#Action
peliculas GET /peliculas(.:format) peliculas#index
POST /peliculas(.:format) peliculas#create
new_pelicula GET /peliculas/new(.:format) peliculas#new
edit_pelicula GET /peliculas/:id/edit(.:format) peliculas#edit
pelicula GET /peliculas/:id(.:format) peliculas#show
PATCH /peliculas/:id(.:format) peliculas#update
PUT /peliculas/:id(.:format) peliculas#update
DELETE /peliculas/:id(.:format) peliculas#destroy
root GET / peliculas#index
My controller comlete
class PeliculasController < ApplicationController
before_action :set_pelicula, only: [:show, :edit, :update, :destroy]
# GET /peliculas
# GET /peliculas.json
def index
#peliculas = Pelicula.all
end
# GET /peliculas/1
# GET /peliculas/1.json
def show
end
# GET /peliculas/new
def new
#pelicula = Pelicula.new
end
# GET /peliculas/1/edit
def edit
end
# POST /peliculas
# POST /peliculas.json
def create
#pelicula = Pelicula.new(pelicula_params)
respond_to do |format|
if #pelicula.save
format.html { redirect_to #pelicula, notice: 'Pelicula was successfully created.' }
format.json { render :show, status: :created, location: #pelicula }
else
format.html { render :new }
format.json { render json: #pelicula.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /peliculas/1
# PATCH/PUT /peliculas/1.json
def update
respond_to do |format|
if #pelicula.update(pelicula_params)
format.html { redirect_to #pelicula, notice: 'Pelicula was successfully updated.' }
format.json { render :show, status: :ok, location: #pelicula }
else
format.html { render :edit }
format.json { render json: #pelicula.errors, status: :unprocessable_entity }
end
end
end
# DELETE /peliculas/1
# DELETE /peliculas/1.json
def destroy
#pelicula.destroy
respond_to do |format|
format.html { redirect_to peliculas_url, notice: 'Pelicula was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pelicula
#pelicula = Pelicula.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def pelicula_params
params.require(:pelicula).permit(:titulo, :genero, :director, :duracion)
end
end
I think it's because you have the syntax wrong. try adding this :method => :delete right now you have <%= link_to 'Eliminar', pelicula, method: :delete, data: { confirm: 'Estás seguro?' } %>. Below is a way to debug if request are being sent or not.
When your running your server locally check the logs in the terminal window.
Pressing enter a few times within the terminal, while the server is running will give you some negative space inbetween the last action a new action your app will preform.
After you've added some negitave space in the terminal, Go back to the app and press delete on the item.
Go back to your terminal and find the gap of space If you see
Started DELETE "/peliculas/{Some Number}"
Processing by PeliculasController#destroy as HTML
That means it's working.
I'm pretty sure it could be because your syntax after the 'Eliminar' is wrong change it to, :method => :delete instead of method: :delete

Resources