Rails conditional form / remote_form

I have a partial form that should display remote_form_for or form_for depending on the value of the local variable passed to it from the view of the caller. Seem to be...

<% 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 get a syntax error of about <% else%> because it expects an “end”.

What is the right way to do this?

+3
source share
1 answer

you can create 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 will be just

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

Source: https://habr.com/ru/post/1748561/


All Articles