No Method Error - Getting Name from Table - ruby-on-rails

I have an agent and an agent_review model and I am trying to create a page where all of the reviews are accessible on the same page. I am having an issue pulling out the agent who is being reviewed and the reviewer's name.
ERB:
<div class="mega-container">
<%= render 'matrix/listings/matrix_navigation' %>
<div class="standard-page-header">
<h1>Agent Reviews</h1><br>
<hr>
</div>
<div class="text-center">
<div class="container feedback-index">
<% #agent_reviews.each do |agent_review| %>
<div class="row feedback-strip">
<h3>Agent Name: <%= agent_review.agent.name %></h3>
<p>Review: <%= agent_review.comment %></p>
<p>Star Rating: <%= agent_review.rating %> of 5</p>
<p>Reviewer: <%= agent_review.reviewer.name %></p>
<p>Submitted: <%= agent_review.created_at.strftime('%D # %l:%M%p') %></p>
</div>
<% end %>
</div>
</div>
</div>
Schema:
create_table "agent_reviews", force: :cascade do |t|
t.integer "agent_id"
t.integer "reviewer_id"
t.text "comment"
t.integer "rating"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Controller:
def all_reviews
#agent_reviews = AgentReview.all
end
Models:
class Agent < ActiveRecord::Base
has_many :agent_reviews
end
class AgentReview < ActiveRecord::Base
belongs_to :agent, foreign_key: 'reviewer_id'
end

In your AgentReview model:
belongs_to :reviewer, :class_name => 'Agent', :foreign_key => 'reviewer_id'
belongs_to :agent, :class_name => 'Agent', :foreign_key => 'agent_id'

It looks like you're missing an association on AgentReview
class AgentReview < ActiveRecord::Base
belongs_to :reviewer, class_name: "Agent", foreign_key: 'reviewer_id'
belongs_to :agent
end

Related

Can't save data from collection_select helper to model

I'm trying to save an organization to a contact model via a form using a collection_select helper but it's not saving the data to a model. I can't seem to understand why.
I am using Rails 5.1.4
Here are my contact.rb model:
class Contact < ApplicationRecord
belongs_to :organization
has_many :deals
end
Here is my organization.rb model:
class Organization < ApplicationRecord
has_many :deals, through: :contacts
has_many :contacts, dependent: :destroy
end
Here is my schema.rb:
create_table "contacts", force: :cascade do |t|
t.string "contact_name"
t.integer "organization_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["organization_id"], name:
"index_contacts_on_organization_id"
end
create_table "organizations", force: :cascade do |t|
t.string "org_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Here is my contacts_controller.rb:
def new
#contact = Contact.new
end
def edit
#contact = Contact.find(params[:id])
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to #contact
else
render 'new'
end
end
private
def contact_params
params.require(:contact).permit(:contact_name, :organization_id,
organizations_attributes: [:org_name,
:id])
end
Here is my new.html.erb file:
<h1>New Contact</h1>
<%= form_with scope: :contact, url: contacts_path, local: true do |form| %>
<p>
<%= form.label :contact_name %><br>
<%= form.text_field :contact_name %>
</p>
<p>
<%= form.label :organization %><br>
<%= form.collection_select(:organization_id, Organization.all, :id, :org_name) %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<hr>
Edit: I added params for my controller and schema.
In organization.rb model:
class Organization < ApplicationRecord
has_many :deals, through: :contacts
has_many :contacts, dependent: :destroy
def org_name
"#{name}"
end
end
in your new.html.erb file:
<%= form.fields_for :organizations do |o| %>
<%= o.collection_select(:organization_id, Organization.all,
:id, :org_name,
{:prompt => 'Please select the organization'}) %>

Rails use collection_select to assign equipment to site

