需要编辑操作方面的帮助
我正在开发角色扮演游戏角色数据库应用程序,我需要一些帮助。
我有两个模型,字符模型和统计模型。每个角色都会有一个统计模型实例,该模型是一张包含 6 个独立统计数据的表。我使用了部分在角色视图上呈现统计表单,因此我可以从角色视图创建与该角色关联的新统计信息。但是,我无法编辑统计信息,并且可以生成多个实例,这都是问题。
我的问题是:
如何在统计控制器中编写编辑操作,以便可以从角色视图编辑统计实例?我还希望它覆盖任何存在的统计实例,这样我就不会得到每个角色的多组统计数据。
谢谢!
编辑:这是一些代码:
从统计控制器:
def edit
@statistic = Statistic.find(params[:id])
end
从字符视图:
%= render "statistics/form" %
以及此代码呈现的形式:
%= form_for([@character, @character.statistics.build]) do |f| %<br />
div class="field"<br />
%= f.label :strength % <br />
%= f.text_field :strength %<br />
/div<br />
div class="field"<br />
%= f.label :dexterity %br /<br />
/div<br />
div class="field"<br />
%= f.label :constitution %<br />
%= f.text_field :constitution %<br />
/div<br />
div class="field"<br />
%= f.label :intelligence %<br />
%= f.text_field :intelligence %<br />
/div<br />
div class="field"<br />
%= f.label :wisdom %<br />
%= f.text_field :wisdom %<br />
/div<br />
div class="field"<br />
%= f.label :charisma %<br />
%= f.text_field :charisma %<br />
/div<br />
div class="actions"<br />
%= f.submit %<br />
/div<br />
% end %<br />
I'm working on a roleplaying game character database app and I need some help.
I've got two models, Character and Statistic. Each Character will have one instance of the Statistic model, which is a table with 6 separate statistics. I've used a partial to render the Statistic form on the Character view, so I can create a new Statistic that is associated with that Character from the Character view. However, I can't edit the Statistic and I can generate more than one instance, which are both problems.
My questions are:
How do I code an edit action in the Statistic controller so that I can edit the instance of Statistic from the Character view? I also want this to over write any Statistic instance that is present, so that I don't end up with multiple sets of Statistics per Character.
Thanks!
EDIT: Here's some code:
From the Statistic controller:
def edit
@statistic = Statistic.find(params[:id])
end
From the Character view:
%= render "statistics/form" %
And the form this code renders:
%= form_for([@character, @character.statistics.build]) do |f| %<br />
div class="field"<br />
%= f.label :strength % <br />
%= f.text_field :strength %<br />
/div<br />
div class="field"<br />
%= f.label :dexterity %br /<br />
/div<br />
div class="field"<br />
%= f.label :constitution %<br />
%= f.text_field :constitution %<br />
/div<br />
div class="field"<br />
%= f.label :intelligence %<br />
%= f.text_field :intelligence %<br />
/div<br />
div class="field"<br />
%= f.label :wisdom %<br />
%= f.text_field :wisdom %<br />
/div<br />
div class="field"<br />
%= f.label :charisma %<br />
%= f.text_field :charisma %<br />
/div<br />
div class="actions"<br />
%= f.submit %<br />
/div<br />
% end %<br />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也很难理解你的意思,但在你的上一个问题和这个问题之间,我想我可能理解你遇到的大部分问题。
我将不再假设“统计数据”是一个表行,其中包含您正在跟踪的每个“统计数据”的列。如果是这样的话,这应该就可以了。
我确实认为,如果字符表直接将每个统计数据都放在该表上,那么事情可能会简单得多,这样表单就可以只用于一个字符,并且您只需在显示页面上为统计数据创建表单元素。
I am also having some difficulty trying to figure out what you mean, but between your last question and this one, I think I may understand most of what you are having trouble with.
I'm going off the assumption that a 'statistic' is a single table row with columns for each of the 'stats' you are tracking. If that is the case, this should about do it.
I do think that things would probably be a lot simpler if the character table just had each of the stats directly on that table, so that the form could just be for a character, and you only create form elements on the show page for the stats.