Can't get 'has_and_belongs_to_many' to work
Made a previous post about this but after a lot of testing and changing
it's outdated.
I'm trying to tie users and activities together.(One user can have several
activities. An activity can have several users). I'ts pretty much a
multi-user agenda thing.
Whenever i create a new activity i can select users by checkbox which will
be doing the activity.
I can't get it to work though; The users won't show up in my activity
show. Printing @activity.users in my show results in
#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_User:0x43d2158>
but looping it, or checking my activities.js shows nothing(The
activities.js shows "users":[] in the activity. So I'm pretty certain
they're not associated properly.
Here's some code:
The activities.js index method
def index
@activities = Activity.all
respond_to do |format|
format.html
format.js {render_json @activities.to_json(:include => [:pictogram
,:users]) }
end
end
The activities' form(Loops all users)
<%= form_for(@activity) do |f| %>
<% if @activity.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@activity.errors.count, "error") %> prohibited
this activity from being saved:</h2>
<ul>
<% @activity.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :start_date %><br>
<%= f.date_select :start_date %>
</div>
<div class="field">
<%= f.label :end_date %><br>
<%= f.date_select :end_date %>
</div>
<div class="users">
<% for user in User.all %>
<label class="activity">
<%= check_box_tag "activity[user_ids][]", user.id %>
<%= user.name %>
</label>
<% end %>
</div>
<div class="pictograms">
<% for p in Pictogram.all %>
<% #f.fields_for :pictograms do |x| %>
<%= p %>
<%
No comments:
Post a Comment