需要编辑操作方面的帮助

发布于 2024-11-05 04:06:04 字数 1402 浏览 0 评论 0原文

我正在开发角色扮演游戏角色数据库应用程序,我需要一些帮助。

我有两个模型,字符模型和统计模型。每个角色都会有一个统计模型实例,该模型是一张包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2024-11-12 04:06:04

我也很难理解你的意思,但在你的上一个问题和这个问题之间,我想我可能理解你遇到的大部分问题。

我将不再假设“统计数据”是一个表行,其中包含您正在跟踪的每个“统计数据”的列。如果是这样的话,这应该就可以了。

# 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

我确实认为,如果字符表直接将每个统计数据都放在该表上,那么事情可能会简单得多,这样表单就可以只用于一个字符,并且您只需在显示页面上为统计数据创建表单元素。

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.

# 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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文