Skip to content

Instantly share code, notes, and snippets.

@wethu
Last active August 29, 2015 13:56
Show Gist options
  • Save wethu/9063349 to your computer and use it in GitHub Desktop.
Save wethu/9063349 to your computer and use it in GitHub Desktop.
Form works, but each "Job_Value" needs its own separate record. One for A property selected, or an array of choices, or included in both is a text for other
<%= form_for @job do |f| -%>
<%= f.fields_for :job_values do |j| -%>
<%= field_set_tag j.object.detail.name do %>
<% if (!j.object.detail.select_many && !j.object.detail.yes_no) %>
<%= j.collection_select :property_id, j.object.detail.properties.collect,
:id, :name, :prompt => '- Select -' -%>
<% end -%>
<% if (j.object.detail.yes_no) %>
<%= j.select(:select, BOOLEAN_SELECT, { prompt: true }) -%>
<% end -%>
<% if (j.object.detail.select_many) %>
<% j.object.detail.properties.each_with_index do |l, i| %>
<%= l.name %>
<%= check_box_tag "job[job_values_attributes][#{i}][property_ids][]", l.id %>
<% end %>
<% end -%>
<% if (j.object.detail.has_other) %>
<%= j.label :Other -%>
<%= j.text_field :other -%>
<% end -%>
<% end # fieldset%>
<hr>
<% end # job_values fields%>
<%= f.submit class: 'btn btn-success', class: 'btn btn-success' -%>
<% end # job fields%>
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mPqHohtSN2c0l80a+1tFvYTIhaCvy70OgFxiX1ZyJIg=",
Parent Model Job:
"job"=>{
"job_values_attributes"=>{
"0"=>{"property_id"=>"1", "property_ids"=>["4", "7"]}, <--- this is based off conditionals, which only renders a select options, which means, why is there property_ids[4,7]
"1"=>{"property_id"=>"", "other"=>"test", "property_ids"=>["5", "8"]},
"2"=>{"property_ids"=>["6", "9"]},
"3"=>{"select"=>"true", "property_ids"=>["10"]}}},
"commit"=>"Create Job"}
class JobsController < ApplicationController
def index
@jobs = Job.all
@job = Job.new # TODO Redirected to index after creating job expects @job
end
def create
@job = Job.new(safe_params)
if @job.save
respond_to do |format|
format.html { redirect_to jobs_path, notice: "Created #{@job.description}."} # TODO Truncate this or find some other description
end
else
raise @job.inspect # FIXME handle this another way..
end
end
# Create a new job
def new
details = Detail.where(:product_id => params[:product_id]).to_a
@job = Job.new
details.each do |detail|
@job.job_values.build(:detail_id => detail.id)
end
end
def destroy
job = Job.find(params[:id])
job.destroy
flash[:notice] = "#{job.description} deleted."
redirect_to jobs_path
end
private
def safe_params
params.require(:job).permit(
:job, :quantity, :description, :client_id, :product_id,
job_values_attributes: [:detail_id, :property_id, { :property_ids => [] }, :select, :other, :_destroy]
)
end
end
class Job < ActiveRecord::Base
has_many :job_values
belongs_to :product
accepts_nested_attributes_for :job_values, reject_if: :all_blank, allow_destroy: true
end
class JobValue < ActiveRecord::Base
belongs_to :job
belongs_to :detail
has_and_belongs_to_many :properties
end
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Comprint</title>
<link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/jquery.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery_ujs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/turbolinks.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery_nested_form.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/clients.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jobs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/products.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/application.js?body=1"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="mPqHohtSN2c0l80a+1tFvYTIhaCvy70OgFxiX1ZyJIg=" name="csrf-token" />
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div id="wrapper" class="">
<div class="container">
<div class="page-header">
<h1>
<small>Comprint - Server</small>
</h1>
</div>
</div>
<div id="messages" class="container">
</div>
<div id="content" class="container">
<h1>Create a new job <i>Business Cards</i></h1>
<form accept-charset="UTF-8" action="/jobs" class="new_job" id="new_job" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="mPqHohtSN2c0l80a+1tFvYTIhaCvy70OgFxiX1ZyJIg=" /></div>
<fieldset><legend>Size</legend>
<select id="job_job_values_attributes_0_property_id" name="job[job_values_attributes][0][property_id]"><option value="">- Select -</option>
<option value="1">90 x 55</option>
<option value="2">95 x 60</option>
<option value="3">100 x 55</option></select>
</fieldset> <hr>
<fieldset><legend>Colour</legend>
<select id="job_job_values_attributes_1_property_id" name="job[job_values_attributes][1][property_id]"><option value="">- Select -</option>
</select>
<label for="job_job_values_attributes_1_Other">Other</label> <input id="job_job_values_attributes_1_other" name="job[job_values_attributes][1][other]" type="text" />
</fieldset> <hr>
<fieldset><legend>Finishing</legend>
Celloglaze
<input id="job_job_values_attributes_0_property_ids_" name="job[job_values_attributes][0][property_ids][]" type="checkbox" value="4" />
DeSample
<input id="job_job_values_attributes_1_property_ids_" name="job[job_values_attributes][1][property_ids][]" type="checkbox" value="5" />
Foilovers
<input id="job_job_values_attributes_2_property_ids_" name="job[job_values_attributes][2][property_ids][]" type="checkbox" value="6" />
</fieldset> <hr>
<fieldset><legend>Double Sided</legend>
<select id="job_job_values_attributes_3_select" name="job[job_values_attributes][3][select]"><option value="">Please select</option>
<option value="true">Yes</option>
<option value="false">No</option></select>
</fieldset> <hr>
<fieldset><legend>Rounded Corners</legend>
Top Right
<input id="job_job_values_attributes_0_property_ids_" name="job[job_values_attributes][0][property_ids][]" type="checkbox" value="7" />
Top Left
<input id="job_job_values_attributes_1_property_ids_" name="job[job_values_attributes][1][property_ids][]" type="checkbox" value="8" />
Bottom Right
<input id="job_job_values_attributes_2_property_ids_" name="job[job_values_attributes][2][property_ids][]" type="checkbox" value="9" />
Bottom Left
<input id="job_job_values_attributes_3_property_ids_" name="job[job_values_attributes][3][property_ids][]" type="checkbox" value="10" />
</fieldset> <hr>
<input class="btn btn-success" name="commit" type="submit" value="Create Job" /></form>
</div>
<div id="actions" class="container">
<hr />
</div>
</div>
<div id="footer">
<div class="container">
<div class="navbar navbar-default">
<div class="nav navbar-nav">
<p class="navbar-text">
<span class="text-muted">Developed by Ellis Gray
&copy; 2014 Complete Printing. All rights reserved.
</span>
</p>
</div>
</div>
</div>
</div>
</body>
</html>
Started POST "/jobs" for 127.0.0.1 at 2014-02-18 11:50:25 +1000
Processing by JobsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mPqHohtSN2c0l80a+1tFvYTIhaCvy70OgFxiX1ZyJIg=", "job"=>{"job_values_attributes"=>{"0"=>{"property_id"=>"1", "property_ids"=>["4", "7"]}, "1"=>{"property_id"=>"", "other"=>"test", "property_ids"=>["5", "8"]}, "2"=>{"property_ids"=>["6", "9"]}, "3"=>{"select"=>"true", "property_ids"=>["10"]}}}, "commit"=>"Create Job"}
Property Load (0.3ms) SELECT `properties`.* FROM `properties` WHERE `properties`.`id` IN (4, 7)
Property Load (0.3ms) SELECT `properties`.* FROM `properties` WHERE `properties`.`id` IN (5, 8)
Property Load (0.2ms) SELECT `properties`.* FROM `properties` WHERE `properties`.`id` IN (6, 9)
Property Load (0.2ms) SELECT `properties`.* FROM `properties` WHERE `properties`.`id` = 10 LIMIT 1
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO `jobs` (`created_at`, `updated_at`) VALUES ('2014-02-18 01:50:25', '2014-02-18 01:50:25')
SQL (0.1ms) INSERT INTO `job_values` (`created_at`, `job_id`, `property_id`, `updated_at`) VALUES ('2014-02-18 01:50:25', 15, 1, '2014-02-18 01:50:25')
(6.4ms) INSERT INTO `job_values_properties` (`job_value_id`, `property_id`) VALUES (23, 4)
(0.1ms) INSERT INTO `job_values_properties` (`job_value_id`, `property_id`) VALUES (23, 7)
SQL (0.2ms) INSERT INTO `job_values` (`created_at`, `job_id`, `other`, `updated_at`) VALUES ('2014-02-18 01:50:25', 15, 'test', '2014-02-18 01:50:25')
(0.1ms) INSERT INTO `job_values_properties` (`job_value_id`, `property_id`) VALUES (24, 5)
(0.1ms) INSERT INTO `job_values_properties` (`job_value_id`, `property_id`) VALUES (24, 8)
SQL (0.1ms) INSERT INTO `job_values` (`created_at`, `job_id`, `updated_at`) VALUES ('2014-02-18 01:50:25', 15, '2014-02-18 01:50:25')
(0.1ms) INSERT INTO `job_values_properties` (`job_value_id`, `property_id`) VALUES (25, 6)
(0.1ms) INSERT INTO `job_values_properties` (`job_value_id`, `property_id`) VALUES (25, 9)
SQL (0.1ms) INSERT INTO `job_values` (`created_at`, `job_id`, `select`, `updated_at`) VALUES ('2014-02-18 01:50:25', 15, 1, '2014-02-18 01:50:25')
(1.1ms) INSERT INTO `job_values_properties` (`job_value_id`, `property_id`) VALUES (26, 10)
(0.3ms) COMMIT
Redirected to http://0.0.0.0:3000/jobs
#snip
create_table "details", force: true do |t|
t.string "name"
t.integer "product_id"
t.integer "resource_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "required_at"
t.boolean "has_other"
t.boolean "select_many"
t.boolean "yes_no"
end
create_table "job_values", force: true do |t|
t.integer "job_id"
t.integer "detail_id"
t.integer "property_id"
t.string "other"
t.boolean "select"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "job_values_properties", id: false, force: true do |t|
t.integer "property_id"
t.integer "job_value_id"
end
create_table "jobs", force: true do |t|
t.integer "client_id"
t.integer "product_id"
t.integer "quantity"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "products", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "properties", force: true do |t|
t.string "name"
t.integer "detail_id"
t.datetime "created_at"
t.datetime "updated_at"
end
#snip
@wethu
Copy link
Author

wethu commented Feb 18, 2014

As above, the outputted form looks a bit like:

Size - Select <- this should be 1 record in the job_values table, but it gets mixed up with the others
Colour - Select, Other. <- Same with this
Finishing - Checkboxes <- And this

Etc..

Whats happening I think is the names of the checkboxes are matching the same index as the auto generated ones from fields_for form helper.

@wethu
Copy link
Author

wethu commented Feb 18, 2014

Anyone who stumbles on this, the fix was using j.index instead of i for the checkbox array index like:

<%= check_box_tag "job[job_values_attributes][#{j.index}][property_ids][]", l.id %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment