首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >接受Batman.js中的嵌套属性

接受Batman.js中的嵌套属性
EN

Stack Overflow用户
提问于 2014-07-21 03:15:40
回答 2查看 65关注 0票数 0

我使用HTML和HTTP实现了这种关系,但我完全不知道如何使用Batman.js来工作。我做了很多尝试,但都没有成功。我还试图在没有运气的情况下在本指南中实现这些步骤。谁能给我指明正确的方向?

除了控制器之外,我的路由和模型是rails文件的Batman.js等价物,我预计在这里我需要实现一些花哨的Batman.js。

路线

代码语言:javascript
复制
resources :tasks do
  resources :task_entries
end

模型

代码语言:javascript
复制
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        

控制器

代码语言:javascript
复制
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

有效的形式

代码语言:javascript
复制
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示例

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-21 04:40:01

首先,在这个公关合并之前,我不会指望batman.js来编码您的属性。相反,让batman.js以自己的方式对数据进行编码,然后将其转换到Rails控制器中。(这就是我们在计划中心办理登机手续的方式。)

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

代码语言:javascript
复制
  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模型如下所示:

代码语言:javascript
复制
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中得到澄清。

票数 0
EN

Stack Overflow用户

发布于 2014-07-21 03:32:17

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

= 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24857223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档