Rails has_and_belongs_to_many AND collection_check_boxes - ruby-on-rails

So there isn't an error, my join table just isn't being populated with any information... #lead.districts returns District::ActiveRecord_Associations_CollectionProxy rather than the expected text...
Where have I made my mistake?
new.html.erb
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="subjects new">
<h2>Create Lead</h2>
<%= form_for(:lead, :url => {:action => 'create'}) do |f| %>
<table cellspacing="0">
<tr>
<th>First Name</th>
<td><%= f.text_field(:first_name) %></td>
</tr>
<tr>
<th>Last Name</th>
<td><%= f.text_field(:last_name)%></td>
</tr>
<tr>
<th>Phone Number</th>
<td><%= f.text_field(:phone_number)%></td>
</tr>
<tr>
<th>Email</th>
<td><%= f.text_field(:email)%></td>
</tr>
<tr>
<th>Move-in Date</th>
<td><%= f.date_field(:move_in_date, min: Date.today)%></td>
</tr>
<tr>
<th>Bedrooms</th>
<td><%= f.text_field(:beds)%></td>
</tr>
<tr>
<th>Bathrooms</th>
<td><%= f.text_field(:baths)%></td>
</tr>
<tr>
<th>Maximum Price Considered</th>
<td><%= f.text_field(:maxprice)%></td>
</tr>
<tr>
<th>Preferred Neighborhoods</th>
<td>
<%= collection_check_boxes(:lead, :district_ids, District.all.order("districts.name ASC"), :id, :name )%>
</td>
</tr>
<tr>
<th>Have you ever broken a lease?</th>
<td>
<%= f.label :broken_lease, "Yes", :value => true %>
<%= f.radio_button :broken_lease, true %>
<%= f.label :broken_lease, "No", :value => false %>
<%= f.radio_button :broken_lease, false %>
</td>
</tr>
<tr>
<th>Have you ever been convicted of a felony?</th>
<td>
<%= f.label :felon, "Yes", :value => true %>
<%= f.radio_button :felon, true %>
<%= f.label :felon, "No", :value => false %>
<%= f.radio_button :felon, false %>
</td>
</tr>
<tr>
<th>Do you have any pets?</th>
<td>
<%= f.label :pets, "Yes", :value => true %>
<%= f.radio_button :pets, true %>
<%= f.label :pets, "No", :value => false %>
<%= f.radio_button :pets, false %>
</td>
</tr>
<tr>
<th>If so, please provide a brief description</th>
<td><%= f.text_area(:pets_description)%></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Create Lead") %>
</div>
<% end %>
</div>
leads_controller.rb
class LeadsController < ApplicationController
layout false
def index
#leads = Lead.order("leads.created_at DESC")
end
def show
#lead = Lead.find(params[:id])
end
def new
#lead = Lead.new
#districts = District.all.order("districts.name ASC")
end
def create
#lead = Lead.new(lead_params)
if #lead.save
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#lead = Lead.find(params[:id])
end
def update
#lead = Lead.find(params[:id])
if #lead.update_attributes(lead_params)
redirect_to(:action => 'show', :id => #lead.id)
else
render('edit')
end
end
def delete
#lead = Lead.find(params[:id])
end
def destroy
lead = Lead.find(params[:id]).destroy
flash[:notice] = "Lead '#{lead.id}' deleted successfully."
redirect_to(:action => 'index')
end
private
def lead_params
params.require(:lead).permit(:first_name, :last_name, :phone_number, :email, :move_in_date, :beds, :baths, :maxprice, :broken_lease, :felon, :pets, :pets_description, :district => [])
end
end
districts_controller.rb
class DistrictsController < ApplicationController
layout false
def index
#districts = District.order("districts.name ASC")
end
def show
#district = District.find(params[:id])
end
def new
#district = District.new
end
def create
#district = District.new(district_params)
if #district.save
flash[:notice] = "District created successfully."
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#district = District.find(params[:id])
end
def update
#district = District.find(params[:id])
if #district.update_attributes(district_params)
flash[:notice] = "District updated successfully."
redirect_to(:action => 'show', :id => #district.id)
else
render('edit')
end
end
def delete
#district = District.find(params[:id])
end
def destroy
district = District.find(params[:id]).destroy
flash[:notice] = "District '#{district.name}' deleted successfully."
redirect_to(:action => 'index')
end
private
def district_params
params.require(:district).permit(:name)
end
end
district.rb
class District < ActiveRecord::Base
has_and_belongs_to_many :leads
end
lead.rb
class Lead < ActiveRecord::Base
has_and_belongs_to_many :districts
end
Join table migration:
class CreateDistrictsLeadsJoin < ActiveRecord::Migration
def change
create_table :districts_leads, :id => false do |t|
t.integer "district_id"
t.integer "lead_id"
end
add_index :districts_leads, ["district_id", "lead_id"]
end
end

My error was in mass assigning my params (used :district should have been :district_ids)
Correct params function:
# leads_controller.rb
private
def lead_params
params.require(:lead).permit(:first_name, :last_name, :phone_number, :email, :move_in_date, :beds, :baths, :maxprice, :broken_lease, :felon, :pets, :pets_description, :district_ids => [])
end

You will need to add leads to district and districts to leads in both create methods.
def create # in LeadsController
#lead = Lead.new(lead_params)
if #lead.save
district = District.find(...) #find district to associate
district.leads << #lead #add lead to district
redirect_to(:action => 'index')
else
render('new')
end
end

#leads_controller.rb
def create
#lead = Lead.new(lead_params)
if #lead.save
selected_dis = params['lead_district_ids'] #[3,4,5]
districts = District.where(id: selected_dis).first
#check how you getting district params and use that to fetch orders.
districts.each do |d|
#lead.districts << d
#assumming your one lead can have many districts.
end
redirect_to leads_path
else
render :new
end
end

Related

How to update multiple record in rails 6

my controller
class IndustryController < ApplicationController
def index
#industries=Industry.all
end
def new
#industry=Industry.new
end
def create
#industry = Industry.new(industry_params)
# #industry = Industry.new(params[:name_bef,:name,:status])
# #industry = Industry.new(:name_bef => (params[:name]),:name => (params[:name]), :status => (params[:status]))
if #industry.save
redirect_to industry_index_path
else
render 'new'
end
end
def update
#industry = Industry.all
#industry.each do |i|
#industry.find(i.id)
#industry.update(industry_params)
end
end
private
def industry_params
params.require(:industry).permit(:name, :status)
end
private
def industry_update
params.require(:industry).permit(:name, :status)
end
end
my html
<%= form_for #industries, as: :industry, url: industry_path(#industries), method: :patch do |f| %>
<table class="table table-bordered">
<thead class="bg-light">
<tr>
<th scope="col">業種名(変更前)</th>
<th scope="col">業種名</th>
<th scope="col">状態</th>
</tr>
</thead>
<tbody>
<% #industries.each do |industry| %>
<tr>
<td><%= industry.name %></td>
<td>
<div class="form-group">
<%= f.text_field :name, :value => industry.name, :id => industry.id, :class => "form-control" %>
</div>
</td>
<td>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="form-group text-center">
<%= f.submit"登録", :class => " btn btn-success" %>
</div>
<%end%>
My assumption is that you are talking about the update action in your controller. It looks like update_all is the method you are looking for.
def update
Industry.update_all(industry_params)
end
This can be dangerous - be aware that this will bypass active record validations and callbacks, so make sure you are certain.

Rails 5: Displaying object value not ID

I have a simple app for a skydiving dropzone. Parachute packers keep track of their packsjobs. There are several packjobs for each parachute rig.
I have the Show view working perfectly by showing the rig_type_number under the rig detail (ie Reflex #2) but I can only get the rig_id to work in the same column for the index. Ideally, I'd like to so the rig_type_number as well instead of the ID.
How can I reference in the view something to the effect of #rigs.rig_type_number?
This makes sense to me, but does not work:
<td><%= link_to packjob.rig_id,rig_path(rig.rig_type_number) %>
Will I have to a where clause for the index controller?
db:
class CreatePackjobs < ActiveRecord::Migration[5.1]
def change
create_table :packjobs do |t|
t.string :packer
t.string :rig
t.references :rig, foreign_key: true
t.timestamps
end
end
end
class CreateRigs < ActiveRecord::Migration[5.1]
def change
create_table :rigs do |t|
t.boolean :rig_status
t.string :rig_type_number
t.integer :rig_season_jumpnum
t.date :rig_res_last
t.string :rig_res_who
t.timestamps
end
end
end
packjobs_controller:
class PackjobsController < ApplicationController
def index
#packjobs = Packjob.paginate(page: params[:page]).order('id DESC')
#rigs = Rig.where(rig_status: "t")
end
def show
#packjob = Packjob.find(params[:id])
#rig = Rig.find(#packjob.rig_id)
end
def new
#packjob = Packjob.new
#rigs = Rig.where(rig_status: "t")
end
def edit
#packjob = Packjob.find(params[:id])
#rigs = Rig.where(rig_status: "t")
end
def create
#packjob = Packjob.new(packjob_params)
#rigs = Rig.where(rig_status: "t")
if #packjob.save
redirect_to #packjob
else
render 'new'
end
end
def update
#packjob = Packjob.find(params[:id])
#rigs = Rig.where(rig_status: "t")
if #packjob.update(packjob_params)
redirect_to #packjob
else
render 'edit'
end
end
def destroy
#packjob = Packjob.find(params[:id])
#packjob.destroy
redirect_to packjobs_path
end
private
def packjob_params
params.require(:packjob).permit(:packer, :rig_id)
end
def rigs_params
params.require(:rig).permit(:rig_status, :rig_type_number)
end
end
show.html.erb:
<p>
<b>Datestamp:</b>
<%= #packjob.created_at.strftime("%m/%d/%Y %I:%M %P") %><br>
</p>
<p>
<strong>Packer:</strong>
<%= #packjob.packer %>
</p>
<p>
<strong>Rig:</strong>
<%= #rig.rig_type_number %><br>
</p>
<%= link_to 'Edit', edit_packjob_path(#packjob) %> |
<%= link_to 'Back', packjobs_path %>
index.html.erb:
<h1>Listing Packjobs</h1>
<%= link_to 'New Packjob', new_packjob_path %> | <br>
<br>
<div class="digg_pagination">
<%= will_paginate #packjobs, :container => false %>
</div>
<table id=listtable CELLPADDING="4" border="1">
<tr class="headerBlue" >
<th>Datestamp</th>
<th>Packer</th>
<th>Rig</th>
<th colspan="3"></th>
</tr>
<% #packjobs.each do |packjob| %>
<tr class="<%= cycle('rowA', 'rowB') %>">
<td><%= link_to packjob.created_at.strftime("%m/%d/%Y %I:%M:%S %P"),packjob_path(packjob.id) %></td>
<td><%= packjob.packer %></td>
<td><%= link_to packjob.rig_id,rig_path(packjob.rig_id) %>
<td><%= link_to 'Show', packjob_path(packjob) %></td>
<td><%= link_to 'Edit', edit_packjob_path(packjob) %></td>
<td><%= link_to 'Destroy', packjob_path(packjob),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
</table>
<div class="digg_pagination">
<div class="page_info">
<%= page_entries_info #packjobs %>
</div>
<%= will_paginate #packjobs, :container => false %>
</div>
<br><%= link_to 'New Packjob', new_packjob_path %> |
EDITED:
models:
class Packjob < ApplicationRecord
belongs_to :rig
validates :packer, presence: true
validates :rig_id, presence: true
self.per_page = 25
end
class Rig < ApplicationRecord
has_many :packjobs
validates :rig_type_number, presence: true
self.per_page = 25
end
Your associations are set correctly, you can reference the associated object like this:
<td><%= link_to packjob.rig.rig_type_number,rig_path(packjob.rig_id) %>
You can call it as below in case you have has_one :rig association in PackJob model :
<td><%= link_to packjob..rig.rig_type_number,rig_path(packjob.rig_id) if packjob.rig.present? %> </td>

Rails: Search bar isn't working

I'm trying to add a search bar for a database website I created, I found a tutorial and I "think" I did it correct.
When I do a search, such as "Judy Zhang", nothing shows up, even though it is in the database
my vendor.rb/concerns/models/app file
class Vendor < ApplicationRecord
has_many :fotos
def self.search(search)
if search
Vendor.where('lower(contact_name) LIKE ?', "'%#{search.downcase}%'")
else
Vendor.all
end
end
end
I believe I didn't do the coding right. Very new to ruby on rails. What did I do wrong here?
code for index.html.erb/vendors/views/layouts/app
<body>
<div class = "head">
<h1>Vendors </h1>
<div class = "image1" >
<img src= "http://dx.deucex.com/i/logo.png" >
</div>
</div>
</body>
<table>
<tr>
<%= button_to "New Vendor", new_vendor_path, :method => "get" %>
<%= button_to "Inventory", inventories_path, :method => "get" %>
<%= form_tag vendors_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
</tr>
</table>
<table>
<tr>
<th>Company</th>
<th>Contact Name</th>
<th>Phone</th>
<th>Email</th>
</tr>
<% for vendor in #vendors %>
<tr>
<td><%= vendor.company %></td>
<td><%= vendor.contact_name %></td>
<td><%= vendor.phone %></td>
<td><%= vendor.email %></td>
<body>
<div class = "button1" >
<td><%= button_to "Show", vendor_path(vendor), :method => "get" %></td>
</div>
</body>
<td><%= button_to "Edit", edit_vendor_path(vendor), :method => "get" %></td>
<div class = "button3">
<td><%= button_to 'Delete',
vendor_path(vendor),
method: :delete,
data: { confirm: 'Are you sure?'} %></td>
</div>
</tr>
<% end %>
</table>
code for my VendorsController.rb/concerns/controller/app
class VendorsController < ApplicationController
def index
#vendors = Vendor.search(params[:search])
end
def show
#vendor = Vendor.find(params[:id])
end
def new
#vendor = Vendor.new
end
def create
#vendor = Vendor.new(vendor_params)
if #vendor.save
redirect_to #vendor
else
render 'new'
end
end
def edit
#vendor = Vendor.find(params[:id])
end
def update
#vendor = Vendor.find(params[:id])
if #vendor.update (vendor_params)
redirect_to #vendor
else
render 'edit'
end
end
def destroy
#vendor = Vendor.find(params[:id])
#vendor.destroy
redirect_to vendors_path
end
end
private
def vendor_params
params.require(:vendor).permit(:company, :contact_name, :phone, :email, :moq, :cost_per_item, :payment_method, :terms, :turnover, :returns, :notes)
end
Try changing the code in Vendor to
class Vendor < ApplicationRecord
has_many :fotos
def self.search(search)
if search
Vendor.where('lower(name) LIKE ?', "'%#{search.downcase}%'")
else
Vendor.all
end
end
end
My search bar isn't showing up
You should change the following
<% form_tag vendors_path, :method => 'get' do %>
to
<%= form_tag vendors_path, :method => 'get' do %>
# Notice that <%= %> evaluates and prints the output

undefined 'items' for nil class

on my e-store website when I try to checkout my cart, I'm getting
undefined method `items' for nil:NilClass.
Although on the error page
I know that my cart is there... but when I call it, it gives me nil
Cart Model
class Cart
attr_reader :items
def self.build_from_hash hash
items = if hash["cart"] then
hash["cart"]["items"].map do |item_data|
CartItem.new item_data["product_id"], item_data["quantity"]
end
else
[]
end
new items
end
def initialize items = []
#items = items
end
def add_item product_id
item = #items.find { |item| item.product_id == product_id }
if item
item.increment
else
#items << CartItem.new(product_id)
end
end
def empty?
#items.empty?
end
def count
#items.length
end
def serialize
items = #items.map do |item|
{
"product_id" => item.product_id,
"quantity" => item.quantity
}
end
{
"items" => items
}
end
def total_price
#items.inject(0) { |sum, item| sum + item.total_price }
end
end
Application Controller
def initialize_cart
#cart = Cart.build_from_hash session
end
Cart Controller
class CartsController < ApplicationController
before_filter :initialize_cart
def add
#cart.add_item params[:id]
session["cart"] = #cart.serialize
product = Product.find params[:id]
redirect_to :back, notice: "Added #{product.name} to cart."
end
def show
end
def checkout
#order_form = OrderForm.new user: User.new
end
end
Order Controller
class OrdersController
def create
#order_form = OrderForm.new(
user: User.new(order_params[:user]),
cart: #cart
)
if #order_form.save
redirect_to '/', notice: "Thank you for placing your order."
#cart.empty?
else
render 'carts/checkout'
end
end
Checkout View
<div class="container-checkout">
<p class="text-title"> You are checking out the following: </p>
<table class="table table-striped">
<thead class="name-table">
<tr>
<td> Image </td>
<td> Name </td>
<td> Category</td>
<td> Size </td>
<td> Item Price </td>
</tr>
</thead>
<tbody>
<% #cart.items.each do |item| %>
<tr>
<td><img src="<%= item.product.image %>" width="50px"></td>
<td><%= item.product.name.capitalize %></td>
<td><%= item.product.category.name %></td>
<td><%= item.product.size %></td>
<td class="price-item"><%= number_to_currency item.total_price %>
</td>
<% end %>
</tr>
<tr class="total-price total-price-checkout">
<td class="name-table">Total Price</td>
<td class="price-item"><%= number_to_currency #cart.total_price %></td>
</tr>
</tbody>
</table>
</div>
<div class="details-user-form">
<%= form_for #order_form, url: orders_path do |f|%>
<% f.fields_for :user, #order_form.user do |u| %>
<p class="text-title">Fill the form with your details</p>
<p><%= render "orders/errors" %></p>
<p><%= u.text_field :name, placeholder: "Name" %></p>
<p><%= u.text_field :email, placeholder: "Email" %></p>
<p><%= u.text_field :address, placeholder: "Address" %></p>
<p><%= u.text_field :postal_code, placeholder: "Postal code" %></p>
<p><%= u.text_field :city, placeholder: "City" %></p>
<p><%= u.text_field :country, placeholder: "Country" %></p>
<%= f.submit "Place order", class: "order-btn"%><br>
<% end %>
<% end %>
</div>
Any idea of why is it doing so? Also because, it was working before.. I don't know why it stopped.
I think the problem may be that the #cart variable isn't being set in the OrdersController. Setting the variable in CartsController doesn't make it available globally, as it would only be scoped to the controller that created it, in your case the CartsController.
Also, I see that your Cart model is more of a virtual model than an ActiveRecord model, is that the behaviour you were looking for as I believe ActiveRecord already has a lot of the methods you're recreating there.
I'm not totally sure but I think these may be the issues.
UPDATE
I think I found your error.
In your OrdersController you should have a
before_action :initialize_cart
That seems to be coming from your ApplicationController
If you check your checkout method in your CartController, you will see that you did not set #cart. So when you hit the checkout view, it comes to look for the value or #cart in this method. Setting it there, like the code below should clear your error.
def checkout
#order_form = OrderForm.new user: User.new
#cart = # cart object
end

I get this error: "'3' is not an ActiveModel-compatible object. It must implement :to_partial_path." and it only happens when I add validations

This is the error I get.
This is my model code.
class Subject < ActiveRecord::Base
has_many :pages
validates :name,
:presence => true,
:length => { :maximum => 255 }
scope :visible, lambda { where(:visible => true) }
scope :invisible, lambda { where(:visible => false) }
scope :sorted, lambda { order("subjects.position ASC")}
scope :newest_first, lambda { order("subject.created_at DESC") }
scope :search, lambda { |query|
where(["name LIKE ?", "%#{query}%"])
}
end
I noticed that it's saying something about my create action. So here's that:
class SubjectsController < ApplicationController
layout "admin"
def index
#subjects = Subject.sorted
end
def show
#subject = Subject.find(params[:id])
end
def new
#subject = Subject.new({:name => 'Default'})
#subject_count = Subject.count + 1
end
def create
# Instantiate a new object using form parameters
#subject = Subject.new(subject_params)
# Save the object
if #subject.save
# If save succeeds, redirect to the index
flash[:notice] = "The subject was created successfully."
redirect_to(:action => 'index')
else
#subject_count = Subject.count + 1
render(new)
end
end
def edit
#subject = Subject.find(params[:id])
#subject_count = Subject.count
end
def update
# Find an existing object using form parameters
#subject = Subject.find(params[:id])
# Update the object
if #subject.update_attributes(subject_params)
# If save succeeds, redirect to the show
flash[:notice] = "The subject was updated successfully."
redirect_to(:action => 'show', :id => #subject.id)
else
# If update fails, redisplay the form so user can fix problem.
#subject_count = Subject.count
render('edit')
end
end
def delete
#subject = Subject.find(params[:id])
end
def destroy
subject = Subject.find(params[:id]).destroy
flash[:notice] = "The subject '#{subject.name}' was deleted successfully."
redirect_to(:action => 'index')
end
private
def subject_params
# same as using "params[:subject]", except that it:
# -raises an error if :subject is not present
# -allows listed attributes to be mass-assigned
params.require(:subject).permit(:name, :position, :visible)
end
end
If I take the validations off it works, but then I can create the subject without a name. I noticed that if I add a subject the number in the paranthesis '3' moves up. I wonder if it has something to do with the select form. Here's the form:
<%= error_messages_for(#subject) %>
<table summary="Subject form fields" class="table table-hover">
<tr>
<th><%= f.label(:name, "Name") %></th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th><%= f.label(:position) %></th>
<td><%= f.select(:position, 1..#subject_count) %></td>
</tr>
<tr>
<th><%= f.label(:visible) %></th>
<td><%= f.check_box(:visible) %></td>
</tr>
</table>
Update: I'm adding the index view per request.
<% #page_title = 'Subjects' %>
<div class="subjects show">
<h2>Subjects</h2>
<%= link_to("Add New Subject", {:action => 'new'}, :class => 'action new btn btn-default') %>
<table class="listing table table-hover" summary="Subject list">
<tr class="header">
<th>Position</th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% #subjects.each do |subject| %>
<tr>
<td><%= subject.position %></td>
<td><%= subject.name %></td>
<td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
<td class="center"><%= subject.pages.size %></td>
<td class="actions">
<%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
<%= link_to("Edit", {:action => 'edit', :id => subject.id}, :class => 'action edit') %>
<%= link_to("Delete", {:action => 'delete', :id => subject.id}, :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
</div>
due to a syntax error for the partial on the else part of create action, active model was throwing an exception(http://apidock.com/rails/v3.2.1/ActionView/PartialRenderer/partial_path)
def create
#few lines escaped....
else
#subject_count = Subject.count + 1
render(new) # <-- Error
end
It should be either render :new or render 'new' as mentioned by Nobilik. your method should look like:
def create
#few lines escaped....
else
#subject_count = Subject.count + 1
render :new # for best practice
end

Resources