我使用HTML和HTTP实现了这种关系,但我完全不知道如何使用Batman.js来工作。我做了很多尝试,但都没有成功。我还试图在没有运气的情况下在本指南中实现这些步骤。谁能给我指明正确的方向?
除了控制器之外,我的路由和模型是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发布于 2014-07-21 04:40:01
首先,在这个公关合并之前,我不会指望batman.js来编码您的属性。相反,让batman.js以自己的方式对数据进行编码,然后将其转换到Rails控制器中。(这就是我们在计划中心办理登机手续的方式。)
要转换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中得到澄清。
发布于 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
https://stackoverflow.com/questions/24857223
复制相似问题