I have a very simple rails application that collects "equipment" and assigns it to a "site"
class CreateEquipment < ActiveRecord::Migration[5.0]
def change
create_table :equipment do |t|
t.string :site_name
t.string :equip_name
t.string :equip_model
t.string :equip_make
t.string :equip_type
t.belongs_to :site
t.timestamps
end
end
end
class CreateSites < ActiveRecord::Migration[5.0]
def change
create_table :sites do |t|
t.string :site_name
t.timestamps
end
end
end
Two models "Equipment" and "Site"
class Equipment < ApplicationRecord
end
class Site < ApplicationRecord
has_many :equipment
end
I have set up the equipment belongs_to Site and site has_many Equipment.
In my equipment form I have a collection_select to choose the site to assign the equipment too.
<div class="field">
<%= f.label :site_name %>
<%= f.collection_select :site_name, Site.all, :site_name, :site_name, prompt: true %>
</div>
<div class="field">
<%= f.label :equip_name %>
<%= f.text_field :equip_name %>
</div>
<div class="field">
<%= f.label :equip_model %>
<%= f.text_field :equip_model %>
</div>
<div class="field">
<%= f.label :equip_make %>
<%= f.text_field :equip_make %>
</div>
<div class="field">
<%= f.label :equip_type %>
<%= f.text_field :equip_type %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and on the site show page, I have begun to set up the page to show equipment.
<p>
<strong>Sitename:</strong>
<%= #site.site_name %>
</p>
<% #site.equipment.each do |equipment| %>
<p><%= equipment.equip_name %></p>
<% end %>
but I cannot see any equipment when I open the site show page.
I am not getting any faults or issues on the form or the page, I have allowed all params. I think I may have set up my collection_select incorrectly.
Update: Watching Michael Hartl's Ruby on Rails Tutorial Section 14.1.2 User/relationship associations and feel some enlightenment that anyone with this issue should get privy too.
Well I have learned alot over the time I have been playing with rails. Hopefully when a newbie finds this they will see the very simple error I made.
I didnt add the association to the site
class Equipment < ApplicationRecord
end
class Site < ApplicationRecord
has_many :equipment
end
should be
class Equipment < ApplicationRecord
belongs_to :site
end
class Site < ApplicationRecord
has_many :equipment
end
and the table should change from
class CreateEquipment < ActiveRecord::Migration[5.0]
def change
create_table :equipment do |t|
t.string :site_name
t.string :equip_name
t.string :equip_model
t.string :equip_make
t.string :equip_type
t.belongs_to :site
t.timestamps
end
end
end
class CreateEquipment < ActiveRecord::Migration[5.0]
def change
create_table :equipment do |t|
t.string :equip_name
t.string :equip_model
t.string :equip_make
t.string :equip_type
t.references :site
t.timestamps
end
end
end
I hope that helps someone :)

Nested Form with Many-to-Many relationship - Rails

