2011-11-21 92 views
0

我有模式叫Product,它有以下几列:模型有两种不同的形式是安全的吗?

create_table :products do |t| 
    t.decimal :price 
    t.string :name 
    t.integer :offline_store_id 
    t.integer :online_store_id 
    t.date :product_date 
end 

现在我做了两个不同的表格可以看到有关联:

# using find_or_create_by 
webstore = online_store_id 
store = offline_store_id 

<%= form_for @product do |f| %> 
    <%= f.text_field :price %> 
    <%= f.text_field :name %> 
    <%= f.date_select :product_date 
    <%= f.text_field :webstore %> 
<% end %> 

另一种形式是,它只是切换相同代替:webstore:store。原因是为在线产品制作一个表格,另一个用于离线。我担心的是,如果该字段仍然可以填写或不填写,即使其中没​​有任何一种可用。产品不能同时属于在线和离线商店。

这是一件好事吗?即使我没有可用的字段,该字段是否真的消失了,或者黑客仍然可以填写它?

回答

3

您如何建模您的域和用户界面是与黑客/安全相关的问题单独关注的问题。如果需要的话,每个模型都有多个表单是完全正确的。你是安全问题应该从你的域名模型分开处理

我会建议看看你如何建模产品。你的产品应该只知道它的属性,并且它属于一个商店。让Store类包含它的离线/在线状态。

此外,我认为你在这张表格上做得太多了(看起来你正在产品表单上创建商店)。我建议为商店/产品管理制定单独的表单,并将产品上的商店字段设置为选择列表。

迁移

create_table :products do |t| 
    t.decimal :price 
    t.string :name 
    t.integer :store_id 
    t.date :product_date 
end 

create_table :stores do |t| 
    t.boolean :online 
    ..... 

型号

class Product < ActiveRecord::Base 
    belongs_to :store 

class Store < ActiveRecord::Base 
    has_many :products 

具有相同的形式,但与此元素:

online_status = true # switch this depending on online status 
collection_select(:product, :store_id, Store.find_all_by_online(online_status), :id, :name, :prompt => 'Please select store') 
+0

谢谢,太感激的额外信息。我会尝试使用它。 – LearningRoR