如何通过ajax更新模型属性true或false

时间:2013-02-14 13:22:48

标签: ruby-on-rails jquery ruby-on-rails-3.2

我正在使用这样的表单:

<%= form_for student.account, remote: true do |f| %>
  <% student.account.attributes.each do |key, value| %>
    <span class="btn btn-danger student-payment" id="account-<%= key %>" name="account[<%= key %>]" checked="true"><%= key %></span>
  <% end %>
<% end %>

这给了我一定数量的按钮。在模型中,对于每个键,默认值为false,按下按钮,值应更改为true(和返回)。

按钮的咖啡脚本:

$('.student-payment').click ->
  btn = $(this)
  btn.parent('form').submit()

表格的ajax:

$('.edit-account').submit ->
  $.ajax({
    type:   this.method,
    url:    this.put,
    data:   $(this).serialize(),
    success:    ->
      alert 'Success'
    });
  return false

帐户控制器:

def update
  @account = Account.find(params[:id])
   if @account.update_attributes?(params[:account])
     respond_to do |format|
       format.js
     end
   end
end

现在,它做了一些事情,但是firebug-console显示了这个错误:

Unknown action
The action 'update' could not be found for AccountsController

路线基本上是这样的:

resources :users
resources :students
resources :schools
resources :accounts

我做错了什么? 提前谢谢!

修改

我构建了一个小解决方法:现在,每个按钮都有一个隐藏的queckbox,它是在按钮单击时触发的。通过这种方式,可以访问数据库。暂时工作。

0 个答案:

没有答案