0

So, I am making a todo list app using Rails and Ajax . I have a form for each todo task in my "lists/show.html.erb" file . Here is some code from it :

<h3>Unfinished Tasks</h3>
<div class="tasks" id="incomplete_tasks">
  <% @list.tasks.incomplete.each do |task| %>
      <%= form_for [current_user, @list, task], remote: true do |f| %>
          <%= f.label :completed, class: 'checkbox' do %>
             <%= f.check_box :completed %>
             <%= task.description %>
          <% end %>
         <%= f.submit "Update" %>        <!--We hide this button in jquery-->
      <% end %>
  <% end %>
</div>

The problem with this is that multiple forms are created in the same page and have input checkboxes with same id, i.e the input field(checkbox) in all the forms have the same input id which is automatically created by Rails . This makes the label tag useless because we need a unique id in the for attribute.

<label for="">

So, Is there any way to change the default id creation by adding a prefix so that the input tag for each form is unique ? Since this is a pretty common issue,Is there a "Rails way" to solve this "label" issue ?

P.S: This is a followup question to this http://goo.gl/bB9D6

On a sidenote , how do I send an error report to Railscasts(Ryan Bates)( That is where I got this code).

4

1 回答 1

1

好的,这很简单。我刚刚使用了以下代码:

  <%= form_for [current_user, @list, task], remote: true do |f| %>
      <%= f.label 'completed_'+task.object_id.to_s, class: 'checkbox' do %>
          <%= f.check_box :completed ,id:'task_completed_'+task.object_id.to_s%>   <!--have overridden default id because label works only if there is a unique id in each page-->
          <%= task.description %>
      <% end %>
      <%= f.submit "Update" %>        <!--We hide this button in jquery-->
  <% end %>
<% end %>
于 2013-06-17T15:14:48.397 回答