所以没有错误,我的联接表只是没有填充任何信息...... @ lead.districts返回District :: ActiveRecord_Associations_CollectionProxy而不是预期的文本......
我在哪里弄错了?
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
加入表格迁移:
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
答案 0 :(得分:1)
我的错误是大量分配我的参数(使用过:区应该是:district_ids)
纠正params功能:
# 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
答案 1 :(得分:0)
您需要在两个创建方法中将区域和区域的潜在客户添加到潜在客户。
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
答案 2 :(得分:0)
#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