Sebastian Plasschaert Sebastian Plasschaert - 1 month ago 9
Ajax Question

Replacing partial with AJAX in Rails 4: undefined method `id`

When replacing a partial through Ajax, one method is giving me the proper result but the other (almost identical) isn't. I want to select and deselect a group for a specific campaign. Selecting gives me a direct replacement, deselecting doesn't.

models/general_connection.rb

belongs_to :campaign
belongs_to :group


models/group.rb

has_many :general_connections
has_many :campaigns, through: :general_connections


models/campaign.rb

has_many :general_connections
has_many :groups, through: :general_connections


controllers/general_connection.rb

def connect_group_to_campaign
@group = Group.find(params[:group_id])
@campaign = Campaign.find(params[:campaign_id])
@campaign.general_connections.create(group: @group)
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end

def disconnect_group_from_campaign
@general_connection = GeneralConnection.where("group_id = ? AND campaign_id = ?", params[:group_id], params[:campaign_id]).first
@group = @general_connection.group

@general_connection.destroy
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end


views/campaign/show.html.erb

<div class="row">
<% @main_groups.each do |group| %>
<%= render partial: 'groups_form', locals: { group: group } %>
<% end %>
</div>


*views/campaign/_groups_form.html.erb

<div class="row" id="group_<%= group.id %>">
<div class="col m6">
<div class="valign-wrapper black-text">
<% if group.icon.nil? %>
<i class="material-icons">group</i>
<% else %>
<i class="material-icons"><%= group.icon %></i>
<% end %>
&nbsp;&nbsp<%= group.title %>
</div>
</div>
<div class="col m6">
<% if !grouped(group) %>
<%= form_tag connect_group_to_campaign_path(:group_id => group.id, :campaign_id => @campaign.id), remote: true do %>
<%= button_tag 'check_box_outline_blank', class: "secondary-content material-icons grey-text", style: "background-color:white;border:none;" %>
<% end %>
<% else %>
<%= form_tag disconnect_group_from_campaign_path(:group_id => group.id, :campaign_id => @campaign.id), remote: true do %>
<%= button_tag 'check_box', class: "secondary-content material-icons grey-text", style: "background-color:white;border:none;" %>
<% end %>
<% end %>
</div>
</div>


views/general_connection/connect_group_to_campaign.js.erb

$('#group_<%= @group.id %>').replaceWith('<%= j render partial: 'campaigns/groups_form', locals: {group: @group} %>');


views/general_connection/disconnect_group_from_campaign.js.erb

$('#group_<%= @group.id %>').replaceWith('<%= j render partial: 'campaigns/groups_form', locals: {group: @group} %>');


...and this is the error that I'm getting:

ActionView::Template::Error (undefined method `id' for nil:NilClass):
12: </div>
13: <div class="col m6">
14: <% if !grouped(group) %>
15: <%= form_tag connect_group_to_campaign_path(:group_id => group.id, :campaign_id => @campaign.id), remote: true do %>
16: <%= button_tag 'check_box_outline_blank', class: "secondary-content material-icons grey-text", style: "background-color:white;border:none;" %>
17: <% end %>
18: <% else %>


To sum it up: both methods are working, but only
connect_group_to_campaign
gives a direct result and
disconnect_group_from_campaign
doesn't and needs a browser refresh.

Can anyone help me out?

Answer

It seems @campaign object is not declared in disconnect_group_from_campaign method. Try it after adding the below line in your disconnect_group_from_campaign method

@campaign = Campaign.find(params[:campaign_id])