On Thu, Nov 13, 2008 at 6:53 PM, Makoto Kuwata <[EMAIL PROTECTED]> wrote:
>
> Is there any good way to add model.id value to 'name' and 'id' attributes?
>

I create a patch to enhance form_for(), fields_for(), and fieldset_for()
to take :index_by option which helps you to generate same controls
for several model objects.

http://merb.lighthouseapp.com/projects/7435/tickets/10

For example:

    <% @students.each do |student| %>
    <%   id = student.id %>
    <p>
      <%= text_field :name => "student[#{id}][name]", :value =>
student.name, :label => 'Name' %>
      <%= text_field :mail => "student[#{id}][mail]", :value =>
student.mail, :label => 'Mail' %>
    </p>
    <% end %>

...will be:

    <% @students.each do |student| %>
    <%=  fields_for student, :index_by => :id do %>
    <p>
      <%= text_field :name, :label => 'Name' %>
      <%= text_field :mail, :label => 'Mail' %>
    </p>
    <%   end =%>
    <% end %>

...if you apply the patch. The latter is more compact and readable
than the former.
Both generate the same HTML such as:

    <p>
      <label for="student_name_101">Name</label>
      <input type="text" name="student[101][name]"
id="student_name_101" value="Foo" />
      <label for="student_mail_101">Mail</label>
      <input type="text" name="student[101][mail]"
id="student_mail_101" value="[EMAIL PROTECTED]" />
    </p>
    <p>
      <label for="student_name_102">Name</label>
      <input type="text" name="student[102][name]"
id="student_name_102" value="Bar" />
      <label for="student_mail_102">Mail</label>
      <input type="text" name="student[102][mail]"
id="student_mail_102" value="[EMAIL PROTECTED]" />
    </p>


This way makes for controller action to handle several model objects very easy.

    def update
      params[:student].each do |id, values|
        student = Student.get(id) or NotFound
        student.update_attributes(values)
        ...
      end
    end


I hope this will help you.

--
regards,
makoto kuwata


On Thu, Nov 13, 2008 at 6:53 PM, Makoto Kuwata <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have a question about html tag helper methods.
> Is there any good way to add model.id value to 'name' and 'id' attributes?
>
> For example, assume the following view code:
>
>   <%= form_for model, :action => '/path' %>
>     <%= text_field :attr, :label=>"LABEL" %>
>   <% end =%>
>
> text_filed() helper in the above code will generates the following
> <label> and <input> tags:
>
>    <label for="model_attr">LABEL</label>
>    <input type="text" value="" class="text" name="model[attr]"
> id="model_attr"/>
>
> But I want to generate:
>
>    <label for="model_attr_123">LABEL</label>
>    <input type="text" value="" class="text" name="model[123][attr]"
> id="model_attr_123"/>
>
> (assume that model.id == 123)
>
> Is there any good way to do that?
>
> --
> regards,
> makoto kuwata
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"merb" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/merb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to