I get for what is this error, but I can't see a mistake in my code.
A controller is plural, a model is singular and the table name is plural.
Error on visiting index:
NameError at /admin/custom_communities uninitialized constant
Admin::CustomCommunitiesController::CustomCommunity
Generated controller: (file: controllers/admin/custom_communities_controller.rb)
# frozen_string_literal: true
class Admin::CustomCommunitiesController < Admin::BaseController
before_action :set_custom_community, only: [:show, :edit, :update, :destroy]
def index
#custom_communities = CustomCommunity.page(params[:page])
end
def show; end
def new
#custom_community = CustomCommunity.new
end
def edit; end
def create
#custom_community = CustomCommunity.new(custom_community_params)
respond_to do |format|
if #custom_community.save
format.html { redirect_to #custom_community, notice: "Custom community was successfully created." }
format.json { render :show, status: :created, location: #custom_community }
else
format.html { render :new }
format.json { render json: #custom_community.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #custom_community.update(custom_community_params)
format.html { redirect_to #custom_community, notice: "Custom community was successfully updated." }
format.json { render :show, status: :ok, location: #custom_community }
else
format.html { render :edit }
format.json { render json: #custom_community.errors, status: :unprocessable_entity }
end
end
end
def destroy
#custom_community.destroy
respond_to do |format|
format.html { redirect_to admin_custom_communities_url, notice: "Custom community was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_custom_community
#custom_community = CustomCommunity.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def custom_community_params
params.require(:custom_community).permit(:name, :description, :picture, :should_delete_picture)
end
end
Model: (file: models/custom_community.rb)
# frozen_string_literal: true
class CustomCommunity < ApplicationRecord
end
Migration:
class CreateCustomCommunities < ActiveRecord::Migration[5.2]
def change
create_table :custom_communities do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
Routes:
in admin routes:
resources :custom_communities
I'm sure someone else can explain why, but there are times rails will think that the following class in the index method:
class Admin::CustomCommunitiesController < Admin::BaseController
def index
CustomCommunity.all
end
end
Is referencing a class defined within the Admin::CustomCommunitiesController, i.e. it thinks you're trying to call Admin::CustomCommunitiesController::CustomCommunity.all. To 'unnamespace' the class, try:
# frozen_string_literal: true
class Admin::CustomCommunitiesController < Admin::BaseController
before_action :set_custom_community, only: [:show, :edit, :update, :destroy]
def index
#custom_communities = ::CustomCommunity.page(params[:page])
end
def show; end
def new
#custom_community = ::CustomCommunity.new
end
def edit; end
def create
#custom_community = ::CustomCommunity.new(custom_community_params)
respond_to do |format|
if #custom_community.save
format.html { redirect_to #custom_community, notice: "Custom community was successfully created." }
format.json { render :show, status: :created, location: #custom_community }
else
format.html { render :new }
format.json { render json: #custom_community.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #custom_community.update(custom_community_params)
format.html { redirect_to #custom_community, notice: "Custom community was successfully updated." }
format.json { render :show, status: :ok, location: #custom_community }
else
format.html { render :edit }
format.json { render json: #custom_community.errors, status: :unprocessable_entity }
end
end
end
def destroy
#custom_community.destroy
respond_to do |format|
format.html { redirect_to admin_custom_communities_url, notice: "Custom community was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_custom_community
#custom_community = ::CustomCommunity.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def custom_community_params
params.require(:custom_community).permit(:name, :description, :picture, :should_delete_picture)
end
end
I think that's all of them. Basically switch every CustomCommunity class call to ::CustomCommunity to unnamespace it.
Related
I'm new to Ruby on Rails and would appreciate any support!
Users can create a case and select a specific diagnosis via dropdown. The Admin (called 'rki') can see a list of all diagnoses in the database. Now I'm trying to implement that the admin can choose a specific diagnosis und get a list of all cases, with that diagnosis.
This is my RkisController
class RkisController < ApplicationController
before_action :authenticate_user!
before_action :current_user_rki?
def current_user_rki?
return if current_user.role == 'rki'
redirect_to root_path
end
def index
#diagnoses = Diagnosis.all
end
def all_cases
#show all cases with a certain diagnosis
end
end
And this is my Model for Case
class Case < ApplicationRecord
belongs_to :user
belongs_to :diagnosis
belongs_to :district
end
Diagnosis
class Diagnosis < ApplicationRecord
has_many :cases
end
CasesController
class CasesController < ApplicationController
before_action :set_case, only: [:show, :edit, :update, :destroy, :confirm]
def index
#cases = current_user.cases
end
def show
end
def new
#case = Case.new
#case.user_id = current_user.id
end
def edit
end
def create
#case = Case.new(case_params) do |c|
c.user_id = current_user.id
end
#case.district = current_user.district
respond_to do |format|
if #case.save
format.html { redirect_to #case, notice: 'Case was successfully created.' }
format.json { render :show, status: :created, location: #case }
else
format.html { render :new }
format.json { render json: #case.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #case.update(case_params)
format.html { redirect_to #case, notice: 'Case was successfully updated.' }
format.json { render :show, status: :ok, location: #case }
else
format.html { render :edit }
format.json { render json: #case.errors, status: :unprocessable_entity }
end
end
end
def destroy
#case.destroy
respond_to do |format|
format.html { redirect_to cases_url, notice: 'Case was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def allowed_to_create
redirect_to root_path unless current_user.role.in?(['arzt', 'labor'])
end
def set_case
#case = Case.find(params[:id])
end
def case_params
params.require(:case).permit(:first_name, :last_name, :gender,:birthdate, :place_of_residence,
:diagnosis_id, :user_id, :case_id, :confirmed_at, :district_id)
end
end
DiagnosisController
class DiagnosesController < ApplicationController
before_action :set_diagnosis, only: [:show, :edit, :update, :destroy]
def index
#diagnoses = Diagnosis.all
end
def show
end
def new
#diagnosis = Diagnosis.new
end
def edit
end
def create
#diagnosis = Diagnosis.new(diagnosis_params)
respond_to do |format|
if #diagnosis.save
format.html { redirect_to #diagnosis, notice: 'Diagnosis was successfully created.' }
format.json { render :show, status: :created, location: #diagnosis }
else
format.html { render :new }
format.json { render json: #diagnosis.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #diagnosis.update(diagnosis_params)
format.html { redirect_to #diagnosis, notice: 'Diagnosis was successfully updated.' }
format.json { render :show, status: :ok, location: #diagnosis }
else
format.html { render :edit }
format.json { render json: #diagnosis.errors, status: :unprocessable_entity }
end
end
end
def destroy
#diagnosis.destroy
respond_to do |format|
format.html { redirect_to diagnoses_url, notice: 'Diagnosis was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_diagnosis
#diagnosis = Diagnosis.find(params[:id])
end
def diagnosis_params
params.require(:diagnosis).permit(:illness)
end
end
Thank you very much in advance.
Are you wanting to go to a show page from the index of diagnosis? If so you can just use the id from the index, passed to the show, ie normal flow. And then in your diagnosis show action you can have
def show
#diagnosis = Diagnosis.includes(:cases).find_by(id: params[:id])
end
And then if using erb you can iterate through the cases
<% #diagnosis.cases.each do |case| %>
<%= case.name %>
<% end %>
i have ProjectSite model and ManagerRemark model related to many to one association. my MangerRemark model has boolean value true and false i want to access that boolean value to other controller view. please help. here is my code.i want to print decision boolean value next to each project site index list how can i do that? in other controller name new_manager_controller view
project_sites_controller.rb
class ProjectSitesController < ApplicationController
before_action :authenticate_user!
before_action :is_project_site?, except: [:show]
before_action :set_project_site, only: [:show, :edit, :update, :destroy]
# GET /project_sites
# GET /project_sites.json
def index
#project_sites = ProjectSite.all.order("created_at DESC")
end
# GET /project_sites/1
# GET /project_sites/1.json
def show
#manager_remark = ManagerRemark.new
#manager_remark.project_site_id = #project_site.id
end
# GET /project_sites/new
def new
#project_site = ProjectSite.new
end
# GET /project_sites/1/edit
def edit
end
# POST /project_sites
# POST /project_sites.json
def create
#project_site = ProjectSite.new(project_site_params)
respond_to do |format|
if #project_site.save
format.html { redirect_to #project_site, notice: 'Project site was successfully created.' }
format.json { render :show, status: :created, location: #project_site }
else
format.html { render :new }
format.json { render json: #project_site.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /project_sites/1
# PATCH/PUT /project_sites/1.json
def update
respond_to do |format|
if #project_site.update(project_site_params)
format.html { redirect_to #project_site, notice: 'Project site was successfully updated.' }
format.json { render :show, status: :ok, location: #project_site }
else
format.html { render :edit }
format.json { render json: #project_site.errors, status: :unprocessable_entity }
end
end
end
# DELETE /project_sites/1
# DELETE /project_sites/1.json
def destroy
#project_site.destroy
respond_to do |format|
format.html { redirect_to project_sites_url, notice: 'Project site was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project_site
#project_site = ProjectSite.find(params[:id])
end
# Never trust parameters frmanager_level_twoom the scary internet, only allow the white list through.
def project_site_params
params.require(:project_site).permit(:name, :date, :file)
end
def is_project_site?
redirect_to root_path unless (current_user.role=='project_site')
end
end
This is how my manage remark controller looks.
Manager_Remarks_controller.rb
class ManagerRemarksController < ApplicationController
def create
#manager_remark = ManagerRemark.new(remark_params)
#manager_remark.project_site_id = params[:project_site_id]
#manager_remark.save
redirect_to project_site_path(#manager_remark.project_site)
end
def remark_params
params.require(:manager_remark).permit(:name, :remark, :decision)
end
end
Hello I am trying to store a PDF document in a PostgreSQL database using Ruby Rails.
Currently my code looks like this:
DB File:
'$20151126021922_create_pdf_creates.rb'
class CreatePdfCreates < ActiveRecord::Migration
def change
create_table :pdf_creates do |t|
t.binary :pdfload
t.timestamps null: false
end
end
end
Model:
'$pdf_create.rb'
class PdfCreate < ActiveRecord::Base
end
Controller:
'$pdf_creates_controller.rb'
class PdfCreatesController < ApplicationController
before_action :set_pdf_create, only: [:show, :edit, :update, :destroy]
# GET /pdf_creates
# GET /pdf_creates.json
def index
#pdf_creates = PdfCreate.all
end
# GET /pdf_creates/1
# GET /pdf_creates/1.json
def show
end
# GET /pdf_creates/new
def new
#pdf_create = PdfCreate.new
end
# GET /pdf_creates/1/edit
def edit
end
# POST /pdf_creates
# POST /pdf_creates.json
def newpdf
#pdf_create = PdfCreate.new(pdf_create_params)
respond_to do |format|
if #pdf_create.save
format.html { redirect_to #pdf_create, notice: 'Pdf create was successfully created.' }
format.json { render :show, status: :created, location: #pdf_create }
else
format.html { render :new }
format.json { render json: #pdf_create.errors, status: :unprocessable_entity }
end
end
end
def create
#pdf_create = PdfCreate.new(pdf_create_params)
#data = File.read(Rails.root + "tmp/consent(1).pdf")
#Document.create pdfload: data
respond_to do |format|
if #pdf_create.save
format.html { redirect_to #pdf_create, notice: 'Pdf create was successfully created.' }
format.json { render :show, status: :created, location: #pdf_create }
format.pdf { send_data #pdf_create.render}
else
format.html { render :new }
# format.json { render json: #pdf_create.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /pdf_creates/1
# PATCH/PUT /pdf_creates/1.json
def update
respond_to do |format|
if #pdf_create.update(pdf_create_params)
format.html { redirect_to #pdf_create, notice: 'Pdf create was successfully updated.' }
format.json { render :show, status: :ok, location: #pdf_create }
else
format.html { render :edit }
format.json { render json: #pdf_create.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pdf_creates/1
# DELETE /pdf_creates/1.json
def destroy
#pdf_create.destroy
respond_to do |format|
format.html { redirect_to pdf_creates_url, notice: 'Pdf create was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pdf_create
#pdf_create = PdfCreate.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def pdf_create_params
params.require(:pdf_create).permit(:pdfload)
end
end
I send a pdf form and the result that is returned is me:
{"id":5,"pdfload":null,"created_at":"2015-11-26T03:24:37.457Z","updated_at":"2015-11-26T03:24:37.457Z"}
What is wrong? Tks
You're sending the whole newly created record, not the PDF data stored in it.
Try changing this:
format.pdf { send_data #pdf_create.render}
to this:
format.pdf { send_data #pdf_create.pdfload}
I've used this instructions for simply add score when a user creates a "solucion" (which is a kind of "answer" to a micropost). I have added the has_merit line to user.rb (user model).
I want to display the user points earned for that action at the show view.
show.html.erb (for solucion):
<h2><span class="red"><%= current_user.points %></span><br>Points</br></h2>
It displays 0 points...
point_rules.rb:
module Merit
class PointRules
include Merit::PointRulesMethods
def initialize
score 5, on: 'solucions#create'
end
end
end
When I create a solucion with the current_user (already saving the user_id index and identifier to solucion), This is what my rails server output shows...
Direct link to github gist:
https://gist.github.com/roadev/7b34fd67ab93c979fa48
Embed:
<script src="https://gist.github.com/roadev/7b34fd67ab93c979fa48.js"></script>
EDIT:
solucions_micropost.rb
class SolucionsController < ApplicationController
before_action :set_solucion, only: [:show, :edit, :update, :destroy]
def index
#solucions = Solucion.all
end
def show
end
def new
#solucion = current_user.solucions.build
end
def edit
end
def create
#solucion = current_user.solucions.build(solucion_params)
respond_to do |format|
if #solucion.save
format.html { redirect_to #solucion, notice: 'Solucion was successfully created.' }
format.json { render action: 'show', status: :created, location: #solucion }
else
format.html { render action: 'new' }
format.json { render json: #solucion.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #solucion.update(solucion_params)
format.html { redirect_to #solucion, notice: 'Solucion was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #solucion.errors, status: :unprocessable_entity }
end
end
end
def destroy
#solucion.destroy
respond_to do |format|
format.html { redirect_to solucions_url }
format.json { head :no_content }
end
end
private
def set_solucion
#solucion = Solucion.find(params[:id])
end
def current_micropost
#solucion = microposts.find_by(id: params[:id])
end
def solucion_params
params.require(:solucion).permit(:solucion, :image, :micropost_id)
end
end
user.rb:
class User < ActiveRecord::Base
has_many :dreams
has_many :microposts
has_many :solucions
has_merit
end
I had a problem with a migration when I installed the merit gem.
I have added a module to my application called Tokened.rb and added the include Tokened to my model. However, now when I try to load that model, I get a "NameError in TestingsController#index" error... I haven't included Tokened in my TestingsController, but not sure why I should or where I should put it.
My code:
testing.rb
class Testing < ActiveRecord::Base
include Tokened
end
My Tokened.rb module:
module Tokened
extend ActiveSupport::Concern
included do
after_initialize do
self.token = generate_token if self.token.blank?
end
end
private
def generate_token
loop do
key = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
break key unless self.class.find_by(token: key)
end
end
end
Finally, my testing controller:
class TestingsController < ApplicationController
before_action :set_testing, only: [:show, :edit, :update, :destroy]
# GET /testings
# GET /testings.json
def index
#testings = Testing.all
end
# GET /testings/1
# GET /testings/1.json
def show
end
# GET /testings/new
def new
#testing = Testing.new
end
# GET /testings/1/edit
def edit
end
# POST /testings
# POST /testings.json
def create
#testing = Testing.new(testing_params)
respond_to do |format|
if #testing.save
format.html { redirect_to #testing, notice: 'Testing was successfully created.' }
format.json { render :show, status: :created, location: #testing }
else
format.html { render :new }
format.json { render json: #testing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /testings/1
# PATCH/PUT /testings/1.json
def update
respond_to do |format|
if #testing.update(testing_params)
format.html { redirect_to #testing, notice: 'Testing was successfully updated.' }
format.json { render :show, status: :ok, location: #testing }
else
format.html { render :edit }
format.json { render json: #testing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /testings/1
# DELETE /testings/1.json
def destroy
#testing.destroy
respond_to do |format|
format.html { redirect_to testings_url, notice: 'Testing was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_testing
#testing = Testing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def testing_params
params.require(:testing).permit(:name, :address, :signature)
end
end
What gives? I am not sure what is going on here and why it needs to be included in the controller.
First of all it should be lowercased: tokened.rb. But where is your file? It should be here modules/concerns/tokened.rb.