最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add remote: => true on Form_for - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Dec 15, 2012 at 14:21 user1658756user1658756 1,0245 gold badges13 silver badges27 bronze badges 1
  • The ONLY difference is aesthetic. Omitting the parentheses is a matter of style preference. – Jesse the Game Commented Nov 13, 2013 at 13:38
Add a comment  | 

3 Answers 3

Reset to default 16

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

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

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

Also with namespace you can use

<%= form_for [:namespace, @document], html: { help: :block }, remote: true do |f| %>
  ...
<% end %>
发布评论

评论列表(0)

  1. 暂无评论