form_for也提交其他提交

时间:2014-05-02 17:12:49

标签: ruby-on-rails ruby ruby-on-rails-4

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

1 个答案:

答案 0 :(得分:1)

  

现在,当我只想一次更新一个时,它既可以参加也可以通过。

因为对他们没有限制。您在if条件上提供的限制只是检查证书记录是否存在。这对于trainedpassed都是如此。

查询参数传递给创建操作,然后您可以使用该操作来确定提交的表单是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