Ruby on Rails Tuesday, June 4, 2013

Hello!

In my app i have a model Adventure, which has many Quests (and has "accepts_nested_attributes_for :quests, :allow_destroy => true" ). My form for adventure looks like this:
<%= form_for(@adventure) do |f| %>
  <% if @adventure.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@adventure.errors.count, "error") %> prohibited this adventure from being saved:</h2>

      <ul>
      <% @adventure.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.hidden_field :is_special_event, :value => @is_special %>

  <div class="field">
    <% unless @is_special %>
       Minimal player level<br />
      <%= f.number_field :minimal_level %>
    <% end %>
  </div>

  <div class="field">
    Name:<br />
    <%= f.text_field :name, :id => 'adv_name' %>
  </div>
  <div class="field">
    Description<br />
    <%= f.text_area :description, :id => 'adv_desc' %>
  </div>
<br/><h2>Story</h2>
<div id="quests" ng-controller="QuestsController" ng-init="quests = <%= @adventure.quests.to_json %>">
<div ng-repeat="quest in quests">
<%= render 'quest_fields' %>
</div>
<a href="#quest" class="new-quest" ng-click="add()">New +</a>

</div>

  <div class="actions">
    <%= f.submit 'Save', :class => 'submit-btn' %>
  </div>
<% end %>

And the _quest_feilds.html.erb file
<div id="quest-{{$index}}" ng-controller="QuestController" ng-init="quest_index = $index">
<%= hidden_field_tag 'adventure[quests_attributes][{{quest_index}}][order]', :value => '{{$index}}' %>

<br/>Title:<br/>
<%= text_field_tag 'adventure[quests_attributes][{{quest_index}}][title]', '{{quest.title}}', :size => 150, id: 'quest_{{$index}}_title' %>
<br/>Description<br/>
<%= text_area_tag 'adventure[quests_attributes][{{quest_index}}][description]','{{quest.description}}', :cols => 107, :rows => 10, id: 
  'quest_{{$index}}_description' %>
 
And the problem is, when i'm trying to update any Adventure, instead of updating the quests, it keeps old records and is simply adding modified ones. So, instead of two records, when i'll modify the adventure, i have four records. Does anybody have an idea ?

Thanks in advance! 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c6313689-f42d-4293-bc55-e4df006815dc%40googlegroups.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment