views:

40

answers:

1

I have a form partial that needs to render a remote_form_for or a form_for depending on the value of a local variable passed into it from the caller view. It looks like...

<% if ajax  %>
  <% remote_form_for @search, :url => {:action => :search_set, :controller => :searches, :stype => stype} do |f| %>
  <% else %>
    <% form_for @search, :url => {:action => :search_set, :controller => :searches, :stype => stype} do |f| %>
    <% end  %>

Obviously, I am getting a syntax error near the <%else %>, because its expect an "end".

What's the right way to do this?

A: 

you could make a helper method

def form_or_remote_form_for object, *opts, &proc
  if ajax
    remote_form_for object, *opts, &proc
  else
    form_for object, *opts, &proc
  end
end

and then in your views it'd just be

<% form_or_remote_form_for @search, :url => {:action => :search_set, :controller => :searches, :stype => stype} do |f| %>
Dennis Collective