我有一个ownership模型,它属于product模型和user模型。
ownership具有以下参数:
当当前为真时,用户是产品的所有者。如果owning_date不是零,而given_date是零,则当前为真。
在我的update所有权控制器中有一个方法,定义如下:
def update
@ownership = Ownership.find(params[:id])
@ownership.update_attributes(ownership_params)
respond_to do |format|
format.html do
flash[:success] = t('flash.success.ownership.update')
redirect_to product_path(@ownership.product)
end
format.js
end
end我创建了一个同意按钮和一个接受按钮:
<%= form_for(ownership, remote: true) do |f| %>
<div><%= f.hidden_field :agreed, value: true %></div>
<%= f.submit "agree" %>
<% end %>
<%= form_for(ownership, remote: true) do |f| %>
<div><%= f.hidden_field :owning_date, value: Time.now %></div>
<%= f.submit "take" %>
<% end %>现在,我希望更改控制器中的update方法,因为当用户单击采取按钮时,以前所有权的given_date必须更改为Time.now。但是,我不希望当用户单击 click按钮时,我的更新方法会这样做。
我尝试了下面的代码,但没有起作用。单击按钮后,同一产品有两个当前的所有权。
def update
@ownership = Ownership.find(params[:id])
if @ownership.owning_date != params[:owning_date]
@ownership.product.ownerships.find_by(current: true).update_attributes(given_date: Time.now, current: nil)
end
@ownership.update_attributes(ownership_params)
respond_to do |format|
format.html do
flash[:success] = t('flash.success.ownership.update')
redirect_to product_path(@ownership.product)
end
format.js
end
end你有什么主意吗?
发布于 2013-07-16 09:23:53
也许你想要检查女巫表单是否提交,并根据用户的选择执行一些操作?
在OwnershipCcontroller#update中这样简单的检查
if params[:commit] == 'take'
# do some action
else
# do another action
endhttps://stackoverflow.com/questions/17672434
复制相似问题