0

I have two models; Letters and Representatives, and a join table called Subscriptions. I already have many Representatives in my database. I would like to associate one or more representatives to a letter when creating a new letter. I assume something needs to be added to the letters controller to create the association, but I'm not to Rails and a bit lost. Thank you in advance!

letter.rb

class Letter < ActiveRecord::Base
  has_many :subscriptions
  has_many :representatives, :through => :subscriptions
  accepts_nested_attributes_for :subscriptions, 
      reject_if: :all_blank, 
      allow_destroy: true
end

representative.rb

class Representative < ActiveRecord::Base
    has_many :subscriptions
    has_many :letters, :through => :subscriptions
end

subscription.rb

class Subscription < ActiveRecord::Base
    belongs_to :letter
    belongs_to :representative
end

subscription migration

class CreateSubscriptions < ActiveRecord::Migration
  def change
    create_table :subscriptions do |t|
      t.integer :letter_id
      t.interger :representative_id
      t.belongs_to :letter, index: true
      t.belongs_to :representative, index: true

      t.timestamps null: false
    end
  end
end

letter _form.html.erb

<%= simple_form_for @letter do |f| %>
  <% if @letter.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@letter.errors.count, "error") %> prohibited this letter from being saved:</h2>

      <ul>
      <% @letter.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :subject %><br>
    <%= f.text_field :subject %>
  </div>
  <div class="field">
    <%= f.label :body %><br>
    <%= f.text_area :body %>
  </div>
  <div class="field">
    <%= f.label 'Recipients'%><br>
    <%= f.association :representatives, as: :check_boxes,label_method: :first_name  %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

letters_controller.rb

def new
    @letter = Letter.new
end

def create
    @letter = Letter.new(letter_params)

    respond_to do |format|
    if @letter.save
        format.html { redirect_to @letter, notice: 'Letter was successfully created.' }
        format.json { render :show, status: :created, location: @letter }
    else
        format.html { render :new }
        format.json { render json: @letter.errors, status: :unprocessable_entity }
    end
  end
end
rcrusoe
  • 441
  • 2
  • 4
  • 14
  • Can you explain the current behavior is? – ruby_newbie Jun 17 '16 at 17:13
  • Current behavior: The letters/new form has a checkbox field that shows all existing representatives. I can check several of those and then create a letter, but the subscription is not created. Also, if I go to letters/X/edit, the representatives field is now all unchecked. – rcrusoe Jun 17 '16 at 17:31
  • what do your params look like coming into the controller and are you using strong params? If so can you post that code too? – ruby_newbie Jun 17 '16 at 17:32
  • def letter_params params.require(:letter).permit(:title, :subject, :body) end – rcrusoe Jun 17 '16 at 17:41
  • Does that answer your question? (Sorry, still a total rails rookie) – rcrusoe Jun 17 '16 at 17:42
  • OK so you need to allow the nested params. http://stackoverflow.com/questions/18436741/rails-4-strong-parameters-nested-objects – ruby_newbie Jun 17 '16 at 17:42
  • I changed to this: def letter_params params.permit( {:letter => [:title, :subject, :body]}, {:subscription => [:letter_id, :representative_id]} ) end and am now getting this error: unknown attribute 'letter' for Letter. – rcrusoe Jun 17 '16 at 17:52

0 Answers0