I have a Rails app with a Bootstrap modal.
Here is part of the code that launches the modal:
<a type="button" class="btn" href="#labor_modal" data-toggle="modal" data-woid= <%= workorder.id %>><i class="icon-time"></i></a>
Here’s part of the code for the modal:
<div class="modal fade" id="labor_modal">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Add Labor</h3>
</div>
<div class="modal-body">
<%= simple_form_for(Event.new, validate: true, remote: true, html: {"data-type" => :json}) do |f| %>
<%= f.hidden_field :workorder_id, :value => data-woid.val() %>
I want to get the work order.id passed to the modal so it can be saved in the new record.
This code isn’t working:
<%= f.hidden_field :workorder_id, :value => data-woid.val()
Any help would be appreciated !!
PS – I was hoping I could do this without jquery – but, if I need it so be it.
<%= %>is an ERb (embedded Ruby) tag. Inside should be Ruby code.is not valid Ruby in the context which you use it.
ERb is processed before rendering HTML. At the time ERb is processed, Ruby is not aware of the DOM or rendered HTML elements or their attributes. There is no way to read HTML data attributes in Ruby via ERb. You must access them after the page is rendered and the DOM is built. Use JavaScript/jQuery for this.
I’m guessing that
work_order.idwill be used to set an association. Exposing this to the user is insecure; it is possible to change the value to anything before submitting. Associations should be set in the controller, specifically thecreateaction before you callsave.