Send parameters while rendering partial (_show.html.erb, which contains modal) from index.html.erb

47 views
Skip to first unread message

Padmahas Bn

unread,
May 4, 2015, 10:47:06 AM5/4/15
to rubyonra...@googlegroups.com
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

Colin Law

unread,
May 4, 2015, 11:46:44 AM5/4/15
to rubyonra...@googlegroups.com
On 4 May 2015 at 15:47, Padmahas Bn <padm...@gmail.com> 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>

Do you mean that when you click this link it does not show the correct
one? If so then have a look at the html the for the link in the
browser (View Source, or something similar in the browser to see it,
Ctrl+U in Firefox) to see if it is correct and look in development.log
to see what parameters are passed for it when you click it.

Colin

Padmahas Bn

unread,
Jun 25, 2015, 1:30:04 AM6/25/15
to rubyonra...@googlegroups.com


Do you mean that when you click this link it does not show the correct
one?  If so then have a look at the html the for the link in the
browser (View Source, or something similar in the browser to see it,
Ctrl+U in Firefox) to see if it is correct and look in development.log
to see what parameters are passed for it when you click it.

Colin

Hello colin,

   I saw the View Source option as well as inspect element  in the firefox. Both of them contain corresponding values of their respective rows' in MODAL. What would
   be the problem to show the values as it is ? Why it is showing the same values for other show button when clicked, while different MODAL contains different values?

Here is the "View source" of two MODAL. I've pasted first and second row values and deleted some rows of code such as styles etc. so ignore the number of opening and closing tags like <div> etc. Please tell me if any other information is needed.

Row 1

<td>1</td>
<td>Commissioner</td>
	
<td>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title" id="myModalLabel">1</h4>
      </div>
      <div class="modal-body">
        <div class="table-responsive">
          <table class="table table-striped table-show">
	  <tr>
	   <th>
	     <strong>Designation code:</strong>
	   </th>
				
	   
<td>
	                 1                                                  #Value should be changed here in the next row

</td> </tr> <tr> <th> <strong>Designation description:</strong> </th>
<td>
	               Commissioner                                 #Value should be changed here in the next row
</td> </tr> </table> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </td> <td><a data-target="#myModal" data-toggle="modal" class="btn btn-default" href="/designation_dicts/1">Show</a></td>
				
<tr>

Row 2

<td>2</td>
<td>Assistant Commissioner</td>
				
<td>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title" id="myModalLabel">2</h4>
      </div>
      <div class="modal-body">
        <div class="table-responsive">
         <table class="table table-striped table-show">
	  <tr>
	   <th>
	    <strong>Designation code:</strong>
	   </th>
			
	   
<td>
	                   2                                              #Value changed here

</td> </tr> <tr> <th> <strong>Designation description:</strong> </th>
<td>
	              Assistant Commissioner                    #Value changed here
                   </td>
	  </tr>
	 </table>
	</div>				    
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>        
      </div>   
</td>

Thank you.

Padmahas Bn

unread,
Jun 25, 2015, 1:46:45 AM6/25/15
to rubyonra...@googlegroups.com
Hey Colin I forgot about development.log in my previous message. Here is the copy paste from development log.

Here I clicked show button of 8th row. According to development.log, at first it started rendering /designation_dicts/8 but it seems some internal error occurred. I've pasted the part after I clicked the 8th row.

Development.log

