form_for标签条件+ rails3

时间:2012-11-28 20:42:45

标签: ruby-on-rails-3 form-for

我有一个form_for标记,我想根据用户状态启用其:remote => true选项

if the current user is not an admin I want the form to be :remote => true
if the current user is an admin I want the form to be a default form

这是我想出来的,它现在正在工作;(

<%= form_for @school, ((:remote => true) unless current_user.admin?) ,  :html => { :class => 'form-horizontal' } do |f| %>

可以帮助我一些人

我在

ruby 1.9
Rails 3.2.x

提前致谢

1 个答案:

答案 0 :(得分:0)

如何使用以下

的Helper方法
def form_remote_or_not(user, object, &block)
    if user.admin?
        form_for object, :remote => true, &block
    else
        form_for object, &block
    end
end

至于你的视图文件; <%= form_remote_or_not(current_user, @school) do |f| %>