一个表单中的多个hidden_​​field?

时间:2016-05-11 00:10:43

标签: javascript ruby-on-rails ruby polymorphic-associations hidden-field

ruby​​不允许在一个表单中使用两个hidden_field吗?即使用户针对具有3

的挑战更改了排名值,排名值仍会保存为date_started.present?

如果我取出<%= f.hidden_field(:notes_date, value: date) %>,则根据用户的选择正确保存该值,但当然我不能只取出该行,因为我需要它来设置日期。

备注/形式

<%= form_for [@notable, @note] do |f| %>
  <%= f.text_area :notes_text, placeholder: 'Enter Note' %>
  <%= f.hidden_field(:ranking, id: 'rank') %> # Defaults ranking to 3, but if user changes it in the form it still gets saved as 3 for date_started.present? challenges

  <% if @challenge.date_started.present? %>
    <%= f.hidden_field(:notes_date, value: date) %>
  <% else %>
    <%= f.date_select :notes_date, :order => [:month, :day] %>
  <% end %>

  <%= image_tag('1.png', data: { ranking: 1 }, class: 'image-clicker') %>
  <%= image_tag('2.png', data: { ranking: 2 }, class: 'image-clicker') %>
  <%= image_tag('3.png', data: { ranking: 3 }, class: 'image-clicker clicked') %>
  <%= image_tag('4.png', data: { ranking: 4 }, class: 'image-clicker') %>

  <%= f.submit %>
<% end %>

<script>
  $('.image-clicker').click(function() {
    $('#rank').val($(this).data('ranking'));
    $('.clicked').removeClass('clicked')
    $(this).addClass('clicked');
  });
</script>

呈现备注/表单

  <% if @challenge.date_started %>
    <% @challenge.dates_challenged.first(@challenge.days_challenged + @challenge.missed_days).each_with_index do |date, i| %>
      <% if @notes.any? { |note| note.notes_date.strftime("%m/%d/%y") == date.strftime("%m/%d/%y") } %>
        <% @notes.each do |note| %>
          <% if note.notes_text.present? %>
            <% if note.notes_date.strftime("%m/%d/%y") == date.strftime("%m/%d/%y") %>
              <div class="notes-notes-background">
                <% if note.ranking == 1 %>
                  <%= image_tag '1.png', class: 'note-emoticon' %>
                <% elsif note.ranking == 2 %>
                  <%= image_tag '2.png', class: 'note-emoticon' %>
                <% elsif note.ranking == 3 %>
                  <%= image_tag '3.png', class: 'note-emoticon' %>
                <% elsif note.ranking == 4 %>
                  <%= image_tag '4.png', class: 'note-emoticon' %>
                <% end %>
                <div style="font-weight: bold; font-size: 18px;">Day <%= i + 1 %></div>
                <b><%= note.notes_date.strftime("%b %d, %Y") %></b><br><br>
                <%= link_to edit_note_path(note) do %>
                  <%= simple_format note.notes_text %>
                <% end %>
              </div>
            <% end %>
          <% else %>
          <% end %>
        <% end %>
      <% else %>
        <div class="notes-form-background">
          <div style="margin-bottom: -23px; color: #a4a99f; font-weight: bold; font-size: 18px; color: #446CB3;">Day <%= i + 1 %></div><br>
          <div style="color: #a4a99f;"><%= date.strftime("%m/%d") %></div>
          <%= render 'notes/form', :date => date %> # For date_started date is passed through to form
        </div>
      <% end %>
    <% end %>
  <% else %>
    <% @notes.each do |note| %>
      <div class="notes-notes-background">
        <% if note.ranking == 1 %>
          <%= image_tag '1.png', class: 'note-emoticon' %>
        <% elsif note.ranking == 2 %>
          <%= image_tag '2.png', class: 'note-emoticon' %>
        <% elsif note.ranking == 3 %>
          <%= image_tag '3.png', class: 'note-emoticon' %>
        <% elsif note.ranking == 4 %>
          <%= image_tag '4.png', class: 'note-emoticon' %>
        <% end %>
        <b><%= note.notes_date.strftime("%b %d, %Y") %></b><br><br>
        <%= link_to edit_note_path(note) do %>
          <%= simple_format note.notes_text %>
        <% end %>
      </div>
    <% end %>
    <div class="notes-form-background">
      <%= render "notes/form" %> # For nil date_started date is not passed to form
    </div>
  <% end %>

日志

# NOTE FOR CHALLENGE WITHOUT DATE_STARTED (AKA GOAL CHALLENGE)
# NOTE CORRECTLY SAVED RANKING

