2011-05-05 105 views
0

我正在制作角色扮演游戏角色数据库应用程序,我需要一些帮助。需要编辑操作帮助

我有两个模型,Character和Statistic。每个角色将具有统计模型的一个实例,该统计模型是具有6个单独统计的表格。我使用了partial来在Character视图上渲染统计表单,因此我可以创建一个与Character视图中的Character相关联的新统计信息。但是,我无法编辑统计信息,并且可以生成多个实例,这两个实例都是问题。

我的问题是:

如何在统计控制器代码的编辑操作,这样我可以从角色视图编辑统计的实例?我也希望这可以重写任何存在的统计实例,这样我就不会得到多组每个字符的统计信息。

谢谢!

编辑:下面是一些代码:

从统计控制器:

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 /> 
+0

当然可以。以下是我对Statistic控制器编辑操作的要求: – illbzo1 2011-05-05 00:27:12

+0

@bacchus谢谢,我很快意识到600个字符是不够的! – illbzo1 2011-05-05 00:35:09

+1

@bacchus感谢您的编辑。我也为轨道添加了轨道。 – illbzo1 2011-05-05 00:54:16

回答

0

我是al所以在试图弄清楚你的意思时有一些困难,但是在你最后一个问题和这个问题之间,我想我可以理解你遇到的大部分问题。

我假设'统计信息'是一个单独的表格行,包含您正在跟踪的每个'统计信息'的列。如果是这样的话,那就应该这样做。

# character.rb 
class Character < ActiveRecord::Base 
    has_one :statistic 
end 

# statistic.rb 
class Statistic < ActiveRecord::Base 
    belongs_to :character 
end 

# characters_controller 
def show 
    @character = Character.find(params[:id]) 
end 

# characters#show.html.erb 
<h1><%= @character.name %></h1> 
<%= form_for @character.statistic do |f| %> 
    <fieldset> 
    <label>Statistics</label> 
    <%= f.text_field :strength %> 
    <%= f.text_field :dexterity %> 
    ... 
    <%= f.submit 'Update' %> 
    </fieldset> 
<% end %> 

# statistics_controller.rb 
def update 
    @statistic = Statistic.find(params[:id]) 
    if @statistics.update_attributes(params[:statistics]) 
    redirect_to character_path(@statistic.character, :notice => 'Updated stats' 
    else 
    redirect_to character_path(@statistic.character, :error => 'Could not update' 
    end 
end 

我认为,事情可能会简单得多,如果字符表只是相依为命直接在统计上表,以便在窗体可能只是一个字符,你只创建表单元素在统计数据的显示页面上。

+0

真棒,感谢您的帮助!就统计角色而言,我考虑过这个问题,但我还有其他元素,比如战斗能力,技能,装备等等,我不想把所有这些东西加载到角色表中。我的想法是,如果我能弄清楚如何操作一个附加模型,我可以推断代码并将其用于其他模型。 – illbzo1 2011-05-05 11:13:58

+0

现在更有意义,如果是这样的话,保持它的独立性。 – Unixmonkey 2011-05-05 12:35:47