我目前有几个表单,我正在尝试根据我是否正在进行编辑或新操作来更改提交表单的按钮。我创建了两个表单,但这闻起来很糟糕,现在我只使用一个表单部分。
在我的部分结束时,我的表格末尾有类似这样的内容:
<p>
<% if controller.action_name == 'new' %>
<%= f.submit "Create", :class => "pink_button"%> or
<% elsif controller.action_name == 'edit' %>
<%= f.submit "Update", :class => "pink_button"%> or
<% end %>
<%= link_to "cancel", :back %>
</p>
这样,如果我要创建一些新内容,按钮会显示“创建”,如果用户尝试完成更新,按钮会显示“更新”。这非常有效,直到提交表单并且验证失败为止。
在我的 Controller 中,我发现了一些失败的事情,如下所示:
def update
@list = current_user.lists.find(params[:id])
if @list.update_attributes(params[:list])
redirect_to list_path(@list), :notice => "List '#{@list.name}' updated."
else
render :action => 'edit'
end
end
因此,表单只是重新呈现。问题是,我不再处于编辑路径上。这意味着,我的表单按钮不再显示。
我想做的事情有约定吗?
谢谢
最佳答案
是的,默认情况下这是在 Rails 中使用 i18n 处理的。翻译位于 ActionView 中,如下所示:
en:
helpers:
select:
# Default value for :prompt => true in FormOptionsHelper
prompt: "Please select"
# Default translation keys for submit FormHelper
submit:
create: 'Create %{model}'
update: 'Update %{model}'
submit: 'Save %{model}'
您需要做的就是f.submit
(无需传递“创建”或“编辑”等),翻译将完成其余的工作。您可以通过将上述 yaml 放入本地语言环境来覆盖它们。
如果你需要设置类,你可以传递nil,例如f.submit nil, :class => 'whatev'
关于ruby-on-rails - Rails 3 - 表单、基于操作的自定义提交按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7031571/