2011-05-05 73 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