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

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

<% @designation_dicts.each do |designation_dict| %>

<%= designation_dict.desig_code %> <%= designation_dict.designation %> <%= render :partial => "show", :locals => {:designation_dict => designation_dict} %> <%= link_to 'Show', "#myModal", "data-toggle" => "modal", :class => 'btn btn-default' %> <%= link_to 'Edit', edit_designation_dict_path(designation_dict), :class => 'btn btn-default' %> <%= link_to 'Destroy', designation_dict, method: :delete, data: { confirm: 'Are you sure?' } , :style => 'color:#FFFFFF', :class => 'btn btn-danger' %>

<% end %>

Part of the modal (_show.html.erb)

Designation code:

<%= designation_dict.desig_code %>

Designation description:

<%= designation_dict.designation %>

Thank you

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

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

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.

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.

<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

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

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

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

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

Colin

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

<%= link_to 'Show', "data-toggle" => "modal", :class => 'btn btn-default', :id => designation_dict.dd_id %>

To this:

<%= render :partial => "show", :locals => {:designation_dict => designation_dict} %> <%= link_to 'Show', designation_dict, "data-target" => "#myModal_#{[designation_dict.id](http://designation_dict.id)}", "data-toggle" => "modal", :class => 'btn btn-default' %>

Working like a charm. Thank you a lot. I was searching for this from past 20 days.

Thank you again.

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