Ruby on Rails Monday, June 29, 2015

On Jun 29, 2015, at 8:18 AM, Padmahas Bn <padmahas@gmail.com> wrote:

> Hello Elizabeth McGurty that worked!!! (with little modification)
>
> Unique id for each modal generated fine but when calling that modal the the link_to didn't worked for me.
>
> I had to change this
> <td><%= link_to 'Show', "data-toggle" => "modal", :class => 'btn btn-default', :id => designation_dict.dd_id %></td>
>
> To this:
> <td><%= render :partial => "show", :locals => {:designation_dict => designation_dict} %></td>
>
> <td><%= link_to 'Show', designation_dict, "data-target" => "#myModal_#{designation_dict.id}", "data-toggle" => "modal", :class => 'btn btn-default' %></td>

A quick tip for generating HTML ID attributes from Rails: there is a helper method named #dom_id. You use it like this: dom_id(designation_dict), and by default, you get [object classname]_[numerical id] as a string. It takes an optional prefix as the second argument, which gets prepended, so if you did dom_id(designation_dict, :edit), you would get edit_designation_dict_42 out of it. This comes in really handy when you are setting up an ID in a view, and then again in a UJS file or similar. You don't have to fiddle around with a lot string concatenation in two places that way.

Walter

>
> Working like a charm. Thank you a lot. I was searching for this from past 20 days.
>
> Thank you again.
>
> On Mon, Jun 29, 2015 at 12:27 AM, Elizabeth McGurty <emcgurty2@gmail.com> wrote:
> <td><%= render :partial => "show", :locals => {:designation_dict => designation_dict} %></td>
>
> In your locals you are sending designation_dict. Is there something in designation_dict that is unique to all the records? Hopefully an id...
>
> Do you have that unique field, let's call it dd_id?
>
> Now this is going to seem to be Ad nauseam, and Mr Law refers to id="MyModal", which I do not see, yet he is entirely correct. What I understand is that you have an each statement that is iteratively loading...A partial, and numerous link_to
>
> For later, however you need it as params or js elements, each of these elements must contain -- at some required element level -- a unique html id.
>
> Look at the changes I made to your partial:
>
> <div class="modal-body" id="modal-body_<%= designation_dict.dd_id %>">
> <div class="table-responsive" id="table-responsive_<%= designation_dict.dd_id %>">
> <table class="table table-striped table-show">
> <tr>
> <th>
> <strong>Designation code:</strong>
> </th>
> <td>
> <%= designation_dict.desig_code %>
> </td>
> </tr>
>
> <tr>
> <th>
> <strong>Designation description:</strong>
> </th>
> <td>
> <%= designation_dict.designation %>
> </td>
> </tr>
> </table>
> </div>
>
> Do you see now how you are generating unique html ids?
>
> <td><%= link_to 'Show', "data-toggle" => "modal", :class => 'btn btn-default', :id => designation_dict.dd_id %></td>
>
>
> Once you make these changes, and then View Page Source, you will see that a unique id has been generated.... Folks will probably offer more elegance solutions, but the spirit remains the same.
>
> Hope this helps...
>
> Liz
>
>
> On Monday, May 4, 2015 at 10:47:06 AM UTC-4, Padmahas Bn wrote:
> After following so many Stackoverflow and other related posts I was able to render modal (_show.html.erb) from inside index.html.erb. But the problem the parameter I'm sending is showing same id and other details for all show buttons.
>
> For instance if there are 5 different members listed in index.html.erb, when I press the corresponding show button, the same id and other details as the first member is showing for all members.
>
> index.html.erb
> <tbody>
> <% @designation_dicts.each do |designation_dict| %>
> <tr>
> <td><%= designation_dict.desig_code %></td>
> <td><%= designation_dict.designation %></td>
>
> <td><%= render :partial => "show", :locals => {:designation_dict => designation_dict} %></td>
> <td><%= link_to 'Show', "#myModal", "data-toggle" => "modal", :class => 'btn btn-default' %></td>
> <td><%= link_to 'Edit', edit_designation_dict_path(designation_dict), :class => 'btn btn-default' %> </td>
> <td><%= link_to 'Destroy', designation_dict, method: :delete, data: { confirm: 'Are you sure?' } , :style => 'color:#FFFFFF', :class => 'btn btn-danger' %></td>
> </tr>
> <% end %>
> </tbody>
>
> Part of the modal (_show.html.erb)
> <div class="modal-body"> <div class="table-responsive">
> <table class="table table-striped table-show">
> <tr>
> <th>
> <strong>Designation code:</strong>
> </th>
> <td>
> <%= designation_dict.desig_code %>
> </td>
> </tr>
>
> <tr>
> <th>
> <strong>Designation description:</strong>
> </th>
> <td>
> <%= designation_dict.designation %>
> </td>
> </tr>
> </table>
> </div>
>
> Thank you
>
> --
> 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/17d2a1b5-f7ed-40b7-acf0-daf0a70340a0%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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/CAC_h78%2BSVSs_8uU%3DGAHYOHtiKijn7U4oiy%3DjV55HDyP-x5dADw%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

--
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/21DD5BFE-B6C0-493B-B747-4DD20089C67D%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment