在Ruby on Rails中使用多态的替代方案(Alternatives to use polymorphism in Ruby on Rails)
我目前正在编写一些Intranet Web应用程序,用户可以向管理员提交添加不同资源的请求。 示例请求将是:
- 安装程序,在这种情况下,用户将选择他想要安装的程序
- 增加配额,在这种情况下,用户只需输入他需要的磁盘空间量,或者他可以选择预定义数量 - 1GB,10GB等。
- 创建新的电子邮件别名,在这种情况下,用户将只输入别名。
- ...
我在考虑只有一个UserRequests模型,引用发送者和两个可选属性,一个是reference_id,它会反映到其他表(例如他希望安装的程序),另一个将用于免费类型字段,如电子邮件别名或配额。
所以我的问题是,根据模型应该包含的请求类型:
- 参考其他表格
- 整数数据
- 字符串数据
根据请求的类型,应该采取给定的操作 - 可能会从rails中添加电子邮件别名,但用户计算机上的应用程序将手动安装。
有没有人有类似的问题? 你认为使用多态这种东西是一个好主意吗? 你对如何在表格中组织数据有任何建议吗?
I'm currently writing some intranet web application where people could submit to admins requests for adding different resources. The example requests would be:
- installing programs, in this case user will select which program he wants installed
- increasing quota, in this case user will just enter the amount of disk space he needs or maybe he will select the predefined quantities - 1GB, 10GB etc...
- create new email alias, in this case user will just type the alias.
- ...
I was thinking about having just one model UserRequests with the reference to the sender and two optional attributes one would be reference_id that would refefrence to other tables (for example the Program that he wants installed) and another would be used for free type fields like email alias or quota.
So my problem is that based on the type of the request the model should contain either:
- reference to other table
- integer data
- string data
Based on the type of the request the given action should be taken - probably email alias could be added from rails but the application on users computer will be installed by hand.
Does anyone had similar problem? Do you think using polymorphism for this kind of stuff is a good idea? Do you have any suggestions on how to organize data in the tables?
原文:https://stackoverflow.com/questions/926230
最满意答案
当你动态地向LinearLayout添加视图时,可以指定索引。
将第一个视图的高度设置为
android:layout_height="0dp" android:layout_weight="1"
在
XML
为第二个视图设置height = wrap_content
然后,当你动态地添加新的视图时,将其
height = wrap_content
设置height = wrap_content
,并将其添加到像这样的父LinearLayout
parentLinearLayout.addView(childView, index); //index = position where you want to insert the new view.
它可能会帮助你。 :)
Its possible to specify index while u dynamically add a view to a LinearLayout.
Set height of the first view as
android:layout_height="0dp" android:layout_weight="1"
Set
height = wrap_content
for the second view inXML
Then while u are adding new View dynamically, set its
height = wrap_content
and add it to the parentLinearLayout
like thisparentLinearLayout.addView(childView, index); //index = position where you want to insert the new view.
It might help you. :)
相关问答
更多-
当你动态地向LinearLayout添加视图时,可以指定索引。 将第一个视图的高度设置为 android:layout_height="0dp" android:layout_weight="1" 在XML为第二个视图设置height = wrap_content 然后,当你动态地添加新的视图时,将其height = wrap_content设置height = wrap_content ,并将其添加到像这样的父LinearLayout parentLinearLayout.addView(childV ...
-
嗨,您可以尝试设置布局参数 RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rel_btn.height = 60; rel_btn.width = 60; BtnNext.setLayoutParams(rel_btn); 您还可以通过设置相对布局参数来添加规则并 ...
-
你可以这样做: YourView varView = new YourView(this); myButton.setLayoutParams(new YourLayout.LayoutParams()); yourParentLayout.addView(varView); 示例:在父线性布局中添加按钮 Button myButton = new Button(this); myButton.setLayoutParams(new LinearLayout.LayoutParams( ...
-
您可以尝试使用ViewStub而不是手动添加它并使用setInflateId(R.layout.home)将set设置为inflateId http://developer.android.com/resources/articles/layout-tricks-stubs.html 编辑:谷歌似乎必须将android开发者从他们的android页面移动到blogspot。 更新后的链接: http : //android-developers.blogspot.com/2009/03/android-la ...
-
你可以用LayoutInflater服务来做到这一点。 通过像这样加载它来获得对它的引用: LayoutInflater inflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); 然后你可以像这样从一个XML资源创建一个布局: View view = inflater.inflate( R.layout.layoutname, null ); 这里的layoutname只是res/la ...
-
是应该在xml布局中还是以编程方式绘制重复视图?(Should repetitive views be drawn in xml layout or programmatically?)[2022-05-29]
我只有一个标签,并为每个单元格提供一个ID。 有关更多信息,请访问: http : //developer.android.com/training/improving-layouts/reusing-layouts.html I would like to answer my question with the suggestion form 323go and my latest observation. When the views are being repeated and th ... -
你还没有明确提到你是否正在向ViewPager提供Fragment ,但是由于你是在使用Zylinc实现,我会假设你是。 事实上,我只是使用提供的示例代码向您展示这个想法。 理解如何实现您所追求的目标的关键是使ViewPager中的页面成为Fragment形式的自包含单元。 它们是可重用的,有自己的生命周期,并且可以处理自己的布局。 因此,如果要对页面布局进行运行时修改,则需要在构成这些页面的Fragment内执行此操作。 可以在示例代码的ItemFragment类中找到以下方法。 我只是将您自己的代码片段 ...
-
检查这个.. RelativeLayout main = new RelativeLayout(this); main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); TextView textV = new TextView(this); textV.setGravity(Gravity.LEFT); RelativeLayout ...
-
android-以编程方式截取特定视图的屏幕截图不起作用(android- Taking screenshot of a particular view programmatically does not work)[2024-02-05]
如果问题是getDrawingCache返回null,那么只需在v.buildDrawingCache(true);之前添加这行代码v.buildDrawingCache(true); v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); v.layout(0, 0, v.getMeasur ... -
没有内置的东西。 您必须编写自己的View -hierarchy-to-XML代码,或者查看某人是否有一个库。 或者,使用Android Studio的布局检查器 。 或者, 使用uiautomatorviewer 。 或者, 使用Stetho 。 There is nothing built in for that. You would have to write your own View-hierarchy-to-XML code or see if somebody has a library fo ...