Ruby on Rails 4
我的表单是编辑表证书。该表单根据出席和传递属性显示用户。我有一个按钮,用户通行证和/或参加更新证书表。
现在,当我只想一次更新一个时,它既可以参加也可以通过。我可以用@ trained / @传递/ @ untrained / @ unpassed放置它,只使它运行与实例变量的f.submit相关的代码。
所以提交if form_for @passed
提交只运行if @passed。像if @passed.nil? == false
之类的东西或者有没有办法用form_for发送ID?
我的表格:
<%= form_for @untrained, :url => certificates_path(@untrained) do |f| %>
<p> Trained Users </p>
<%= select_tag "certificate[user_id]", options_for_select(@current_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Un-Train", class: "btn btn-large btn-primary" %>
<% end %>
<%= form_for @trained, :url => certificates_path, :method => :post do |f| %>
<p> Non-Trained Users </p>
<%= select_tag "certificate[user_id]", options_for_select(@non_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Train", class: "btn btn-large btn-primary" %>
<% end %>
<%= form_for @unpassed, :url => certificates_path(@unpassed) do |f| %>
<p> Certified Users </p>
<%= select_tag "certificate[user_id]", options_for_select(@current_passed.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Un-Certify", class: "btn btn-large btn-primary" %>
<% end %>
<%= form_for @passed, :url => certificates_path, :method => :post do |f| %>
<p> Non-Certified Users </p>
<%= select_tag "certificate[user_id]", options_for_select(@non_passed.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Certify", class: "btn btn-large btn-primary" %>
<% end %>
控制器:
def create
if @trained = Certificate.where(user_id: params[:certificate][:user_id]).first
@trained.update_attributes(attend: "Yes")
else
@trained = Certificate.new(params[:certificate])
if @trained.save
@trained.update_attributes(attend: "Yes")
end
end
if @passed = Certificate.where(user_id: params[:certificate][:user_id]).first
@passed.update_attributes(pass: "Yes")
else
@passed = Certificate.new(params[:certificate])
if @passed.save
@passed.update_attributes(pass: "Yes")
end
end
redirect_to grandstreamers_resellers_path
end
def update
if @trained = Certificate.where(user_id: params[:certificate][:user_id])
@trained.first.update_attributes(attend: "No")
redirect_to grandstreamers_resellers_path
elsif @untrained = Certificate.where(user_id: params[:certificate][:user_id])
@untrained.first.update_attributes(attend: "No")
redirect_to grandstreamers_resellers_path
end
if @passed = Certificate.where(user_id: params[:certificate][:user_id])
@passed.first.update_attributes(pass: "No")
redirect_to grandstreamers_resellers_path
elsif @unpassed = Certificate.where(user_id: params[:certificate][:user_id])
@unpassed.first.update_attributes(pass: "No")
redirect_to grandstreamers_resellers_path
end
end
答案 0 :(得分:1)
现在,当我只想一次更新一个时,它既可以参加也可以通过。
因为对他们没有限制。您在if
条件上提供的限制只是检查证书记录是否存在。这对于trained
和passed
都是如此。
将查询参数传递给创建操作,然后您可以使用该操作来确定提交的表单是trained
还是passed
。例如:
更新form_for
和@trained
的{{1}},如下所示:
@passed
然后在创建操作中检查<%= form_for @trained, :url => certificates_path(flag: :attend), :method => :post do |f| %>
<%= form_for @passed, :url => certificates_path(flag: :pass), :method => :post do |f| %>
例如:
更新params[:flag]
操作如下:
create