Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails conditional form/remote_form

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?

like image 695
badnaam Avatar asked Jun 16 '26 19:06

badnaam


1 Answers

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| %>
like image 98
Dennis Collective Avatar answered Jun 19 '26 12:06

Dennis Collective



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!