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
Related
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
I have the following rails code
Employee model: id | emp_name | emp_number
class Employee < ActiveRecord::Base
has_many :visitors
end
Visitor Model:id|employee_id|visitor_name|vis_company|vis|email
class Visitor < ActiveRecord::Base
belongs_to :employee
end
Employee Controller :
class EmployeesController < ApplicationController
before_action :set_employee, only: [:show, :edit, :update, :destroy]
# GET /employees
# GET /employees.json
def index
#employees = Employee.all
end
# GET /employees/1
# GET /employees/1.json
def show
end
# GET /employees/new
def new
#employee = Employee.new
end
# GET /employees/1/edit
def edit
end
# POST /employees
# POST /employees.json
def create
#employee = Employee.new(employee_params)
respond_to do |format|
if #employee.save
format.html { redirect_to #employee, notice: 'Employee was successfully created.' }
format.json { render :show, status: :created, location: #employee }
else
format.html { render :new }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /employees/1
# PATCH/PUT /employees/1.json
def update
respond_to do |format|
if #employee.update(employee_params)
format.html { redirect_to #employee, notice: 'Employee was successfully updated.' }
format.json { render :show, status: :ok, location: #employee }
else
format.html { render :edit }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# DELETE /employees/1
# DELETE /employees/1.json
def destroy
#employee.destroy
respond_to do |format|
format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_employee
#employee = Employee.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def employee_params
params.require(:employee).permit(:emp_id, :emp_name, :emp_email, :emp_phone, :emp_mobile)
end
end
Visitor Controller :
class VisitorsController < ApplicationController
before_action :set_visitor, only: [:show, :edit, :update, :destroy]
# GET /visitors
# GET /visitors.json
def index
##visitors = Visitor.find(:all, :order => 'emp_name')
##visitors = Visitor.all.includes(:emp_name)
#visitors = Visitor.all
##employees = #visitors.Employee.find(:all, :order => 'emp_name')
##employees = #visitors.employee :include => [:emp_name]
end
# GET /visitors/1
# GET /visitors/1.json
def show
end
# GET /visitors/new
def new
#visitor = Visitor.new
end
# GET /visitors/1/edit
def edit
end
# POST /visitors
# POST /visitors.json
def create
#visitor = Visitor.new(visitor_params)
respond_to do |format|
if #visitor.save
format.html { redirect_to #visitor, notice: 'Visitor was successfully created.' }
format.json { render :show, status: :created, location: #visitor }
else
format.html { render :new }
format.json { render json: #visitor.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /visitors/1
# PATCH/PUT /visitors/1.json
def update
respond_to do |format|
if #visitor.update(visitor_params)
format.html { redirect_to #visitor, notice: 'Visitor was successfully updated.' }
format.json { render :show, status: :ok, location: #visitor }
else
format.html { render :edit }
format.json { render json: #visitor.errors, status: :unprocessable_entity }
end
end
end
# DELETE /visitors/1
# DELETE /visitors/1.json
def destroy
#visitor.destroy
respond_to do |format|
format.html { redirect_to visitors_url, notice: 'Visitor was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_visitor
#visitor = Visitor.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def visitor_params
params.require(:visitor).permit(:vis_id, :vis_name, :vis_email, :vis_company, :employee_id)
end
end
Now my main problem is that I cant access employee name from visitor view :
<p id="notice"><%= notice %></p>
<h1>Listing Visitors</h1>
<table>
<thead>
<tr>
<th>Vis</th>
<th>Vis name</th>
<th>Vis email</th>
<th>Vis company</th>
<th>Visitor Host</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #visitors.each do |visitor| %>
<tr>
<td><%= visitor.id %></td>
<td><%= visitor.vis_name %></td>
<td><%= visitor.vis_email %></td>
<td><%= visitor.vis_company %></td>
<td><%= visitor.employee.emp_name %></td>
<td><%= link_to 'Show', visitor %></td>
<td><%= link_to 'Edit', edit_visitor_path(visitor) %></td>
<td><%= link_to 'Destroy', visitor, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Visitor', new_visitor_path %>
I created new project from the scratch and for some reason it started working. My mistake was that I didn't define the relation from the start. I added employee_id after everything else is created I think the rails didn't build the relation at that time. thanks everyone
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.
I'm trying to get the results of all generators and results merge them into 1 table.
I have the following association :
Generator has_many: results
Result belongs_to:generators
Whenever i want to create a new results , i got an error saying couldn't find Generator without an ID. Why do i get the error ? How should i fix it ?
For example : localhost:3000/generators/new = is the part where i entered my values into the generator form and after clicking create button , it'll bring me to localhost:3000/generators/8/results/new. It is here after keying in values for this form , i get the error message saying Couldn't find Generator without an ID . I'm trying to have a page whereby i can display both Generators + Result values together. SHow all generators and their respective results data in 1 page.
GeneratorController
class GeneratorsController < ApplicationController
before_action :set_generator, only: [:show, :edit, :update, :destroy]
# GET /generators
# GET /generators.json
def index
#generators = Generator.all(:include => [:results])
end
# GET /generators/1
# GET /generators/1.json
def show
end
# GET /generators/new
def new
#generator = Generator.new
end
# GET /generators/1/edit
def edit
end
# POST /generators
# POST /generators.json
def create
#generator = Generator.new(generator_params)
#generator.choice = params[:choice]
if params[:choice] == 'Randomly'
#generator.random_generate(generator_params)
elsif params[:choice] == 'No_of_ATGC'
#generator.no_ATGC(params[:no_A],params[:no_T],params[:no_G],params[:no_C])
elsif params[:choice] == 'Seating'
#generator.seating(params[:user_seq])
end
#generator.result_choice=params[:result_choice]
respond_to do |format|
if #generator.save
if #generator.result_choice == 'Yes'
format.html { redirect_to(new_generator_result_path(#generator)) }
else
format.html { redirect_to #generator, notice: 'Result was successfully created.' }
format.json { render action: 'show', status: :created, location: #generator }
end
else
format.html { render action: 'new' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /generators/1
# PATCH/PUT /generators/1.json
def update
respond_to do |format|
if #generator.update(generator_params)
format.html { redirect_to #generator, notice: 'Generator was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# DELETE /generators/1
# DELETE /generators/1.json
def destroy
#generator.destroy
respond_to do |format|
format.html { redirect_to generators_url }
format.json { head :no_content }
end
end
private
def set_generator
#generator = Generator.find(params[:id])
end
def generator_params
params.require(:generator).permit(:primer_length,:choice,:random_primer_generated,:no_A,:no_T,:no_G,:no_C,:user_seq)
end
end
Result.rb
class Result < ActiveRecord::Base
attr_accessible :ncbi_ref_seq,:genome_seq, :genome_sample
belongs_to :generator, foreign_key: "generator_id"
def generate_result(result_params)
ref_seq = self.ncbi_ref_seq
Bio::NCBI.default_email = "spykix#hotmail.com"
fasta_sequence = Bio::NCBI::REST::EFetch.nucleotide(ref_seq,"fasta")
fasta=Bio::FastaFormat.new(fasta_sequence)
self.genome_seq = fasta.data
self.genome_sample = fasta.definition
g=Generator.last
p=self.genome_seq.scan(g.c_primer)
self.binding_times= p.length()
end
end
ResultController [ the error is highlighting at the line for def create ; generator = Generator.find(params[:id]) ]
class ResultsController < ApplicationController
before_action :set_result, only: [:show, :edit, :update, :destroy]
# GET /results
# GET /results.json
def index
#results = Result.all
end
# GET /results/1
# GET /results/1.json
def show
end
# GET /results/new
def new
#result = Result.new
end
# GET /results/1/edit
def edit
end
# POST /results
# POST /results.json
def create
#result = Result.new
#result = #result.generate_result(result_params)
generator = Generator.find(params[:id])
#result = generator.results.build(result_params)
# generator = Generator.find(3)
# #result = generator.results.build(result_params)
# #result=#result.generate_result(result_params)
respond_to do |format|
if #result.save
format.html { redirect_to #result, notice: 'Result was successfully created.' }
format.json { render action: 'show', status: :created, location: #result }
else
format.html { render action: 'new' }
format.json { render json: #result.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /results/1
# PATCH/PUT /results/1.json
def update
respond_to do |format|
if #result.update(result_params)
format.html { redirect_to #result, notice: 'Result was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #result.errors, status: :unprocessable_entity }
end
end
end
# DELETE /results/1
# DELETE /results/1.json
def destroy
#result.destroy
respond_to do |format|
format.html { redirect_to results_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_result
#result = Result.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def result_params
params.require(:result).permit(:ncbi_ref_seq)
end
end
generator/index.html.erb
<h1>[Index]Random Amplified Polymorphic DNA [RAPD] Primer Generator</h1>
<table>
<thead>
<tr>
<th>Primer length</th>
<th>Selected choice</th>
<th>Random primer generated</th>
<th>Complimentary primer</th>
<th>NCBI ref seq</th>
<th>Genome sample</th>
<th>Binding times</th>
</tr>
</thead>
<tbody>
<% #generators.each do |generator| %>
<tr>
<td><%= generator.primer_length %></td>
<td><%= generator.choice %></td>
<td><%= generator.random_primer_generated %></td>
<td><%= generator.c_primer %></td>
<% for result in generator.results %>
<td><%= result.ncbi_ref_seq %></td>
<td><%= result.genome_sample %></td>
<td><%= result.binding_times %></td>
<%end%>
<td><%= link_to 'Show', generator %></td>
<td><%= link_to 'Edit', edit_generator_path(generator) %></td>
<td><%= link_to 'Destroy', generator, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'Home', root_path %>
<%= link_to 'New Generator', new_generator_path %>
If generator have many results, than First you should find generator in new method, because when you create any record, it initializes from the new method. I am rewriting you new and create methods, you can take reference from it:
def new
generator = Generator.find(params[:id])
#result = generator.results.build(result_params)
end
# POST /results
# POST /results.json
def create
#result = #result.generate_result(result_params)
respond_to do |format|
if #result.save
format.html { redirect_to #result, notice: 'Result was successfully created.' }
format.json { render action: 'show', status: :created, location: #result }
else
format.html { render action: 'new' }
format.json { render json: #result.errors, status: :unprocessable_entity }
end
end
end
Hope this may help.
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>