2014-10-11 72 views
-1

我有一个表单,我的用户可以选择一个日期和一个地点来创建新的预订。
每个预订可以有几个景点。如何在创建表单中添加多个隐藏项目

class Reservation < ActiveRecord::Base 
    has_many :spotations 
    has_many :spots, :through => :spotations 
end 
class Spotation < ActiveRecord::Base 
     belongs_to :reservation 
     belongs_to :spot 
end 
class Spot < ActiveRecord::Base 
    has_many :spotations 
    has_many :reservations, :through => :spotations 
end 

现在我给我的用户一个select_list来选择一个点。 只有可用斑点显示在该列表中。 (只有当天没有为该物品预订的景点)

<%= f.collection_select :spot_ids, @availablespots, :id, :name, {:prompt => true}, {class: "form-control spot_selection", id:"reservation_spot_ids", name:"reservation[spot_ids]", required:"required" } %> 

一切正常。

但我让我的模型接受几个景点: 我想预订用户选择的地点和之前的地点自动(旅行时间)。

我不知道如何在我的控制器中做到这一点。

我所有的景点都是按照id排序的。 (1到20),如果用户选择可用的点id = 4,我也想预订id = 3。

有人有想法吗? 谢谢,

+0

在创建操作中,您可以找到现场n-1并将其添加到模型中。 – phoet 2014-10-11 22:14:41

+0

Thx您的回复。一旦将它发送到创建过程,我如何访问我的:spot_ids params(散列)? (为了得到n-1),然后我怎么把这个点(我找到的n-1)放回到创建过程中呢? – Devlesch 2014-10-11 23:07:04

回答

0

我想你在想更困难的方式.. 'n'是用户选择的数字。 然后,'n-1'可以在你的控制器中创建。

总是看到所有的传递PARAMS你操作之前, 使用<% logger.debug "PARAM CHECKER: #{params.inspect}" %>

后您考虑如何让您从哈希类型参数想要什么,你可以创建n-1

+0

Thx为您的答案。我明白什么是n和n-1。 但我有几个问题: 首先,我怎样让这个哈希PARAM,然后我知道如何让n-1和最后如何注入新的N-1的值到哈希创造我的问题之前发送。 – Devlesch 2014-10-12 03:30:10

+0

@Devlesch:你只需调用'params'即可。 – seoyoochan 2014-10-12 11:39:34