【发布时间】:2015-09-27 12:28:33
【问题描述】:
我想通过 ajax 更新表单提交而不重新加载页面并根据它更新视图。我尝试了不同的方法,但由于我是 Rails 新手而失败了。
情况是这样的。
在模态中
def new
@new_entry = current_user.notes.build
@words = current_user.notes
@notes_false_list = current_user.notes.where(known: false).order("title").group_by { |note| note.title[0] }
@notes_true_list = current_user.notes.where(known: true).order("title").group_by { |note| note.title[0] }
end
在查看表单代码。
<%= form_for @new_entry, :html => {:class => 'home-form without-entry clearfix'} do |f| %>
<%= f.text_field :title, placeholder: 'Squirrel', autofocus: true, class: 'title-field pull-left' %>
<%= f.submit '', class: 'btn home-add without-entry' %>
<% end %>
创建动作
def create
@new_note = current_user.notes.build(new_note_params)
if @new_note.save
@notes_list = current_user.notes.count
unless @new_note.user.any_entry
@new_note.user.toggle!(:any_entry) if @notes_list >= 50
redirect_to root_url
end
else
redirect_to root_url
end
end
最后在我想要改变的视图中
<p class="instructions count-instruction text-center">
<%= @words.count %>
</p>
花了很多时间用 AJAX 更新它,但每次都失败了。有什么可以帮忙的吗?提前致谢。
【问题讨论】:
-
您尝试了什么,请在此处发布?
-
嘿@Deep 好吧,我有一个表单,它只是从用户那里获取标题并在数据库上更新它,并且在下面显示没有标题计数。我不想在提交表单时重新加载页面。所以我想用 AJAX 来做。
标签: javascript jquery ruby-on-rails ajax forms