I have has_many through for User, Prog and Enrollment models. Idea is
that
User is able to add Prog to his profile and Prog is able to remove
association from Enrollment or update status attribute.
def create
@prog = Prog.find(params[:id])
Enrollment.create(user_id: current_user.id, prog_id: @prog.id,
status:
“pending”)
redirect_to @prog, notice: “Programme is added”
end
def accept
@enrollment = Enrollment.find_by_user_id(params[:id])
@enrollment.status = “accepted”
@enrollment.save!
redirect_to progs_path, notice: “added”
end
<% @prog.users.each do |user| %>
<%= link_to “#{user.email} accept”, controller: “/enrollments”, action:
“accept”, method: “post”, id: user.id, id: @prog %>/
<% end %>
I understand that accept method is lack of @prog to find right column in
Enrollment table, but I don’t undestand how do I pass this id?
Vitalis wrote in post #1110889:
<%= link_to “#{user.email} accept”, controller: “/enrollments”, action:
“accept”, method: “post”, id: user.id, id: @prog %>/
I understand that accept method is lack of @prog to find right column in
Enrollment table, but I don’t undestand how do I pass this id?
<%= link_to “#{user.email} accept”, controller: “/enrollments”,
action: “accept”, method: “post”, id: user.id, prog_id: @prog.id %>
before_filter :load_prog
def create
Enrollment.create(user_id: current_user.id, prog_id: @prog.id, status:
“pending”)
redirect_to @prog, notice: “Programme is added”
end
def accept
@enrollment.status = “accepted”
@enrollment.save!
redirect_to progs_path, notice: “added”
end
private
def load_prog
@prog = Prog.find(params[:prog_id])
end
If you decide to use a before_filter as shown here make sure you pass
the Prog ID as “prog_id” for all actions.
P.S. It is generally considered good practice to avoid abbreviations in
domain objects. What is a “Prog”? Could be Program, could be
Progression, etc. If you mean “Program” then use “Program” as your class
name and variable references. There are exceptions to every rule, such
as Proc objects and abbreviations that are very well understood by
everyone. it’s still good practice to avoid them whenever there is a
possibility of ambiguity.