gpt4 book ai didi

javascript - 允许用户通过 ajax 志愿执行任务

转载 作者:行者123 更新时间:2023-11-28 07:11:35 26 4
gpt4 key购买 nike

我的应用程序设置为用户拥有任务并且其他用户可以自愿完成这些任务。我的模型设置如下:

用户

class User < ActiveRecord::Base
has_many :participations, foreign_key: :participant_id
has_many :owned_tasks, class_name: "Task", foreign_key: :owner_id
end

参与(加入表)

class Participation < ActiveRecord::Base
enum status: [:interested, :selected]

belongs_to :task
belongs_to :participant, class_name: "User"
end

任务

class Task < ActiveRecord::Base
enum status: [:open, :in_progress, :complete]

has_many :participations
has_many :participants, through: :participations, source: :participant
# Dynamically generates relations such as 'selected_participants'
Participation.statuses.keys.each do |status|
has_many "#{status}_participants".to_sym,
-> { where(participations: { status: status.to_sym }) },
through: :participations,
source: :participant
end

belongs_to :owner, class_name: "User"
end

我想要做的只是允许用户单击一个按钮来自愿在特定任务的显示 View 中执行任务。

我可以在 Rails 控制台中轻松完成此操作:

user = User.first
task = Task.first
user.owned_tasks << task

user_2 = User.find(2)
task.participants << user_2

我陷入困境的是试图弄清楚如何设置必要的 Controller 代码以使其正常工作。我也不确定如何/在哪里创建检查用户是否已经参与任务 is_participating? 的条件。它是进入加入模式Participation 还是Task 表?

我想我对 View 应该是什么样子有一个模糊的想法:

任务 - 显示 View

<% unless current_user == @task.owner %>
<div class="volunteer-form">
<% if current_user.is_participating? %>
<%= render 'cancel' %>
<% else %>
<%= render 'volunteer' %>
<% end %>
</div>
<% end %>

_volunteer.html.erb:

 <%= form_for(current_user.participations.build(participant_id: current_user), remote: true) do |f| %>
<div><%= f.hidden_field :participant_id %></div>
<%= f.submit "Volunteer" %>
<% end %>

_cancel.html.erb:

<%= form_for(current_user.participations.find_by(participant_id: current_user), html: { method: :delete }, remote: true) do |f| %>
<%= f.submit "Cancel" %>
<% end %>

JS

// create.js.erb
$(".volunteer-form").html("<%= escape_javascript(render('tasks/volunteer')) %>");

// destroy.js.erb
$(".volunteer-form").html("<%= escape_javascript(render('tasks/cancel')) %>");

最佳答案

从您看来,is_participating? 属于 User 模型,它可能应该是这样的:

def is_participating?(task_id)
participations.where(task_id: task_id).exists?
end

至于 Controller 代码,您可能需要以下内容:

class ParticipationsController
# Note I assume you have access to current_user here

def create
participation = current_user.participations.create(participation_params)
respond_to do |format|
format.js
end
end

def destroy
participation = current_user.participations.find(params[:id])
participation.destroy

respond_to do |format|
format.js
end
end


protected

def participation_params
params.require(:participation).permit :task_id
end
end

_volunteer.html.erb上(请注意,您现在必须将任务传递到部分):

<%= form_for(current_user.participations.build, remote: true) do |f| %>
<%= f.hidden_field :task_id, value: task.id %>
<%= f.submit "Volunteer" %>
<% end %>

关于javascript - 允许用户通过 ajax 志愿执行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31252490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com