Ruby 如何将自定义函数分配给formtastic操作?
我的表格:Ruby 如何将自定义函数分配给formtastic操作?,ruby,formtastic,Ruby,Formtastic,我的表格: <%= semantic_form_for(@campaign) do |f| %> ... <%= f.actions do %> <%= f.action :submit, label: "Save"%> <%= f.action :submit, label: "Save & New" %> <%= f.action :cancel, label: "Cancel"%> <
<%= semantic_form_for(@campaign) do |f| %>
...
<%= f.actions do %>
<%= f.action :submit, label: "Save"%>
<%= f.action :submit, label: "Save & New" %>
<%= f.action :cancel, label: "Cancel"%>
<% end %>
<% end %>
Routes.rb:
resources :campaigns do
member do
post 'save_and_new'
end
end
路线,根据功能:
save_and_new_campaign POST /campaigns/:id/save_and_new(.:format) campaigns#save_and_new
我唯一不明白的是,在调用函数时要编写什么动作。我不确定你到底想用
保存和新建动作做什么,但我可以告诉你为什么不触发它
默认情况下,使用
的
语义表单创建的formtastic表单将使用RESTful约定,即对新记录执行创建操作,对现有记录执行更新操作。如果您使用第一个提交按钮(标记为“保存”)成功点击create
/update
操作,但希望第二个“Save&New”按钮执行不同的操作,则需要检查控制器中params[:commit]
的值,以处理提交。也许有些代码会更清晰。假设您正在提交以更新现有记录:
def create
if params[:commit] == "Save"
# code for handling "Save" scenario
else
# code for handling "Save & New" scenario, perhaps even directly call:
save_and_new
end
end
def update
if params[:commit] == "Save"
# code for handling "Save" scenario
else
# code for handling "Save & New" scenario, perhaps even directly call:
save_and_new
end
end
同样,我不清楚您试图通过save_and_new
操作实现什么,质疑这一假设可能会让您走上更好的设计之路,但要回答您的直接问题:检查params[:commit]的值
中的创建
或更新
将使您走上正确的道路
def create
if params[:commit] == "Save"
# code for handling "Save" scenario
else
# code for handling "Save & New" scenario, perhaps even directly call:
save_and_new
end
end
def update
if params[:commit] == "Save"
# code for handling "Save" scenario
else
# code for handling "Save & New" scenario, perhaps even directly call:
save_and_new
end
end