NoMethodError in Admin - Rails change of namespace - ruby-on-rails

This is really bugging me. My controller was working fine, but I have now changed the namespace and appear to be having issues with the paths or something. I tried editing the paths in accordance with rake routes but still no avail
here is the error:
Showing /home/will/Development/Ruby-Files/tasks/app/views/admin/testos/index.html.erb where line #16 raised:
undefined method `testo_path' for #<#<Class:0xb61ee4f0>:0xaeb2c38>
My controller:
class Admin::TestosController < ApplicationController
# GET /testos
# GET /testos.json
def index
#testos = Testo.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #testos }
end
end
# GET /testos/1
# GET /testos/1.json
def show
#testo = Testo.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #testo }
end
end
# GET /testos/new
# GET /testos/new.json
def new
#testo = Testo.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #testo }
end
end
# GET /testos/1/edit
def edit
#testo = Testo.find(params[:id])
end
# POST /testos
# POST /testos.json
def create
#testo = Testo.new(params[:testo])
respond_to do |format|
if #testo.save
format.html { redirect_to #testo, notice: 'Testo was successfully created.' }
format.json { render json: #testo, status: :created, location: #testo }
else
format.html { render action: "new" }
format.json { render json: #testo.errors, status: :unprocessable_entity }
end
end
end
# PUT /testos/1
# PUT /testos/1.json
def update
#testo = Testo.find(params[:id])
respond_to do |format|
if #testo.update_attributes(params[:testo])
format.html { redirect_to #testo, notice: 'Testo was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #testo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /testos/1
# DELETE /testos/1.json
def destroy
#testo = Testo.find(params[:id])
#testo.destroy
respond_to do |format|
format.html { redirect_to testos_url }
format.json { head :no_content }
end
end
end
And my view file
Listing testos
<table>
<tr>
<th>Title</th>
<th>Entry</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #testos.each do |testo| %>
<tr>
<td><%= testo.title %></td>
<td><%= testo.entry %></td>
<td><%= link_to 'Show', testo %></td>
<td><%= link_to 'Edit', edit_testo_path(testo) %></td>
<td><%= link_to 'Destroy', testo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Testo', new_testo_path %>
My routing:
namespace :admin do
resources :testos
end
Rake routes:
root / Pages#index
admin_testos GET /admin/testos(.:format) admin/testos#index
POST /admin/testos(.:format) admin/testos#create
new_admin_testo GET /admin/testos/new(.:format) admin/testos#new
edit_admin_testo GET /admin/testos/:id/edit(.:format) admin/testos#edit
admin_testo GET /admin/testos/:id(.:format) admin/testos#show
PUT /admin/testos/:id(.:format) admin/testos#update
DELETE /admin/testos/:id(.:format) admin/testos#destroy

Your view contains non-namespaced routes. Substitute these out for namespaced ones:
<td><%= link_to 'Show', admin_testo_path(testo) %></td>
<td><%= link_to 'Edit', edit_admin_testo_path(testo) %></td>

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

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

Ajax in Rails, it doesn't work because it jumps to other page without staying in current page.

I am in entry level of Rails. I has made the database (whose name is "shop") with scaffold. Also I made the page which shows the overview of table and jumps to other page which shows the specific information of record.
Now I try to make the page that the specific information of record is shown in the same page of the overview of table when the "show" link on the one row of table is clicked.
However, despite of my expectation, it jumps to other page as I already have done.
I used many hours about this so would like to have someone's help.
Thank you.
\app\controllers\shops_controller.rb
class ShopsController < ApplicationController
# GET /shops
# GET /shops.json
def index
#shops = Shop.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #shops }
end
end
# GET /shops/1
# GET /shops/1.json
def show
#shop = Shop.find(params[:id])
respond_to do |format|
format.html { render layout: (request.headers["X-Requested-With"] != 'XMLHttpRequest') }
format.json { render json: #shop }
end
end
# GET /shops/new
# GET /shops/new.json
def new
#shop = Shop.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #shop }
end
end
# GET /shops/1/edit
def edit
#shop = Shop.find(params[:id])
end
# POST /shops
# POST /shops.json
def create
#shop = Shop.new(params[:shop])
respond_to do |format|
if #shop.save
format.html { redirect_to #shop, notice: 'Shop was successfully created.' }
format.json { render json: #shop, status: :created, location: #shop }
else
format.html { render action: "new" }
format.json { render json: #shop.errors, status: :unprocessable_entity }
end
end
end
# PUT /shops/1
# PUT /shops/1.json
def update
#shop = Shop.find(params[:id])
respond_to do |format|
if #shop.update_attributes(params[:shop])
format.html { redirect_to #shop, notice: 'Shop was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #shop.errors, status: :unprocessable_entity }
end
end
end
# DELETE /shops/1
# DELETE /shops/1.json
def destroy
#shop = Shop.find(params[:id])
#shop.destroy
respond_to do |format|
format.html { redirect_to shops_url }
format.json { head :no_content }
end
end
end
\app\views\shops\index.html.erb
<h2>Shops</h2>
<table>
<tr>
<th>Name</th>
<th>Link_ID</th>
<th>Offer</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #shops.each do |shop| %>
<tr>
<td><%= shop.name %></td>
<td><%= shop.link_id %></td>
<td><%= shop.offer %></td>
<td><%= link_to 'Show', shop, :remote => true, "data-type" => "html", :class => 'show' %></td>
<td><%= link_to 'Edit', edit_shop_path(shop) %></td>
<td><%= link_to 'Destroy', shop, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<div id="show_area"></div>
<%= javascript_tag do %>
$('a.show').on('ajax:success', function (data, status, xhr) {
$('#show_area').html(status);})
<% end %>
\app\views\shops\show.js.erb
$('#show_area').html("<%= raw(j(render :partial => 'show_body')) %>")
If you have jquery rails in your gemfile (gem 'jquery-rails') and have:
//= require jquery
//= require jquery_ujs
in your application.js, what you've got there should work. Minus the show.js.erb, which would require you changing the data-type to be script instead of html. Although based on your description, the show.js.erb wouldn't be needed. The script you have in index.html.erb will suffice.

Adding a new namespace for admin area

I am attempting to move one of my controllers (Testo) into the admin space, I have changed to route file as so:
namespace :admin do
resources :testos
end
Class name for the controller:
class TestosController < ApplicationController
Error I am getting:
uninitialized constant Admin::TestosController
Rake routes gives me:
root / Pages#index
admin_testos GET /admin/testos(.:format) admin/testos#index
POST /admin/testos(.:format) admin/testos#create
new_admin_testo GET /admin/testos/new(.:format) admin/testos#new
edit_admin_testo GET /admin/testos/:id/edit(.:format) admin/testos#edit
admin_testo GET /admin/testos/:id(.:format) admin/testos#show
PUT /admin/testos/:id(.:format) admin/testos#update
DELETE /admin/testos/:id(.:format) admin/testos#destroy
Full TestosController:
class Admin::TestosController < ApplicationController
# GET /testos
# GET /testos.json
def index
#testos = Testo.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #testos }
end
end
# GET /testos/1
# GET /testos/1.json
def show
#testo = Testo.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #testo }
end
end
# GET /testos/new
# GET /testos/new.json
def new
#testo = Testo.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #testo }
end
end
# GET /testos/1/edit
def edit
#testo = Testo.find(params[:id])
end
# POST /testos
# POST /testos.json
def create
#testo = Testo.new(params[:testo])
respond_to do |format|
if #testo.save
format.html { redirect_to #testo, notice: 'Testo was successfully created.' }
format.json { render json: #testo, status: :created, location: #testo }
else
format.html { render action: "new" }
format.json { render json: #testo.errors, status: :unprocessable_entity }
end
end
end
# PUT /testos/1
# PUT /testos/1.json
def update
#testo = Testo.find(params[:id])
respond_to do |format|
if #testo.update_attributes(params[:testo])
format.html { redirect_to #testo, notice: 'Testo was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #testo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /testos/1
# DELETE /testos/1.json
def destroy
#testo = Testo.find(params[:id])
#testo.destroy
respond_to do |format|
format.html { redirect_to testos_url }
format.json { head :no_content }
end
end
end
and here is the view file:
Listing testos
<table>
<tr>
<th>Title</th>
<th>Entry</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #testos.each do |testo| %>
<tr>
<td><%= testo.title %></td>
<td><%= testo.entry %></td>
<td><%= link_to 'Show', testo %></td>
<td><%= link_to 'Edit', edit_testo_path(testo) %></td>
<td><%= link_to 'Destroy', testo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Testo', new_testo_path %>
Your class declaration should read as:
class Admin::TestosController < ApplicationController

Ruby - error rendering a view referencing a loop that I never wrote

I have this controller:
class AlexesController < ApplicationController
# GET /alexes
# GET /alexes.json
def index
##alexes = Alex.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #alexes }
end
end
# GET /alexes/1
# GET /alexes/1.json
def show
# #alex = Alex.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #alex }
end
end
# GET /alexes/new
# GET /alexes/new.json
def new
#alex = Alex.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #alex }
end
end
# GET /alexes/1/edit
def edit
#alex = Alex.find(params[:id])
end
# POST /alexes
# POST /alexes.json
def create
#alex = Alex.new(params[:alex])
respond_to do |format|
if #alex.save
format.html { redirect_to #alex, notice: 'Alex was successfully created.' }
format.json { render json: #alex, status: :created, location: #alex }
else
format.html { render action: "new" }
format.json { render json: #alex.errors, status: :unprocessable_entity }
end
end
end
# PUT /alexes/1
# PUT /alexes/1.json
def update
#alex = Alex.find(params[:id])
respond_to do |format|
if #alex.update_attributes(params[:alex])
format.html { redirect_to #alex, notice: 'Alex was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #alex.errors, status: :unprocessable_entity }
end
end
end
# DELETE /alexes/1
# DELETE /alexes/1.json
def destroy
#alex = Alex.find(params[:id])
#alex.destroy
respond_to do |format|
format.html { redirect_to alexes_url }
format.json { head :no_content }
end
end
end
It gets called when this link gets pressed:
<%= link_to "Alex Link", alexes_path(#alex) %>
so I am assuming that the get-all part of the controller would get invoked, and I commented out lines in the controller that I thought would get invoked, but I still get this error:
undefined method `each' for nil:NilClass
from line 10:
7: <th></th>
8: </tr>
9:
10: <% #alexes.each do |alex| %>
11: <tr>
12: <td><%= link_to 'Show', alex %></td>
13: <td><%= link_to 'Edit', edit_alex_path(alex) %></td>
Any idea where the problem is happening?
Thanks!
<%= link_to "Alex Link", alexes_path(#alex) %>
=>
<%= link_to "Alex Link", alex_path(#alex) %>
or
<%= link_to "Alex Link", #alex %>

Resources