Started POST "/challenges/175-take-a-picture/notes" for 127.0.0.1 at 2016-05-11 14:34:39 -0400
Processing by NotesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"YYad25L/yIqbgnnWgD6ufHRBuAgQzc5kQZZ6TqvzNbDu2wQMnZ4yI2F8SdHoRLVBD/IOueXh6cJCGJFSVFoY5w==", "note"=>{"notes_text"=>"adfasdfasdf", "notes_date"=>"2016-05-08", "ranking"=>"1", "conceal"=>"0"}, "button"=>"", "challenge_id"=>"175-take-a-picture"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 78]]
  Challenge Load (0.5ms)  SELECT  "challenges".* FROM "challenges" WHERE "challenges"."id" = $1 LIMIT 1  [["id", 175]]
   (0.3ms)  BEGIN
  SQL (0.3ms)  INSERT INTO "notes" ("notes_text", "ranking", "notes_date", "user_id", "challenge_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["notes_text", "adfasdfasdf"], ["ranking", 1], ["notes_date", "2016-05-08 04:00:00.000000"], ["user_id", 78], ["challenge_id", 175], ["created_at", "2016-05-11 18:34:39.351527"], ["updated_at", "2016-05-11 18:34:39.351527"]]
   (0.6ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO "activities" ("action", "trackable_id", "trackable_type", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["action", "create"], ["trackable_id", 146], ["trackable_type", "Note"], ["user_id", 78], ["created_at", "2016-05-11 18:34:39.357991"], ["updated_at", "2016-05-11 18:34:39.357991"]]
   (0.6ms)  COMMIT
Redirected to http://0.0.0.0:3000/challenges/175-take-a-picture
Completed 302 Found in 19ms (ActiveRecord: 3.2ms)

# NOTE FOR CHALLENGE WITH DATE_STARTED (AKA HABIT CHALLENGE)
# NOTE DID NOT SAVE RANKING

Started POST "/challenges/175-take-a-picture/notes" for 127.0.0.1 at 2016-05-11 14:38:05 -0400
Processing by NotesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"AmISZHocVRAxMgg50oiQZm4GT8m04uGJ2AH1iCE6jSeNP4uzdX2vucvMOD668otbFbX5eEHOxi/bjx6U3pOgcA==", "note"=>{"notes_text"=>"super sad face with 1 ranking", "notes_date"=>"2016-05-11", "ranking"=>"", "conceal"=>"0"}, "button"=>"", "challenge_id"=>"175-take-a-picture"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 78]]
  Challenge Load (1.8ms)  SELECT  "challenges".* FROM "challenges" WHERE "challenges"."id" = $1 LIMIT 1  [["id", 175]]
   (3.6ms)  BEGIN
  SQL (0.3ms)  INSERT INTO "notes" ("notes_text", "notes_date", "user_id", "challenge_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["notes_text", "super sad face with 1 ranking"], ["notes_date", "2016-05-11 04:00:00.000000"], ["user_id", 78], ["challenge_id", 175], ["created_at", "2016-05-11 18:38:05.365702"], ["updated_at", "2016-05-11 18:38:05.365702"]]
   (15.8ms)  COMMIT
   (0.2ms)  BEGIN
  SQL (0.4ms)  INSERT INTO "activities" ("action", "trackable_id", "trackable_type", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["action", "create"], ["trackable_id", 148], ["trackable_type", "Note"], ["user_id", 78], ["created_at", "2016-05-11 18:38:05.388367"], ["updated_at", "2016-05-11 18:38:05.388367"]]
   (16.9ms)  COMMIT
Redirected to http://0.0.0.0:3000/challenges/175-take-a-picture
Completed 302 Found in 61ms (ActiveRecord: 39.3ms)

HTML输出

        <div class="notes-form-background">
          <div style="margin-bottom: -23px; color: #a4a99f; font-weight: bold; font-size: 18px; color: #446CB3;">Day 20</div><br>
          <div style="color: #a4a99f;">05/30</div>
          <form class="new_note" id="new_note" action="/challenges/176-meditate/notes" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="O1vx9h2yTuKmv6R/GyN8msppVp5iX+cMSIAbyWUsJKS0BmghEtO0S1xBlHhzWWensdrgL5dzwKpLDvDVmoUJ8w==" />

    <button name="button" type="submit" class="remove-note">
      <span class="glyphicon glyphicon-remove"></span>
</button>
  <div class="form-group">
    <textarea rows="4" class="form-control" id="challenge-name" placeholder="Enter Note" name="note[notes_text]">
</textarea>
    <div class="center">
          <input value="05-30" type="hidden" name="note[notes_date]" id="note_notes_date" />
    </div>
    <div class="emoticons-align-center">
      <span class="label label-primary" style="vertical-align: text-top;">How was it?</span>
      <input id="rank" type="hidden" name="note[ranking]" />
      <img data-ranking="1" class="image-clicker" src="/assets/1-d13146fa38dc5aa389b7136023e0a70d.png" alt="1" />
      <img data-ranking="2" class="image-clicker" src="/assets/2-e855957467934964a571061ed2f87edf.png" alt="2" />
      <img data-ranking="3" class="image-clicker" src="/assets/3-f39b8a225ddd8b1b9633c147ab4c605f.png" alt="3" />
      <img data-ranking="4" class="image-clicker" src="/assets/4-bc78072f299a492c5411b6e47cb85adf.png" alt="4" />
    </div>
    <div style="margin-top: 30px;">
      <div style="float: left;">
        <input name="note[conceal]" type="hidden" value="0" /><input data-size="small" data-on-color="danger" data-on-text="Secret" data-off-text="Public" class="someclass" type="checkbox" value="1" name="note[conceal]" id="note_conceal" /> 
        <script>
          $("[class='someclass']").bootstrapSwitch();
        </script>
      </div>
      <div style="float: right;">
        <button name="button" type="submit" class="btn">
          Save
</button>      </div>
    </div>
  </div>
</form>
<script>
  $('.image-clicker').click(function() {
    $('#rank').val($(this).data('ranking'));
    $('.clicked').removeClass('clicked')
    $(this).addClass('clicked');
  });
</script>

1 个答案:

答案 0 :(得分:0)

Rails没有任何例外,只允许表单的一个隐藏字段。你可以拥有多个。在我看来,你的问题存在于其他地方,也许它并没有像你期望的那样将值设置到隐藏字段。

要对此进行测试,请将隐藏字段设置为普通文本字段,并查看值是否更改。

如果这种情况发生了变化,那么可能会在控制器中执行一个断点,看看你得到了什么作为参数。