Started GET "/designation_dicts/8" for ::1 at 2015-06-25 11:07:20 +0530
Processing by DesignationDictsController#show as HTML
  Parameters: {"id"=>"8"}
  [1m [35mDesignationDict Load (1.0ms) [0m  SELECT  "designation_dicts".* FROM "designation_dicts" WHERE "designation_dicts"."id" = $1 LIMIT 1  [["id", 8]]
Completed 500 Internal Server Error in 23ms

ActionView::MissingTemplate (Missing template designation_dicts/show, application/show with {:locale=>[:en], :formats=>[:html, :html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder]}. Searched in:
  * "E:/VirtualBoxShared/testproj/app/views"
):
  actionview (4.2.0) lib/action_view/path_set.rb:46:in `find'
  actionview (4.2.0) lib/action_view/lookup_context.rb:121:in `find'
  C:in `find_template'
  actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:40:in `determine_template'
  actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:8:in `render'
  actionview (4.2.0) lib/action_view/renderer/renderer.rb:42:in `render_template'
  actionview (4.2.0) lib/action_view/renderer/renderer.rb:23:in `render'
  actionview (4.2.0) lib/action_view/rendering.rb:100:in `_render_template'
  actionpack (4.2.0) lib/action_controller/metal/streaming.rb:217:in `_render_template'
  actionview (4.2.0) lib/action_view/rendering.rb:83:in `render_to_body'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
  actionpack (4.2.0) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
  actionpack (4.2.0) lib/abstract_controller/rendering.rb:25:in `render'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:16:in `render'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
  activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
  C:/Ruby200-x64/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
  activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
  activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
  actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
  actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `call'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `each'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
  rack (1.6.0) lib/rack/etag.rb:24:in `call'
  rack (1.6.0) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.0) lib/rack/head.rb:13:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.0) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.0) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
  activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.0) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.0) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.0) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.0) lib/rails/engine.rb:518:in `call'
  railties (4.2.0) lib/rails/application.rb:164:in `call'
  rack (1.6.0) lib/rack/lock.rb:17:in `call'
  rack (1.6.0) lib/rack/content_length.rb:15:in `call'
  rack (1.6.0) lib/rack/handler/webrick.rb:89:in `service'
  C:/Ruby200-x64/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  C:/Ruby200-x64/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  C:/Ruby200-x64/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/missing_template.text.erb (0.0ms)

Thank you.

Frederick Cheung

unread,
Jun 25, 2015, 2:34:15 AM6/25/15
to rubyonra...@googlegroups.com


On Thursday, June 25, 2015, Padmahas Bn <padm...@gmail.com> wrote:

<td>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
This is your problem: all of your modal divs have the same id and it would seem that the javascript triggering the modal identifies the div by id. Ids are supposed to be unique, so it just uses the first div it finds with that id. You could make the myModal id change for each instance (and obviously change the link to) or perhaps whatever you are using for your modals supports more complex expressions for data-target

Fred 
--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/af158cea-ccd3-47ee-b73f-1c3e6a247ab6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Padmahas Bn

unread,
Jun 28, 2015, 6:46:35 AM6/28/15
to rubyonra...@googlegroups.com
<td>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
This is your problem: all of your modal divs have the same id and it would seem that the javascript triggering the modal identifies the div by id. Ids are supposed to be unique, so it just uses the first div it finds with that id. You could make the myModal id change for each instance (and obviously change the link to) or perhaps whatever you are using for your modals supports more complex expressions for data-target

Hello Fred how can I change the id's of each model? I cannot hard code it to the modal right ? And if it matters, I'm using bootstrap modal instead of JavaScript MODAL. Does this change the way of changing modal id with respect to bootstrap? Also is is easy if I use JavaScript for this kind of MODAL?

Thank you.

Colin Law

unread,
Jun 28, 2015, 7:05:10 AM6/28/15
to rubyonra...@googlegroups.com
It is not the model id you need to change just the bit in the html
where it says id="MyModal", you need that to be different for each
element.

Colin
Message has been deleted

Colin Law

unread,
Jun 28, 2015, 5:17:19 PM6/28/15
to rubyonra...@googlegroups.com
On 28 June 2015 at 19:57, Elizabeth McGurty <emcg...@gmail.com> wrote:
> ...
> Now this is going to seem to be Ad nauseam, and Mr Law refers to
> id="MyModal", which I do not see

Form the OP's post of 25th June
<td>
<!-- Modal -->

<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel"


Colin

Padmahas Bn

unread,
Jun 29, 2015, 8:20:19 AM6/29/15
to rubyonra...@googlegroups.com
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>

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 <emcg...@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

--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Walter Lee Davis

unread,
Jun 29, 2015, 10:12:11 AM6/29/15
to rubyonra...@googlegroups.com

On Jun 29, 2015, at 8:18 AM, Padmahas Bn <padm...@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
> 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.
Reply all
Reply to author
Forward
0 new messages