Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add remote: => true on Form_for

In my _form.html.erb file, I have;

<%= form_for(@document) do |f| %> 
<% end %>

When I add

<%= form_for(@document), :remote => true do |f| %> 
<% end %>

I get an error. I want to add ajax to this form so the user can submit it, it'll appear with a notice saving "saved" and then the user can carry on writing in the textarea within the form.

The error says:

SyntaxError in Documents#edit

Showing /app/views/documents/_form.html.erb where line #1 raised:

<%= form_for(@document), :remote => true do |f| %>

It's saying that line 1 (above) is a syntax error.

How can I add remote true to the form_for so I can add Ajax?

Update

So out of the two answers, I have;

<%= form_for(@document, :remote => true) do |f| %>

and

<%= form_for @document, :remote => true do |f| %>

They both work but is one better than the other or do they end up doing the same thing?

like image 829
user1658756 Avatar asked Dec 15 '12 14:12

user1658756


2 Answers

You've inserted the :remote = true right AFTER the parameter list. Just leave off the parenthesis.

<%= form_for @document, :remote => true do |f| %> 
like image 130
Matzi Avatar answered Sep 30 '22 11:09

Matzi


<%= form_for(@document, :remote => true) do |f| %>
  ...
<% end %>

reefer this : http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

like image 40
Raghvendra Parashar Avatar answered Sep 30 '22 13:09

Raghvendra Parashar