ZK Zhao
ZK Zhao

Reputation: 21573

Rails: remote form with conditional

I'm using _form both in index and edit. The problem is that, in index, it's a remote form, and in edit, I want it to be an ordinary form.

Perhaps something like

= form_for post, remote: true if params[:action] != 'edit' , html: { class: 'post-form' } do |f|

How can I do that?

Upvotes: 2

Views: 508

Answers (3)

Sonalkumar sute
Sonalkumar sute

Reputation: 2575

Try like this

= form_for post, ({ remote: true } if params[:action] == 'edit') do |f|

Upvotes: 0

vee
vee

Reputation: 38645

Here is a slightly different syntax:

= form_for post, remote: (action_name == 'index'), html: { class: 'post-form' } do |f|

You just want either remote: true or remote: false.

Upvotes: 2

ZK Zhao
ZK Zhao

Reputation: 21573

This is what I end up with: When calling

= render 'posts/form', post: @post, remote_control: false

And in _form

- remote_control = true if remote_control.nil?
= form_for post, remote: remote_control , html: { class: 'post-form' } do |f|

Upvotes: 0

Related Questions