I am creating a dashboard for my app but when i want to display the reviews I get the NoMethodError although i have defined the variables in the controller. I am getting the error when I view the dashboard page. Here's my code
pages_controller.rb
class PagesController < ApplicationController
before_action :authenticate_user!, only: [:dashboard]
def about
end
def help
end
def contact
end
def dashboard
#user = current_user
#places = #user.places
#reviews = #user.reviews
end
end
reviews_controller.rb
class ReviewsController < ApplicationController
before_action :set_review, only: [:edit, :update, :destroy]
before_action :set_place
before_action :authenticate_user!
# GET /reviews/new
def new
#review = Review.new
end
# GET /reviews/1/edit
def edit
end
# POST /reviews
# POST /reviews.json
def create
#review = Review.new(review_params)
#review.user_id = current_user.id
#review.place_id = #place.id
#review.save
redirect_to place_path(#place)
end
# PATCH/PUT /reviews/1
# PATCH/PUT /reviews/1.json
def update
respond_to do |format|
if #review.update(review_params)
format.html { redirect_to #review, notice: 'Review was successfully updated.' }
format.json { render :show, status: :ok, location: #review }
else
format.html { render :edit }
format.json { render json: #review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1
# DELETE /reviews/1.json
def destroy
#review.destroy
respond_to do |format|
format.html { redirect_to reviews_url, notice: 'Review was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
#review = Review.find(params[:id])
end
def set_place
unless #place = Place.where(id: params[:place_id]).first
redirect_to places_path, flash: {alert: "Place doesn't exists"}
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:comment,:rating)
end
end
places_controller.rb
class PlacesController < ApplicationController
before_action :set_place, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user! , except: [:index,:show]
# GET /places
# GET /places.json
def index
#places = Place.all
end
# GET /places/1
# GET /places/1.json
def show
#reviews = Review.where(place_id: #place.id)
end
# GET /places/new
def new
#place = Place.new
end
# GET /places/1/edit
def edit
end
# POST /places
# POST /places.json
def create
#place = Place.new(place_params)
#place.user_id = current_user.id
respond_to do |format|
if #place.save
format.html { redirect_to #place, notice: 'Place was successfully created.' }
format.json { render :show, status: :created, location: #place }
else
format.html { render :new }
format.json { render json: #place.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /places/1
# PATCH/PUT /places/1.json
def update
respond_to do |format|
if #place.update(place_params)
format.html { redirect_to #place, notice: 'Place was successfully updated.' }
format.json { render :show, status: :ok, location: #place }
else
format.html { render :edit }
format.json { render json: #place.errors, status: :unprocessable_entity }
end
end
end
# DELETE /places/1
# DELETE /places/1.json
def destroy
#place.destroy
respond_to do |format|
format.html { redirect_to places_url, notice: 'Place was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_place
#place = Place.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def place_params
params.require(:place).permit(:name, :address, :description, :phone, :website)
end
end
/pages/dashboard.html.erb
<div class="container">
<div class="row">
<div class="col-md-2">
</div>
<div class="col-md-5">
<h3>My Places</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tbody>
<% #places.each do |place| %>
<tr>
<td><%= place.name %></td>
<td><%= time_ago_in_words(place.created_at) %> ago</td>
<td><%= link_to "Edit", edit_place_path(place) %>|<%= link_to "Destroy", place_path(place), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to "New Place", new_place_path %>
</div>
<h3>My Reviews</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Place</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tbody>
<% #reviews.each do |review| %>
<tr>
<td><%= review.place.name %></td>
<td><%= time_ago_in_words(review.created_at) %> ago</td>
<td><%= link_to "Edit", edit_review_path(review) %>|<%= link_to "Destroy", review_path(review), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
Here's a screenshot :
Did anyone had this issue before ?
the problem that you are having is that the place is nil.
you can add a a try method like this
<td><%= review.place.try(:name) %></td>
The review has no associated places. So, You should implement a check before attempting to render it.
Try this:
<td><%= review.try(:place).try(:name) %></td>
Related
I've made some updates to my app, but then It can't destroy existing category.
I just want to destroy existing category with confirmation.
Here my code
index.html.erb generating list of existing categories
<table>
<thead>
<tr>
<th>Category</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #categories.each do |category| %>
<tr>
<td><%= category.category %></td>
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
categories_controller.erb actual controller i'm using
class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update, :destroy]
# GET /categories
# GET /categories.json
def index
#categories = Category.all
end
# GET /categories/1
# GET /categories/1.json
def show
end
# GET /categories/new
def new
#category = Category.new
end
# GET /categories/1/edit
def edit
end
# POST /categories
# POST /categories.json
def create
#category = Category.new(category_params)
respond_to do |format|
if #category.save
format.html { redirect_to #category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: #category }
else
format.html { render :new }
format.json { render json: #category.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /categories/1
# PATCH/PUT /categories/1.json
def update
respond_to do |format|
if #category.update(category_params)
format.html { redirect_to #category, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: #category }
else
format.html { render :edit }
format.json { render json: #category.errors, status: :unprocessable_entity }
end
end
end
# DELETE /categories/1
# DELETE /categories/1.json
def destroy
#category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_category
#category = Category.find(params[:id])
end
# Only allow a list of trusted parameters through.
def category_params
params.require(:category).permit(:category)
end
end
model category.rb and the model containing has and belongs to many
class Category < ApplicationRecord
has_and_belongs_to_many :blogs
end
Any ideas?
Thank you in advance
Hi I have a simple rails application that has two models Equipment_Types & Tasks
Each equipment type has many tasks. When I export to csv file I would like to export the name of the equipment type, the task name and the associated schedule. Basically every thing that is displayed on equipment_type/show.html.erb
<p id="notice"><%= notice %></p>
<div class="row">
<table class="table table-bordered">
<thead>
<tr>
<th class="align-middle" rowspan="2">Equipment</th>
<th class="align-middle" rowspan="2">Task</th>
<th class="text-center" colspan="4">Frequency</th>
</tr>
<tr>
<th class="text-center">M</th>
<th class="text-center">Q</th>
<th class="text-center">B</th>
<th class="text-center">A</th>
</tr>
</thead>
<tbody>
<tr>
<% if #taskcount > 3 %>
<td class="text-center bottomtotop" rowspan="0"><%= link_to #equipment_type.name, edit_equipment_type_path(#equipment_type) %></td>
<% else %>
<td class="text-center" rowspan="0"><%= link_to #equipment_type.name, edit_equipment_type_path(#equipment_type) %></td>
<% end %>
</tr>
<% #equip_tasks.each do |task| %>
<tr>
<td><%= link_to task.name, task %></td>
<td class="text-center"><%= if task.monthly then 'x' else ' ' end %></td>
<td class="text-center"><%= if task.quarterly then 'x' else ' ' end %></td>
<td class="text-center"><%= if task.sixmonthly then 'x' else ' ' end %></td>
<td class="text-center"><%= if task.annually then 'x' else ' ' end %></td>
</tr>
<% end %>
<div class="collapse" id="collapsenewline">
</div>
</tbody>
</table>
</div>
<div class="row">
<div class="col-sm">
<%= link_to 'Add Task', new_task_path(#equipment_type), class: 'btn btn-dark' %>
</div>
<div class="col-sm">
<%= link_to 'Back', equipment_types_path, class: 'btn btn-dark' %>
</div>
<div class="col-sm">
<button class="btn btn-dark" type="button" data-toggle="collapse" data-target="#collapsedownload" aria-expanded="false" aria-controls="collapseExample">
Download
</button>
</div>
</div>
<div class="row">
<div class="col-sm">
</div>
<div class="col-sm">
</div>
<div class="col-sm">
<div class="collapse" id="collapsedownload">
<div class="card card-body">
<h3>Download File</h3>
<%= link_to "csv", equipment_types_path(format: "csv"), class: 'btn btn-dark' %>
<br>
<%= link_to "pdf", equipment_types_path(format: "pdf"), class: 'btn btn-dark' %>
<br>
<%= link_to "word", equipment_types_path(format: "word"), class: 'btn btn-dark' %>
</div>
</div>
</div>
</div>
equipment_type Model
class EquipmentType < ApplicationRecord
has_many :tasks
accepts_nested_attributes_for :tasks
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
EquipmentType.create! row.to_hash
end
end
def self.to_csv
CSV.generate do |csv|
csv << column_names
all.each do |equipmenttype|
csv << equipmenttype.attributes.values_at(*column_names)
end
end
end
end
equipment_types Controller
class EquipmentTypesController < ApplicationController
before_action :set_equipment_type, only: [:show, :edit, :update, :destroy]
before_action :set_task, only: [:show]
# GET /equipment_types
# GET /equipment_types.json
def index
#equipment_types = EquipmentType.all
respond_to do |format|
format.html
format.csv { send_data #equipment_types.to_csv }
end
end
# GET /equipment_types/1
# GET /equipment_types/1.json
def show
#equipment_type = EquipmentType.find_by(id: params[:id])
#tasks = Task.all
#equip_tasks = #equipment_type.tasks.all
#taskcount = #equip_tasks.count
respond_to do |format|
format.html
format.csv { send_data text: #equip_tasks.to_csv }
end
end
# GET /equipment_types/new
def new
#equipment_type = EquipmentType.new
end
# GET /equipment_types/1/edit
def edit
end
# POST /equipment_types
# POST /equipment_types.json
def create
#equipment_type = EquipmentType.new(equipment_type_params)
respond_to do |format|
if #equipment_type.save
format.html { redirect_to #equipment_type, notice: 'Equipment type was successfully created.' }
format.json { render :show, status: :created, location: #equipment_type }
else
format.html { render :new }
format.json { render json: #equipment_type.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /equipment_types/1
# PATCH/PUT /equipment_types/1.json
def update
respond_to do |format|
if #equipment_type.update(equipment_type_params)
format.html { redirect_to #equipment_type, notice: 'Equipment type was successfully updated.' }
format.json { render :show, status: :ok, location: #equipment_type }
else
format.html { render :edit }
format.json { render json: #equipment_type.errors, status: :unprocessable_entity }
end
end
end
def import
EquipmentType.import(params[:file])
redirect_to equipment_type_path, notice: "Equipment Type Added Successfully"
end
# DELETE /equipment_types/1
# DELETE /equipment_types/1.json
def destroy
#equipment_type.destroy
respond_to do |format|
format.html { redirect_to equipment_types_url, notice: 'Equipment type was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_equipment_type
#equipment_type = EquipmentType.find(params[:id])
end
def set_task
#task = Task.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def equipment_type_params
params.require(:equipment_type).permit(:name, task: [])
end
end
Task Model
class Task < ApplicationRecord
belongs_to :equipment_type
end
Tasks Controller
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
# GET /tasks
# GET /tasks.json
def index
#tasks = Task.all
end
# GET /tasks/1
# GET /tasks/1.json
def show
end
# GET /tasks/new
def new
#task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks
# POST /tasks.json
def create
#task = Task.new(task_params)
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: 'Task was successfully created.' }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1
# PATCH/PUT /tasks/1.json
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to #task, notice: 'Task was successfully updated.' }
format.json { render :show, status: :ok, location: #task }
else
format.html { render :edit }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1
# DELETE /tasks/1.json
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
#task = Task.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def task_params
params.require(:task).permit(:name, :monthly, :quarterly, :sixmonthly, :annually, :equipment_type_id)
end
end
This should get you started:
You merely need to just append the attributes to the array.
Instead of,
csv << equipmenttype.attributes.values_at(*column_names)
Drop in this (or similar) (note: this is resource-intensive so you'll want to trim it down especially if you have lots of tasks)
def csv_attributes
{
tasks: self.tasks.all.map(&:attributes).map(&:values),
id: self.id
}
end
and then
csv << equipmenttype.csv_attributes
I was trying to show the student name on the submission table but I don't know how to do it. If you can please help me!
Form.rb:
class Form < ActiveRecord::Base
belongs_to :user
has_many :submissions, :dependent => :destroy
has_attached_file :image, styles: { medium: "400x600>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
Submission.rb:
class Submission < ActiveRecord::Base
belongs_to :form
has_many :submissionstudent
has_many :students, :through => :submissionstudent
end
Student.rb:
class Student < ActiveRecord::Base
has_many :submissionstudent
has_many :submissions, :through => :submissionstudent
end
Joint Table: Submissionstudent:
class Submissionstudent < ActiveRecord::Base
belongs_to :submission
belongs_to :student
end
Show Table:
<h1><%= #form.title %></h1>
<p>
<%= image_tag #form.image.url(:medium) %>
</p>
<table class="table table-responsive table-hover">
<% if user_signed_in? %>
<% if #submissions.blank? %>
<h4>No submission just yet</h4>
<% else %>
<thead>
<th>Conflict</th>
<th>Computer</th>
<th>Extra time</th>
<th>AM or PM</th>
</thead>
<tbody>
<% #submissions.each do |submission| %>
<tr>
<td><%= submission.conflict %></td>
<td><%= submission.computer %></td>
<td><%= submission.extra_time %>%</td>
<td><%= submission.am_pm %></td>
<!-- Need to add Edit, Delete -->
</tr>
<% end %>
</tbody>
<% end %>
<% end %>
</table>
<%= link_to 'New Submission', new_form_submission_path(#form) %>
<br>
<%= link_to 'Edit', edit_form_path(#form) %> |
<%= link_to 'Back', forms_path(#form) %>
Submission Controller:
class SubmissionsController < ApplicationController
before_action :set_submission, only: [:show, :edit, :update, :destroy]
before_action :set_form
# GET /submissions/new
def new
#submission = Submission.new
#all_students = Student.all
#submission_student = #submission.submissionstudent.build
end
# GET /submissions/1/edit
def edit
end
# POST /submissions
# POST /submissions.json
def create
#submission = Submission.new(submission_params)
#submission.form_id = #form.id
respond_to do |format|
if #submission.save
format.html { redirect_to #form, notice: 'Submission was successfully created.' }
format.json { render :show, status: :created, location: #submission }
else
format.html { render :new }
format.json { render json: #submission.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /submissions/1
# PATCH/PUT /submissions/1.json
def update
respond_to do |format|
if #submission.update(submission_params)
format.html { redirect_to #submission, notice: 'Submission was successfully updated.' }
format.json { render :show, status: :ok, location: #submission }
else
format.html { render :edit }
format.json { render json: #submission.errors, status: :unprocessable_entity }
end
end
end
# DELETE /submissions/1
# DELETE /submissions/1.json
def destroy
#submission.destroy
respond_to do |format|
format.html { redirect_to submissions_url, notice: 'Submission was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_submission
#submission = Submission.find(params[:id])
end
def set_form
#form = Form.find(params[:form_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def submission_params
params.require(:submission).permit(:conflict, :computer, :extra_time, :am_pm)
end
end
Form Controller:
class FormsController < ApplicationController
before_action :set_form, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
#forms = Form.all
end
def show
#submissions = Submission.where(form_id: #form.id).order("conflict DESC")
#student = Student.find params[:id]
end
def new
#form = current_user.forms.build
end
def edit
end
def create
#form = current_user.forms.build(form_params)
respond_to do |format|
if #form.save
format.html { redirect_to #form, notice: 'Form was successfully created.' }
format.json { render :show, status: :created, location: #form }
else
format.html { render :new }
format.json { render json: #form.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /forms/1
# PATCH/PUT /forms/1.json
def update
respond_to do |format|
if #form.update(form_params)
format.html { redirect_to #form, notice: 'Form was successfully updated.' }
format.json { render :show, status: :ok, location: #form }
else
format.html { render :edit }
format.json { render json: #form.errors, status: :unprocessable_entity }
end
end
end
# DELETE /forms/1
# DELETE /forms/1.json
def destroy
#form.destroy
respond_to do |format|
format.html { redirect_to forms_url, notice: 'Form was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_form
#form = Form.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def form_params
params.require(:form).permit(:title, :image)
end
end
Student Controller:
class StudentsController < ApplicationController
before_action :set_student, only: [:show, :edit, :update, :destroy]
# GET /students
# GET /students.json
def index
#students = Student.all
end
# GET /students/1
# GET /students/1.json
def show
end
# GET /students/new
def new
#student = Student.new
end
# GET /students/1/edit
def edit
end
# POST /students
# POST /students.json
def create
#student = Student.new(student_params)
respond_to do |format|
if #student.save
format.html { redirect_to #student, notice: 'Student was successfully created.' }
format.json { render :show, status: :created, location: #student }
else
format.html { render :new }
format.json { render json: #student.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /students/1
# PATCH/PUT /students/1.json
def update
respond_to do |format|
if #student.update(student_params)
format.html { redirect_to #student, notice: 'Student was successfully updated.' }
format.json { render :show, status: :ok, location: #student }
else
format.html { render :edit }
format.json { render json: #student.errors, status: :unprocessable_entity }
end
end
end
# DELETE /students/1
# DELETE /students/1.json
def destroy
#student.destroy
respond_to do |format|
format.html { redirect_to students_url, notice: 'Student was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_student
#student = Student.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def student_params
params.require(:student).permit(:name)
end
end
If you need something else just comment and I'll provide it
Thanks for your help.
Short answer is:
...
<% #submissions.each do |submission| %>
<tr>
<td><%= submission.conflict %></td>
<td><%= submission.computer %></td>
<td><%= submission.extra_time %>%</td>
<td><%= submission.am_pm %></td>
<td><%= submission.students.first.name %></td>
</tr>
<% end %>
...
Or to not get error if submission do not have a student
<td><%= submission.students.first.try(:name) %></td>
Long answer is to change the assosiation and only add a column to submissions to link a student (student_id) and delete the jointable (submissionstudent) because you always have one student per submission.
EDIT:
to show all student's name you can do some like this
<td><%= submission.students.pluck(:name).join(' - ') %></td>
or if you need more you can iterate over students
<% #submissions.each do |submission| %>
<tr>
<td><%= submission.conflict %></td>
<td><%= submission.computer %></td>
<td><%= submission.extra_time %>%</td>
<td><%= submission.am_pm %></td>
<td>
<% submission.students.each do |ss| %>
<%= ss.name %> - <%= ss.last_name %>
<% end %>
</td>
</tr>
<% 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'm using paperclip & Rails 4 and am getting this error when accessing my galleries index page.
Here is the error:
The code I have for the index page is this:
<h3> for <%= Property.name %></h3>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #galleries.property.each do |gallery| %>
<tr>
<td><%= link_to 'Show', property_gallery_path(params[:property_id], gallery) %></td>
<td><%= link_to 'Edit', edit_property_gallery_path(params[:property_id], gallery) %></td>
<td><%= link_to 'Destroy', property_gallery_path(params[:property_id], gallery), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
And my galleries controller page is this
class GalleriesController < ApplicationController
before_action :set_imageable
before_action :set_property, only: [:show, :edit, :update, :destroy]
before_action :set_gallery, only: [:show, :edit, :update, :destroy]
# GET /galleries
# GET /galleries.json
def index
#galleries = Gallery.all
end
# GET /galleries/1
# GET /galleries/1.json
def show
end
# GET /galleries/new
def new
#gallery = Gallery.new
end
# GET /galleries/1/edit
def edit
end
# POST /galleries
# POST /galleries.json
def create
#gallery = #imageable.galleries.new(gallery_params)
respond_to do |format|
if #gallery.save
if params[:images]
params[:images].each { |image|
#gallery.pictures.create(image: image)
}
end
format.html { redirect_to #imageable, notice: 'Gallery was successfully created.' }
format.json { render :show, status: :created, location: #gallery }
else
format.html { render :new }
format.json { render json: #gallery.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /galleries/1
# PATCH/PUT /galleries/1.json
def update
respond_to do |format|
if #gallery.update(gallery_params)
format.html { redirect_to property_gallery_path, notice: 'Gallery was successfully updated.' }
format.json { render :show, status: :ok, location: #gallery }
else
format.html { render :edit }
format.json { render json: #gallery.errors, status: :unprocessable_entity }
end
end
end
# DELETE /galleries/1
# DELETE /galleries/1.json
def destroy
#gallery.destroy
respond_to do |format|
format.html { redirect_to galleries_url, notice: 'Gallery was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_gallery
#gallery = Gallery.find(params[:id])
end
def set_property
#property = Property.find(params[:id])
end
def set_imageable
klass = [Property, Space].detect { |c| params["#{c.name.underscore}_id"]}
puts "Klass is #{klass.inspect}"
#imageable = klass.find(params["#{klass.name.underscore}_id"])
puts "Imageable is #{#imageable.inspect}"
end
# Never trust parameters from the scary internet, only allow the white list through.
def gallery_params
params.permit(:gallery)
end
end
Now after thinking about it, I went to my Galleries controller and looked under the index line. There, there’s a #galleries = Gallery.all, which of course is pulling all the galleries relevant to the property. I know that it’s pulling all the galleries relevant to the property because I have the before_action :set_property and the before_action :set_gallery.
I’m wondering if the no method error has to do with how the property is being set. I’m not sure. But my property_id is being set as “1” in my request parameters. So the value is passing along.
Any thing obvious I’m missing? And and I generally thinking about this the right way?
Here's my entire galleries_controller.rb
class GalleriesController < ApplicationController
before_action :set_imageable
before_action :set_property, only: [:index, :show, :edit, :update, :destroy]
before_action :set_gallery, only: [:show, :edit, :update, :destroy]
# GET /galleries
# GET /galleries.json
def index
#galleries = #property.galleries
end
# GET /galleries/1
# GET /galleries/1.json
def show
end
# GET /galleries/new
def new
#gallery = Gallery.new
end
# GET /galleries/1/edit
def edit
end
# POST /galleries
# POST /galleries.json
def create
#gallery = #imageable.galleries.new(gallery_params)
respond_to do |format|
if #gallery.save
if params[:images]
params[:images].each { |image|
#gallery.pictures.create(image: image)
}
end
format.html { redirect_to #imageable, notice: 'Gallery was successfully created.' }
format.json { render :show, status: :created, location: #gallery }
else
format.html { render :new }
format.json { render json: #gallery.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /galleries/1
# PATCH/PUT /galleries/1.json
def update
respond_to do |format|
if #gallery.update(gallery_params)
format.html { redirect_to property_gallery_path, notice: 'Gallery was successfully updated.' }
format.json { render :show, status: :ok, location: #gallery }
else
format.html { render :edit }
format.json { render json: #gallery.errors, status: :unprocessable_entity }
end
end
end
# DELETE /galleries/1
# DELETE /galleries/1.json
def destroy
#gallery.destroy
respond_to do |format|
format.html { redirect_to galleries_url, notice: 'Gallery was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_gallery
#gallery = Gallery.find(params[:id])
end
def set_property
#property = Property.find(params[:id])
end
def set_imageable
klass = [Property, Space].detect { |c| params["#{c.name.underscore}_id"]}
puts "Klass is #{klass.inspect}"
#imageable = klass.find(params["#{klass.name.underscore}_id"])
puts "Imageable is #{#imageable.inspect}"
end
# Never trust parameters from the scary internet, only allow the white list through.
def gallery_params
params.permit(:gallery)
end
end
first of all
#galleries = Gallery.all
will return you all galleries irrespective of property
if you want to fetch galleries for any property do this
#galleries = #property.galleries
secondly you cannot fetch single property from galleries as galleries is an array which you are doing here
<% #galleries.property.each do |gallery| %>
you need to do
<% #galleries.each do |gallery| %>
<%property = gallery.property %>
#what you want to do
<% end %>
also update your before_action
before_action :set_property, only: [:index, :show, :edit, :update, :destroy]
and change this
def set_property
#property = Property.find(params[:id])
end
to
def set_property
#property = Property.find(params[:property_id])
end
#galleries is plural - basically an array of gallery objects, so you can't call a method directly on it. You need to iterate through each, and then call the method on the individual object.
You can achieve the similar thing in the following fashion:
<tbody>
<% #galleries.each do |gallery| %>
<tr>
<td><%= link_to 'Show', property_gallery_path(params[:property_id], gallery) %></td>
<td><%= link_to 'Edit', edit_property_gallery_path(params[:property_id], gallery) %></td>
<td><%= link_to 'Destroy', property_gallery_path(params[:property_id], gallery), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>