简体   繁体   English

Rails form_for has_many通过使用AJAX的关联不起作用

[英]Rails form_for has_many through association using AJAX not working

I'm having issues with a form_for that builds an association. 我在建立关联的form_for中遇到问题。 I have an Event model, a User model and a Attendee model. 我有一个事件模型,一个用户模型和一个与会者模型。 The code for the models are as follows: 这些模型的代码如下:

class User < ActiveRecord::Base
    has_many :activities, class_name: "Attendee", dependent: :destroy
    has_many :events, through: :activities

class Event < ActiveRecord::Base
    has_many :attendees, dependent: :destroy
    has_many :users, through: :attendees

class Attendee < ActiveRecord::Base
    belongs_to :user
    belongs_to :event

On the show page for an event I have a button to join the event which creates the association between a user and event. 在事件的显示页面上,我有一个按钮可以加入事件,从而创建用户与事件之间的关联。

<div id="join_event">
    <% if current_user.events.include?(@event) %>
        <%= render 'leave_event' %>
    <% else %>
        <%= render 'join_event' %>
    <% end %>
</div>

The join_event and leave_event partials are: join_event和leave_event部分为:

join_event: join_event:

<%= form_for(current_user.activities.build(event_id: @event.id), remote: true) do |f| %>
    <%= f.hidden_field :event_id %>
    <%= f.submit "Join event" %>
<% end %>

leave_event: leave_event:

<%= form_for(current_user.activities.find_by(event_id: @event.id), html: { method: :delete }, remote: true) do |f| %>
    <%= f.submit "Leave event" %>
<% end %>

The @event in the partials above is in the show method in the events_controller: 上面的局部变量中的@event在events_controller中的show方法中:

def show
    @event = Event.find(params[:id])
end

The create and destroy methods in the attendees_controller are as follows: joinees_controller中的create和destroy方法如下:

def create
    @user_event = Event.find(params[:attendee][:event_id])
    current_user.events.push(@user_event)
    respond_to do |format|
        format.html { redirect_to event_path(@user_event) }
        format.js
    end
end

def destroy
    @user_event = Attendee.find(params[:id])
    current_user.activities.destroy(@user_event)
    respond_to do |format|
        format.html { redirect_to root_path }
        format.js
    end
end

The issue is that when I click the "Join event" button noting happens. 问题是,当我单击“加入事件”按钮时,会发生提示。 The error that shows up in the Chrome dev tools under the 'Network' tab says: Chrome开发者工具“网络”标签下显示的错误是:

undefined method `id' for nil:NilClass
Extracted source (around line #1):
    <%= form_for(current_user.activities.find_by(event_id: @event.id), html: { method: :delete }, remote: true) do |f| %>
        <%= f.submit "Leave event" %>
    <% end %>

The highlighted row is the first row with the "current_user.activities.find_by(event_id: @event.id)". 高亮显示的行是带有“ current_user.activities.find_by(event_id:@ event.id)”的第一行。 When I check the database (I'm using PostgreSQL) the association has been created and if I hit the refresh button the leave_event partial is rendered. 当我检查数据库时(我正在使用PostgreSQL),关联已创建,并且如果单击刷新按钮,则将显示Leave_event部分。 So the issue seems to be that using AJAX I am unable to access the id of the event show page. 因此,问题似乎在于,使用AJAX我无法访问事件显示页面的ID。 The same issue presents itself when I try to click the "Leave event" button. 当我尝试单击“离开事件”按钮时,也会出现相同的问题。 Nothing happens and the error that shows up in the Chrome dev tools under the 'Network' tab says: 没有任何反应,Chrome开发人员工具在“网络”标签下显示的错误表明:

undefined method `id' for nil:NilClass
Extracted source (around line #1):
    <%= form_for(current_user.activities.build(event_id: @event.id), remote: true) do |f| %>
        <%= f.hidden_field :event_id %>
        <%= f.submit "Join event" %>
    <% end %>

The association is destroyed but the "Join event" button isn't rendered because the form_for doesn't seem to be able to access the id from the url (in this particular case the url is localhost:3000 /events/34) 关联被销毁,但未显示“加入事件”按钮,因为form_for似乎无法从URL访问ID(在这种情况下,URL为localhost:3000 / events / 34)

So my question is, how can I get the form_for to access the event id which is in the url? 所以我的问题是,如何获取form_for以访问URL中的事件ID? The association is created and the correct form renders as long as I hit the refresh button on the browser, but I'm unable to get it to work using AJAX. 只要按下浏览器上的“刷新”按钮,就会创建关联并呈现正确的表单,但是我无法使用AJAX使其正常工作。

Just in case it's needed, the create.js.erb and destroy.js.erb files(located in 'views/attendees') are: 万一需要,create.js.erb和destroy.js.erb文件(位于“视图/与会者”中)为:

$("#join_event").html("<%= escape_javascript(render('events/leave_event')) %>")

$("#join_event").html("<%= escape_javascript(render('events/join_event')) %>")

I believe the problem is you're calling @event from a partial, when you should be using locals : 我相信问题是您应该使用locals时从局部调用@event

<%= render partial: "join_event", locals: { event: @event } %>

#app/views/controller/_join_event.html.erb
<%= form_for(current_user.activities.build(event_id: event.id), remote: true) do |f| %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM