当通过Ajax替换部分时,一种方法给我正确的结果,但另一种方法(几乎相同)不是。我想为特定广告系列选择和取消选择一个组。选择给我一个直接的替代,取消选择没有。
模型/ general_connection.rb
belongs_to :campaign
belongs_to :group
模型/ group.rb
has_many :general_connections
has_many :campaigns, through: :general_connections
模型/ campaign.rb
has_many :general_connections
has_many :groups, through: :general_connections
控制器/ 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
视图/运动/ show.html.erb
<div class="row">
<% @main_groups.each do |group| %>
<%= render partial: 'groups_form', locals: { group: group } %>
<% end %>
</div>
*视图/运动/ _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 %>
 <%= 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>
视图/ general_connection / connect_group_to_campaign.js.erb
$('#group_<%= @group.id %>').replaceWith('<%= j render partial: 'campaigns/groups_form', locals: {group: @group} %>');
视图/ general_connection / disconnect_group_from_campaign.js.erb
$('#group_<%= @group.id %>').replaceWith('<%= j render partial: 'campaigns/groups_form', locals: {group: @group} %>');
...这是我得到的错误:
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 %>
总结一下:两种方法都有效,但只有connect_group_to_campaign
给出直接结果而disconnect_group_from_campaign
没有,需要浏览器刷新。
任何人都可以帮助我吗?
答案 0 :(得分:0)
似乎@campaign
对象未在disconnect_group_from_campaign
方法中声明。在disconnect_group_from_campaign
方法
@campaign = Campaign.find(params[:campaign_id])