2

Another Update: here is my development log. I'm confused about 2 things.

  1. 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.
  2. 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.

Scott S.
  • 749
  • 1
  • 7
  • 26
  • Show us your controller. In particular, are you permitting the check box values? Plus, capture `params` at the top of the controller action in question to confirm that they are getting sent from the view. It wouldn't hurt to update your post with the view in question as well. – steve klein Sep 02 '15 at 21:33
  • You shouldn't need a separate controller for your association. Can you show us the controller for the failing create action? I suppose this would be the split controller. Also, it is likely either the checkboxes are not being passed in `params` as you expect or they are not permitted. You should be able to determine which it is from your logs and some debugging. – steve klein Sep 02 '15 at 22:11
  • I only added a controller for the association because I thought I'd need to whitelist the id's going into that table. – Scott S. Sep 02 '15 at 23:38
  • The controller just connects the view to the model. If you create an association an a view, you can whitelist the parameters through the association. Check out section 4.5 Strong Parameters in [this Rails Guide](http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters) for more detail. – steve klein Sep 03 '15 at 12:35
  • I think I identified the problem. My log file shows "Unpermitted parameter: geography_ids". But I searched my project and the only place that appears is in the log file. I'm trying to figure out where that's coming from. It should simply be "geography_id" – Scott S. Sep 03 '15 at 15:50
  • If it is a `has_many` association, it will be plural. Just include the association plus attributes in your permitted parameter list. If you dump your `params`, you'll see the plural association plus attributes coming over from your view. Also, if you haven't already done so, I would strongly recommend reading section 4.5 Strong Parameters referenced above (pun intended). – steve klein Sep 03 '15 at 15:53
  • I removed the unnecessary "has_many" associations in the models and that didn't do it. "has_and_belongs_to_many" is clearly plural. So I tried renaming the column in geographies_splits to "geography_ids" since that's what's in the log and whitelisted it in the controller but that just caused other problems. It's also not what the Rails Guides said in their example. I'm not seeing an answer in section 4.5. I'm embarrassingly dense today. – Scott S. Sep 03 '15 at 16:30

1 Answers1

2

Ok, using this post I was able to fix it.

I needed to add { geography_ids:[] } to my whitelist in the splits controller.

Thanks for the help.

Community
  • 1
  • 1
Scott S.
  • 749
  • 1
  • 7
  • 26