我正在尝试实现连接 table 但我的变量没有正确保存。重构这个的最佳建议?
I am trying to implement a join table but my variables aren't saving properly. Best advice to refactor this?
我正在尝试构建一个应用程序,用户可以在其中 post 作业,其他用户可以接受这些作业,并在单击接受作业后使路由呈现“完成”。我已经构建了一个连接 table 来托管多个 responder_users 问题是我的接受没有保存。这是我的模型:
class User < ApplicationRecord
has_secure_password
validates :username, presence: true
validates :email, presence: true
validates :email, uniqueness: true
has_many :acceptances
has_many :job_requests, :class_name => "Job", :foreign_key => "requestor_user_id"
has_many :job_responses, :class_name => "Job", :foreign_key => "responder_user_id"
end
class Job < ApplicationRecord
has_many :acceptances
belongs_to :requestor_user, :class_name => "User"
has_many :responder_user, :class_name => "User", through: :acceptances
end
class Acceptance < ApplicationRecord
belongs_to :user
belongs_to :job
end
这是从接受作业所在的显示页面弹出的更新方法。
def update
@job = Job.find(params[:id])
@acceptance = Acceptance.create(job_id: @job.id, responder_id: @current_user.id)
@job.acceptances << @acceptance
if @job.save
redirect_to jobs_path
else
render :show
end
end
任何帮助/建议将不胜感激!
我会推荐这个..
def update
@job = Job.find(params[:id])
@acceptance = @job.acceptances.new(job_id: @job.id, user: @current_user)
if @acceptance.save
redirect_to jobs_path
else
render :show
end
end
我觉得措辞很混乱。应用程序可能会更好,一份工作有很多应用程序,一个应用程序被接受。但我希望能有所帮助。
我为那些为类似问题绞尽脑汁的人找到了解决方案。 https://www.theodinproject.com/paths/full-stack-ruby-on-rails/courses/ruby-on-rails/lessons/active-record-associations。这是一个很好的 link,可以进一步扩展您对 odin 项目托管的知识。基本上,我安排我的模型如下:(job)
belongs_to :requestor_user, :class_name => "User"
has_many :acceptances, foreign_key: :job_response_id
has_many :responder_users, through: :acceptances, source: :responder
(用户)
has_many :acceptances, foreign_key: :responder_id
has_many :job_requests, :class_name => "Job", :foreign_key => "requestor_user_id"
has_many :job_responses, through: :acceptances
(接受)
belongs_to :responder, class_name: "User"
belongs_to :job_response, class_name: "Job"
本质上,这里的目标是弄清楚如何拆分同一模型的两个不同组(在用户和工作的实例中)。从那里我能够在加入 table 中主持接受。从那里我能够创建一个响应,以及我更新路由中特定作业的响应者。:
def update
@job = Job.find(params[:id])
@current_user.job_responses << @job
@job.responder_users << @current_user
if @job.save
redirect_to jobs_path
else
render :show
end
end
为了显示完成,我只是在作业的视图中添加了一些逻辑:
<%if @acceptance%>
<h2>Completed!</h2>
<%else%>
<%= form_for(@job) do |f| %>
<%= f.submit "Accept Job" %>
<% end %>
<%end%>
感谢那些伸出援手的人!
我正在尝试构建一个应用程序,用户可以在其中 post 作业,其他用户可以接受这些作业,并在单击接受作业后使路由呈现“完成”。我已经构建了一个连接 table 来托管多个 responder_users 问题是我的接受没有保存。这是我的模型:
class User < ApplicationRecord
has_secure_password
validates :username, presence: true
validates :email, presence: true
validates :email, uniqueness: true
has_many :acceptances
has_many :job_requests, :class_name => "Job", :foreign_key => "requestor_user_id"
has_many :job_responses, :class_name => "Job", :foreign_key => "responder_user_id"
end
class Job < ApplicationRecord
has_many :acceptances
belongs_to :requestor_user, :class_name => "User"
has_many :responder_user, :class_name => "User", through: :acceptances
end
class Acceptance < ApplicationRecord
belongs_to :user
belongs_to :job
end
这是从接受作业所在的显示页面弹出的更新方法。
def update
@job = Job.find(params[:id])
@acceptance = Acceptance.create(job_id: @job.id, responder_id: @current_user.id)
@job.acceptances << @acceptance
if @job.save
redirect_to jobs_path
else
render :show
end
end
任何帮助/建议将不胜感激!
我会推荐这个..
def update
@job = Job.find(params[:id])
@acceptance = @job.acceptances.new(job_id: @job.id, user: @current_user)
if @acceptance.save
redirect_to jobs_path
else
render :show
end
end
我觉得措辞很混乱。应用程序可能会更好,一份工作有很多应用程序,一个应用程序被接受。但我希望能有所帮助。
我为那些为类似问题绞尽脑汁的人找到了解决方案。 https://www.theodinproject.com/paths/full-stack-ruby-on-rails/courses/ruby-on-rails/lessons/active-record-associations。这是一个很好的 link,可以进一步扩展您对 odin 项目托管的知识。基本上,我安排我的模型如下:(job)
belongs_to :requestor_user, :class_name => "User"
has_many :acceptances, foreign_key: :job_response_id
has_many :responder_users, through: :acceptances, source: :responder
(用户)
has_many :acceptances, foreign_key: :responder_id
has_many :job_requests, :class_name => "Job", :foreign_key => "requestor_user_id"
has_many :job_responses, through: :acceptances
(接受)
belongs_to :responder, class_name: "User"
belongs_to :job_response, class_name: "Job"
本质上,这里的目标是弄清楚如何拆分同一模型的两个不同组(在用户和工作的实例中)。从那里我能够在加入 table 中主持接受。从那里我能够创建一个响应,以及我更新路由中特定作业的响应者。:
def update
@job = Job.find(params[:id])
@current_user.job_responses << @job
@job.responder_users << @current_user
if @job.save
redirect_to jobs_path
else
render :show
end
end
为了显示完成,我只是在作业的视图中添加了一些逻辑:
<%if @acceptance%>
<h2>Completed!</h2>
<%else%>
<%= form_for(@job) do |f| %>
<%= f.submit "Accept Job" %>
<% end %>
<%end%>
感谢那些伸出援手的人!