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 %>
  <td><%= 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

On 4 May 2015 at 15:47, Padmahas Bn [email protected] wrote:

<% @designation_dicts.each do |designation_dict| %> <%= designation_dict.desig_code %> <%= designation_dict.designation %>
  <td><%= render :partial => "show", :locals => {:designation_dict =>

designation_dict} %>

<%= link_to ‘Show’, “#myModal”, “data-toggle” => “modal”, :class
=> ‘btn btn-default’ %>

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

1 Commissioner
×

1

Row 2

Thank you.

Designation code: 1 *#Value should be changed here in the next row*
               </td>    </tr>    <tr>       <th> 

Designation description:


Commissioner #Value should be changed
here in the next row
                </td>    </tr>    </table>        <div 

class=“modal-footer”> Close

Show
2 Assistant Commissioner
×

2

Designation code: 2 *#Value changed here*
Designation description: Assistant Commissioner *#Value changed here*
Close

On Thursday, June 25, 2015, Padmahas Bn [email protected] wrote:

<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

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.

On 28 June 2015 at 11:46, Padmahas Bn [email protected] wrote:

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?

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

<%= render :partial => "show", :locals => {:designation_dict => designation_dict} %>

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:

Designation code: <%= designation_dict.desig_code %>
Designation description: <%= designation_dict.designation %>

Do you see now how you are generating unique html ids?

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

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

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:infind’
    C:in find_template' actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:40:indetermine_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:inrender_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:inrender_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:inrender_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:inrender’
    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:inblock 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:inms’
    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:incleanup_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:inrender’
    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:insend_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:inprocess_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:incall’
    activesupport (4.2.0) lib/active_support/callbacks.rb:117:in call' activesupport (4.2.0) lib/active_support/callbacks.rb:151:inblock 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:inblock in
    halting’
    activesupport (4.2.0) lib/active_support/callbacks.rb:151:in call' activesupport (4.2.0) lib/active_support/callbacks.rb:151:inblock 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:inblock in
    halting’
    activesupport (4.2.0) lib/active_support/callbacks.rb:169:in call' activesupport (4.2.0) lib/active_support/callbacks.rb:169:inblock in
    halting’
    activesupport (4.2.0) lib/active_support/callbacks.rb:169:in call' activesupport (4.2.0) lib/active_support/callbacks.rb:169:inblock in
    halting’
    activesupport (4.2.0) lib/active_support/callbacks.rb:169:in call' activesupport (4.2.0) lib/active_support/callbacks.rb:169:inblock 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:inrun_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:inprocess_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:inblock
    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:ininstrument’
    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:inprocess_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:inprocess’
    actionview (4.2.0) lib/action_view/rendering.rb:30:in process' actionpack (4.2.0) lib/action_controller/metal.rb:195:indispatch’
    actionpack (4.2.0)
    lib/action_controller/metal/rack_delegation.rb:13:in
    dispatch' actionpack (4.2.0) lib/action_controller/metal.rb:236:inblock 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:indispatch’
    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:inblock
    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:inserve’
    actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in
    call' rack (1.6.0) lib/rack/etag.rb:24:incall’
    rack (1.6.0) lib/rack/conditionalget.rb:25:in call' rack (1.6.0) lib/rack/head.rb:13:incall’
    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:incall’
    rack (1.6.0) lib/rack/session/abstract/id.rb:225:in context' rack (1.6.0) lib/rack/session/abstract/id.rb:220:incall’
    actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in
    call' activerecord (4.2.0) lib/active_record/query_cache.rb:36:incall’
    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:incall’
    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:incall’
    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:incall’
    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:incall’
    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:incall’
    railties (4.2.0) lib/rails/rack/logger.rb:38:in call_app' railties (4.2.0) lib/rails/rack/logger.rb:20:inblock 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:intagged’
    activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in
    tagged' railties (4.2.0) lib/rails/rack/logger.rb:20:incall’
    actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in
    call' rack (1.6.0) lib/rack/methodoverride.rb:22:incall’
    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:incall’
    rack (1.6.0) lib/rack/lock.rb:17:in call' actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:incall’
    rack (1.6.0) lib/rack/sendfile.rb:113:in call' railties (4.2.0) lib/rails/engine.rb:518:incall’
    railties (4.2.0) lib/rails/application.rb:164:in call' rack (1.6.0) lib/rack/lock.rb:17:incall’
    rack (1.6.0) lib/rack/content_length.rb:15:in call' rack (1.6.0) lib/rack/handler/webrick.rb:89:inservice’
    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:inrun’
    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.

On 28 June 2015 at 19:57, Elizabeth McGurty [email protected] 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

<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}", "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.

On Mon, Jun 29, 2015 at 12:27 AM, Elizabeth McGurty
[email protected]

On Jun 29, 2015, at 8:18 AM, Padmahas Bn [email protected] wrote:

<%= link_to 'Show', designation_dict, "data-target" =>

#myModal_#{designation_dict.id}”, “data-toggle” => “modal”, :class => ‘btn
btn-default’ %>


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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs