2015-03-26 4 views
32

Original FrageRails 4 Form: has_many durch: Kontrollkästchen

Zwei Ressourcen: users und animals. Beim Erstellen eines Benutzers, wählt der Client-Check-Boxen zu sagen, wie viele Tiere, die sie haben. Wenn das Benutzerformular übergeben wird, sollte es nicht nur einen neuen user Datensatz erstellen, sondern es sollte auch eine Reihe von Datensätzen in der Rich Join-Tabelle animal_users erstellen, um jedes der vom Client ausgewählten Kontrollkästchen darzustellen.

Das Problem, das ich denke, ist, dass ich nicht etwas richtig für den Checkboxteil innerhalb des Formulars spezifiziere. Ich habe mir die checkbox_tag API, die Rails Guides on Forms, und viele Websites und StackOverflow Beiträge angesehen.

Vielen Dank im Voraus, Code unten:

Originalcode (Antwortcode weiter unten):

Modelle:

#models/user.rb 
class User < ActiveRecord::Base 
    has_many :animals, through: :animal_users 
    has_many :animal_users 
    accepts_nested_attributes_for :animal_users, allow_destroy: true 
end 

#models/animal.rb 
class Animal < ActiveRecord::Base 
    has_many :users, through: :animal_users 
    has_many :animal_users 
end 

#models/animal_user.rb 
class AnimalUser < ActiveRecord::Base 
    belongs_to :animal 
    belongs_to :user 
end 

Die user Form:

#views/users/_form.html.erb 
    <%= form_for(@user) do |f| %> 
    <div class="field"> 
    <%= f.label :name %><br> 
    <%= f.text_field :name %> 
    </div> 

    <div> 
    <% Animal.all.each do |animal| %> 
    <label> 
     <%= check_box_tag "user[animal_ids][]", animal.id, f.object.animals.include?(animal) %> 
     <%= animal.animal_name %> 
    <% end %> 
    </div> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

Starke params im users_controller.rb

def user_params 
     params.require(:user).permit(:name, animal_users_attributes: [:_destroy, :id, :user_id, :animal_id]) 
end 

Antwort Code hier:

Modelle:

#models/user.rb 
class User < ActiveRecord::Base 
    has_many :animals, through: :animal_users 
    has_many :animal_users 
end 

#models/animal.rb 
class Animal < ActiveRecord::Base 
    has_many :users, through: :animal_users 
    has_many :animal_users 
end 

#models/animal_user.rb 
class AnimalUser < ActiveRecord::Base 
    belongs_to :animal 
    belongs_to :user 
end 

Die user Form:

#views/users/_form.html.erb 
<%= form_for(@user) do |f| %> 
    <div class="field"> 
    <%= f.label :name %><br> 
    <%= f.text_field :name %> 
    </div> 

    # Checkbox part of the form that now works! 
    <div> 
     <% Animal.all.each do |animal| %> 
     <%= check_box_tag "user[animal_ids][]", animal.id, f.object.animals.include?(animal) %> 
     <%= animal.animal_name %> 
     <% end %> 
    </div> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

Starke params im users_controller.rb

def user_params 
    params.require(:user).permit(:name, animal_ids: []) 
    end 

Und nur aus Gründen der Vollständigkeit, hier ist das, was ich s in den Server auf Formular-Vorlage übergeben:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"xyz=", "user"=>{"name"=>"Neil", "animal_ids"=>["1", "3"]}, "commit"=>"Create User"} 
Animal Load (0.2ms) SELECT "animals".* FROM "animals" WHERE "animals"."id" IN (1, 3) 
    (0.1ms) begin transaction 
SQL (0.2ms) INSERT INTO "users" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", "2015-03-26 15:29:00.478767"], ["name", "Neil"], ["updated_at", "2015-03-26 15:29:00.478767"]] 
SQL (0.1ms) INSERT INTO "animal_users" ("animal_id", "created_at", "updated_at") VALUES (?, ?, ?) [["animal_id", 1], ["created_at", "2015-03-26 15:29:00.479833"], ["updated_at", "2015-03-26 15:29:00.479833"]] 
SQL (0.0ms) INSERT INTO "animal_users" ("animal_id", "created_at", "updated_at") VALUES (?, ?, ?) [["animal_id", 3], ["created_at", "2015-03-26 15:29:00.480644"], ["updated_at", "2015-03-26 15:29:00.480644"]] 
SQL (0.1ms) UPDATE "animal_users" SET "updated_at" = ?, "user_id" = ? WHERE "animal_users"."id" = 6 [["updated_at", "2015-03-26 15:29:00.481362"], ["user_id", 8]] 
SQL (0.1ms) UPDATE "animal_users" SET "updated_at" = ?, "user_id" = ? WHERE "animal_users"."id" = 7 [["updated_at", "2015-03-26 15:29:00.482062"], ["user_id", 8]] 
    (2.4ms) commit transaction 
+1

In 'User' Modell versuchen Sie' access_nested_attributes_for: animal_users', schauen Sie sich diesen Beitrag http://stackoverflow.com/questions/21983131/rails-4-nested-attributes-and-has-many-through -associaton-in-a-form – Sontya

+0

@Sontya Vielen Dank für Ihre Antwort. Ich denke, ich komme näher, obwohl noch nicht ganz da. – Neil

+1

ändere deine Ansicht '<% ​​Animal.all.each do | animal | %> \t <% = f.fields_for: animal_users do | ff | %> \t \t <% = ff.check_box: tier_id, {}, animal.id%> \t \t <% = Tier.Name%> \t <% end %> ' – Sontya

Antwort

28

collection_check_boxes ist eine bessere Option:

<%= f.collection_check_boxes(:animal_ids, Animal.all, :id, :name) do |animal| %> 
    <%= animal.label { animal.check_box } %> 
<% end %> 
+2

Dies ist so erstaunlich einfach und einfach. +1 und danke! –