Another Update: here is my development log. I'm confused about 2 things.
- Where is "geography_ids" coming from (Unpermitted parameter: geography_ids). I searched my entire project and the only place it appears is in the log file. I think that's the root of the problem.
- It appears that simple_form is trying to add a fourth item judging from the empty double quotes.
Here is the log file:
Started PATCH "/splits/11" for 127.0.0.1 at 2015-09-03 10:28:38 -0500
Processing by SplitsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"h5/1qn3KJm8lktOjNGvEH/POZHqx8msCIwtoi42ZYVtbEimbyPiEPwDgdIwzrfBYDZZPPxku0uj7flcsRf6b9g==", "split"=>{"name"=>"Domestic Canada Foreign", "description"=>"", "geography_ids"=>["1", "2", "3", ""], "issue_id"=>"1"}, "commit"=>"Update Split", "id"=>"11"}
[1m[36mSplit Load (0.3ms)[0m [1mSELECT "splits".* FROM "splits" WHERE "splits"."id" = $1 LIMIT 1[0m [["id", 11]]
Unpermitted parameter: geography_ids
[1m[35m (0.1ms)[0m BEGIN
[1m[36m (0.1ms)[0m [1mCOMMIT[0m
Redirected to http://phoenix.dev/splits/11
Completed 302 Found in 4ms (ActiveRecord: 0.4ms)
Started GET "/splits/11" for 127.0.0.1 at 2015-09-03 10:28:38 -0500
Processing by SplitsController#show as HTML
Parameters: {"id"=>"11"}
[1m[35mSplit Load (0.2ms)[0m SELECT "splits".* FROM "splits" WHERE "splits"."id" = $1 LIMIT 1 [["id", 11]]
[1m[36mIssue Load (0.2ms)[0m [1mSELECT "issues".* FROM "issues" WHERE "issues"."id" = $1 LIMIT 1[0m [["id", 1]]
Rendered splits/show.html.erb within layouts/application (1.4ms)
Completed 200 OK in 610ms (Views: 608.6ms | ActiveRecord: 0.4ms)
Started GET "/assets/user-2.jpg" for 127.0.0.1 at 2015-09-03 10:28:39 -0500
Started GET "/assets/user-13.jpg" for 127.0.0.1 at 2015-09-03 10:28:39 -0500
Started GET "/assets/user-1.jpg" for 127.0.0.1 at 2015-09-03 10:28:39 -0500
Update: I've updated my models and controllers with my most recent changes. I discovered some pluralization nuances which I fixed up. But, the root problem still exists. The data isn't going into the geographies_splits table which means the checkboxes aren't able to save their state when the split is saved.
I've been racking my head on this for days.
Here are my models:
class Split < ActiveRecord::Base
belongs_to :issue
has_and_belongs_to_many :geographies
end
lass Geography < ActiveRecord::Base
has_and_belongs_to_many :splits
end
When I read the Rails Guides on has_and_belongs_to_many associations it said to create a third table that essentially joins these together.
So I ran this migration:
class CreateGeographiesSplits < ActiveRecord::Migration
def change
create_table :geographies_splits do |t|
t.belongs_to :geography, index: true
t.belongs_to :split, index: true
t.timestamps null: false
end
end
end
It doesn't say anything about creating a model or controller so I didn't initially. While continuing to work on this I did create a controller and model.
I'm using simple_form to display my checkboxes and they are displaying correctly. However, when I create a split the checkboxes with the geography content aren't saved to the DB and when I go back into the split they're unchecked.
Here is the form code for creating a split where I'm trying to say what geographies it has:
<%= simple_form_for(@split) do |f| %>
<%= f.error_notification %>
<div class="form-inputs form-width">
<%= f.input :name %>
<%= f.input :description %>
<%= f.association :geographies, as: :check_boxes %>
<%= f.association :issue, label_method: :name, value_method: :id, include_blank: true %>
</div>
<div>
<p>More robust Query UI here</p>
</div>
<div>
<p>Select printer UI here</p>
</div>
<div class="form-actions">
<%= f.button :submit, :class => 'btn btn-primary btn-small' %>
<%= link_to 'Cancel', splits_path, :class => 'btn btn-default btn-small' %>
</div>
<% end %>
Here is the Splits Controller:
class SplitsController < ApplicationController
before_action :set_split, only: [:show, :edit, :update, :destroy]
# GET /splits
# GET /splits.json
def index
@splits = Split.all
end
# GET /splits/1
# GET /splits/1.json
def show
end
# GET /splits/new
def new
@split = Split.new
end
# GET /splits/1/edit
def edit
end
# POST /splits
# POST /splits.json
def create
@split = Split.new(split_params)
respond_to do |format|
if @split.save
format.html { redirect_to @split, notice: 'Split was successfully created.' }
format.json { render :show, status: :created, location: @split }
else
format.html { render :new }
format.json { render json: @split.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /splits/1
# PATCH/PUT /splits/1.json
def update
respond_to do |format|
if @split.update(split_params)
format.html { redirect_to @split, notice: 'Split was successfully updated.' }
format.json { render :show, status: :ok, location: @split }
else
format.html { render :edit }
format.json { render json: @split.errors, status: :unprocessable_entity }
end
end
end
# DELETE /splits/1
# DELETE /splits/1.json
def destroy
@split.destroy
respond_to do |format|
format.html { redirect_to splits_url, notice: 'Split was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_split
@split = Split.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def split_params
params.require(:split).permit(:name, :description, :geography_id, :issue_id, :geographies_splits_id)
end
end
I'm completely stumped.
All help is greatly appreciated.