I'm learning Rails and I'am pretty stuck building a nested form with many to many relationship.
I was able to get the many-to-many relationship working with has_many :through, but when it comes down to create the views and the controllers to get it working I'm getting stuck.
See models relationships below:
class Timesheet < ActiveRecord::Base
belongs_to :user
has_many :timesheet_payments
has_many :employees, :through => :timesheet_payments
accepts_nested_attributes_for :timesheet_payments,
:reject_if => :all_blank,
:allow_destroy => true
accepts_nested_attributes_for :employees
end
class Employee < ActiveRecord::Base
belongs_to :user
has_many :timesheet_payments
has_many :timesheets, :through => :timesheet_payments
end
class TimesheetPayment < ActiveRecord::Base
belongs_to :employee
belongs_to :timesheet
accepts_nested_attributes_for :employee,
:reject_if => :all_blank
end
See db schema below:
create_table "timesheet_payments", force: true do |t|
t.integer "employee_id"
t.integer "timesheet_id"
t.float "basic_hours"
t.float "sunday_bh_hours"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "timesheets", force: true do |t|
t.date "upload_date"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "employees", force: true do |t|
t.string "pps_no"
t.string "fname"
t.string "lname"
t.date "dob"
t.text "address"
t.string "ph_number"
t.float "basic_rop"
t.float "sunday_bh_rop"
t.string "email"
t.date "date_joined"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
I want to create a form where I can create a new Timesheet, display all Employees and an field to add the basic_hours and sunday_bh_hours for each employee.
This would be similar to the idea of a relationship between Customer -> Order -> Products.
I hope this makes sense! Thanks in advance!
I tried this view form
<%= form_for(#payment, :html => {:multipart => true}) do |f| %>
<% if #payment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#payment.errors.count, "error") %> prohibited this payment from being saved:</h2>
<% end %>
<!--creates a new timesheet -->
<%= f.fields_for :timesheet do |builder| %>
<%= builder.label "New timesheet" %>
<%= builder.text_field :upload_date %>
<p></p>
<% end %>
<!-- Add worked hours for each employee -->
<% #employee.each do |t| %>
<%= f.label t.fname %>
<br />
<%= f.label "Basic Hours" %>
<%= f.text_field :basic_hours %>
<%= f.label "Sunday/BH Hours" %>
<%= f.text_field :sunday_bh_hours %>
<br />
<% end %>
<%= f.submit 'Submit', :class => 'btn btn-primary' %>
<% end %>
It is loading fine but i really can't get my head around how I go about creating the "create" method on the controller.
I have the "new" method is following:
def new
#payment = TimesheetPayment.new
#timesheet = Timesheet.new
#employee = Employee.all
end

How to set up a form to create several records on a has_many :through join table?

I'm trying to create a form that creates a new exercise with muscle_groups that are primary and secondary to the exercise.
Here's the relevant part of the schema:
create_table "exercised_muscle_groups", force: true do |t|
t.integer "muscle_group_id"
t.integer "exercise_id"
t.string "used_as"
end
add_index "exercised_muscle_groups", ["exercise_id"], name: "index_exercised_muscle_groups_on_exercise_id", using: :btree
add_index "exercised_muscle_groups", ["muscle_group_id"], name: "index_exercised_muscle_groups_on_muscle_group_id", using: :btree
create_table "exercises", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "muscle_groups", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
Here's what I have for the models:
class Exercise < ActiveRecord::Base
has_many :exercised_muscle_groups, dependent: :destroy
has_many :muscle_groups, through: :exercised_muscle_groups
accepts_nested_attributes_for :muscle_groups
accepts_nested_attributes_for :exercised_muscle_groups
end
class MuscleGroup < ActiveRecord::Base
has_many :exercised_muscle_groups
has_many :exercises, through: :exercised_muscle_groups
accepts_nested_attributes_for :exercises
accepts_nested_attributes_for :exercised_muscle_groups
end
class ExercisedMuscleGroup < ActiveRecord::Base
belongs_to :exercise
belongs_to :muscle_group
end
Here's what I have so far for the controller actions for new and create:
def new
#exercise = Exercise.new
#exercise.exercised_muscle_groups.build
end
def create
#exercise = Exercise.new(exercise_params)
#exercise.save
#then loop through all muscle groups marked as primary and then create a record for each one with value of "primary" for used_as column
#then loop through all muscle groups marked as secondary and then create a record for each one with value of "secondary" for used_as column
#if all saves are successful, then redirect to show page for that exercise
#if any save unsuccessful, then redirect to new page with error message
end
Here's what I have for the form in the view. I know it's wrong and it doesn't work.
<%=form_for(#exercise) do |exercise_form| %>
<%= exercise_form.label :name %>
<%= exercise_form.text_field :name, class: 'text_field' %><br>
<div class="col-xs-12">
<% exercise_form.fields_for :exercised_muscle_groups do |muscle_group_form| %>
<%= muscle_group_form.label :primary %>
</div>
<% MuscleGroup.all.each do |muscle_group| %>
<div class="col-xs-6">
<span class="col-xs-3">
<%= check_box_tag "exercise[muscle_group_ids][]", muscle_group.id %>
</span>
<span class="col-xs-3">
<%= muscle_group.name %>
</span>
</div>
<% end %>
<div class="col-xs-12">
<%= muscle_group_form.label :secondary %>
</div>
<% MuscleGroup.all.each do |muscle_group| %>
<div class="col-xs-6">
<span class="col-xs-3">
<%= check_box_tag "exercise[muscle_group_ids][]", muscle_group.id %>
</span>
<span class="col-xs-3">
<%= muscle_group.name %>
</span>
</div>
<% end %>
<% end %>
<%= exercise_form.submit "Submit", class: "btn btn-lg btn-primary" %>
<% end %>
How do I set up the form for the exercise and controller actions? Am I overcomplicating this? Is there an easier way?
Any help would be greatly appreciated. Thanks!

Displaying an image associated with an artist

I am trying to display an image that is in my public/image folder and I
don't know how to.
What I want to do is to be able to assign a photo to an artist so you
can navigate through different artist's photo.
This is my image model
class Image < ActiveRecord::Base
has_many :publishings
has_many :artists, :through => :publishings
has_many :comments,:through => :friends
has_many :comments, :as => :resource, :class_name => "Commentable"
end
This is my image show.html
<p id="notice"><%= notice %></p>
<p>
<b>Title:</b>
<%= #image.title %>
</p>
<p>
<b>Filename:</b>
<%= #image.filename %>
</p>
<p>
<b>Likes:</b>
<%= #image.likes %>
</p>
<%= link_to 'Edit', edit_image_path(#image) %> |
<%= link_to 'Back', images_path %>
This is the database for the images
class CreateImages < ActiveRecord::Migration
def self.up
create_table :images do |t|
t.string :title :null => false
t.string :filename :null =>false
t.integer :likes :default =>0
t.timestamps
end
end
def self.down
drop_table :images
end
end
Thanks in advance
<%= image_tag #image.filename %>

Resources