简体   繁体   English

如何在Rails中实现呢?

[英]How Can I implement this in Rails?

I want to implement a feature where user can "add additional details" to his profile. 我想实现一个功能,用户可以在其中向自己的个人资料“添加其他详细信息”。 Here he should be able to create the label for the detail and the actual details like: 在这里,他应该能够为明细和实际明细创建标签,例如:

Education : Degree 

where Education is the label for the detail and Degree is the detail. 其中教育是细节的标签,而学位是细节。

Apart from this, he should also have an option to decide whether this details should be made visible or hidden . 除此之外,他还应该有一个选择权,可以决定是否应visiblehidden此详细信息。

How can I implement this using a new model Profile with the association User has_one Profile. 如何使用带有关联用户has_one配置文件的新模型配置文件来实现此目的。

If I just had Label and Text for the new details, I could have tried hash , but since I would also have to get the details from the user on whether the user wants the detail to be made hidden or visible, I might require an extra field to store that value (true or false). 如果我只是使用Label和Text来获取新的详细信息,则可以尝试使用hash ,但是由于我还必须从用户那里获取有关用户希望将其隐藏还是可见的详细信息,因此可能需要额外的操作字段以存储该值(真或假)。

I am really confused as to how I can get the whole thing implemented together. 我对如何使整个事情实现在一起感到非常困惑。

Please suggest me how I can implement this and also how can I update the model each time a user creates a new detail without changing the schema of the db. 请建议我如何实现此目标,以及每次用户创建新的详细信息而不更改数据库的schema时如何更新模型。

I am working on Rails 3.2. 我正在使用Rails 3.2。

I don't really see your problem (or I don't get it), but: 我看不到您的问题(或者我不明白),但是:

Why don't you just create a has_many AdditionalData with user_id:integer name:string content:string visible:boolean ? 为什么不使用user_id:integer name:string content:string visible:boolean创建has_many AdditionalData

So you can loop through @user.additional_datas.visible (assuming you defined a scope scope :visible, where("visible = 1") . 因此,您可以遍历@user.additional_datas.visible (假设您定义了范围scope :visible, where("visible = 1")

Added benefit: You could make this model polymorph and add additional information to other things you have in your app without the need to create an extra "information" table for every model you want to store these. 额外的好处:您可以使该模型变形,并向应用程序中的其他内容添加其他信息,而无需为要存储这些模型的每个模型创建额外的“信息”表。

For validation, you could also add a data_type field and create validations according to the data type (needs to be an url, a phone number, just text, ...) 为了进行验证,您还可以添加data_type字段并根据数据类型创建验证(需要是url,电话号码,仅是文本等)。

I don't see the problem too; 我也看不出问题。 I think an hash-like table could be the solution, something like this: 我认为像哈希表这样的解决方案可能是这样的:

rails generate model UserDetails label:string value:string \
  visible:boolean user:references

in which you put records like this: 在其中放置这样的记录:

user.details.create(:label => 'Dog name', :value => 'Fuffy' :visible => false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM