接受Batman.js中的嵌套属性

时间:2014-07-21 03:15:40

标签: javascript ruby-on-rails coffeescript batman.js

我有这种关系使用HTML和HTTP,但我完全失去了使用Batman.js的工作。我做了大量的尝试,但都没有成功。我还试图在没有运气的情况下实施this guide中的步骤。谁能指出我正确的方向?

除了控制器之外,我的路由和模型是相当于rails文件的Batman.js,我预计我需要实现一些花哨的Batman.js

路线

resources :tasks do
  resources :task_entries
end

模型

class Task < ActiveRecord::Base
  has_many :task_entries

  accepts_nested_attributes_for :task_entries

  # must have at least one entry, built in controller
  validates :task_entries, presence: true
end


class TaskEntry < ActiveRecord::Base
  belongs_to :task
  has_one :item

  accepts_nested_attributes_for :item

  # must have an item, built in controller
  validates :item, :task, presence: true
end

class Item < ActiveRecord::Base
  belongs_to :parent, polymorphic: true

  validates :title, presence: true
end        

控制器

class TasksController < ApplicationController
  def new
    @task = Task.new
    @task.task_entries.build(item: Item.new)
  end

  def task_params
    returned_params = params.require(:task).permit(
      task_entries_attributes: [
        :status, :due_at, :defer_until, :estimated_duration, :completed_at, 
        item_attributes: [
          :title, :description, :flagged, :visibility, :difficulty
        ]
      ]
    )
  end
end

有效的表格

tasks/_form.html.slim
= form_for @task do |f|
  = f.fields_for :task_entries do |e|
    = e.fields_for :item do |i|
      = i.text_field :title
      = i.text_area :description
      = i.check_box :flagged
      = i.select :visibility, Item.visibility.options
      = i.select :difficulty, Item.difficulty.options
    = e.select :status, TaskEntry.status.options
    = e.datetime_select :due_at
    = e.datetime_select :defer_until
    = e.number_field :estimated_duration
    = e.datetime_select :completed_at
  = f.submit

有效的HTTP示例

POST /tasks HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost:3000

task[task_entries_attributes][0][item_attributes][title]=help+me+stack+overflow%2C+you%27re+my+only+hope

2 个答案:

答案 0 :(得分:0)

表单似乎不正确,项目属于任务条目,而不是任务

= form_for @task do |f| = f.fields_for :task_entries do |e| = e.fields_for :item do |i| = i.text_field :title = i.text_area :description = i.check_box :flagged = i.select :visibility, Item.visibility.options = i.select :difficulty, Item.difficulty.options = e.select :status, TaskEntry.status.options = e.datetime_select :due_at = e.datetime_select :defer_until = e.number_field :estimated_duration = e.datetime_select :completed_at = f.submit

答案 1 :(得分:0)

首先,我要说在合并this PR之前,我不会依靠batman.js对您的属性进行编码。相反,让batman.js以自己的方式对数据进行编码,然后在Rails控制器中对其进行转换。 (这就是我们在Planning Center Check-Ins中的表现。)

要转换Rails控制器中的params,请更新task_params方法:

  def task_params
    # first, permit the params without the _attributes suffix:
    returned_params = params.require(:task).permit(
      task_entries: [
        :status, :due_at, :defer_until, :estimated_duration, :completed_at, 
        items: [
          :title, :description, :flagged, :visibility, :difficulty
        ]
      ]
    )

    # then, reassign them to different keys in the params hash (and delete the old keys)
    if returned_params[:task_entries].present?
      task_entries_params = returned_params.delete(:task_entries)
      returned_params[:task_entries_attributes] = task_entries_params
      # do the same for the items params
      if returned_params[:task_entries_attributes][:items].present?
        items_params = returned_params[:task_entries_attributes].delete(:items)
        returned_params[:task_entries_attributes][:items_attributes] = items_params
      end
    end
  end

这假设batman.js模型看起来像这样:

class MyApp.Task extends Batman.Model 
  # ...
  @hasMany "task_entries", saveInline: true 

class MyApp.TaskEntry extends Batman.Model 
  # ...
  @encode "status", "due_at", "defer_until", "estimated_duration", "completed_at"
  @hasOne "item", saveInline: true, polymorphic: true, as: "parent"

class MyApp.Item extends Batman.Model 
  # ...
  @encode "title", "description", "flagged", "visibility", "difficulty"
  @belongsTo "parent", polymorphic: true

我同意改变参数并不理想。希望这将在batman.js 0.17中清除。