Reputation: 1150
I am trying to create a single page CRUD. Right now, I faced some errors with the show function.
An example of the application can be found here: http://tryoutcreation.herokuapp.com/
It appears to work alright. After creation, the show function works fine if the page is not refreshed.
However, if it is refreshed, the dialog stops working as a dialog and just shows as a div.
You can try it by creating a user and clicking the show function. Then try refreshing the page, the same div will just be shown on the page instead of as a dialog.
The source can be found here: https://github.com/frozzie/ModalCrud
Related codes:
The dialog is the show-form.
<tr id = "<%= dom_id user%>">
<td><%= user.user_name %></td>
<td><%= user.email %></td>
<td><%= user.password %></td>
<td><%= user.account_type %></td>
<td><%= link_to 'Show', user, :remote=>true%></td>
<td><%= link_to 'Edit', edit_user_path(user), :remote => true %></td>
<td><%= link_to 'Destroy', user, :remote => true, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
<div id = "show-form" title="User Information">
<p>
<b>User name:</b>
<%= user.user_name %>
</p>
<p>
<b>Email:</b>
<%= user.email %>
</p>
<p>
<b>Password:</b>
<%= user.password %>
</p>
<p>
<b>Account type:</b>
<%= user.account_type %>
</p>
</div>
</tr>
It should only be opened when the show button is clicked. $("#show-form").dialog( "open" );
This is the dialog.
$( "#show-form" ).dialog({
autoOpen: false,
height: 400,
width: 350,
modal: true,
closeOnEscape: true,
resizable:false,
buttons: {
Cancel: function() {
$( this ).dialog( "close" );
}
}
})
Any ideas on why it happened would be appreciated.
Upvotes: 0
Views: 179
Reputation: 28096
This has less to do with your js than it does your php.
Remove this from your code:
<div id = "show-form" title="User Information">
<p>
<b>User name:</b>
<%= user.user_name %>
</p>
<p>
<b>Email:</b>
<%= user.email %>
</p>
<p>
<b>Password:</b>
<%= user.password %>
</p>
<p>
<b>Account type:</b>
<%= user.account_type %>
</p>
</div>
then look to output the users information correctly. Don't just output this at the top of the page.
<tr id = "<%= dom_id user%>">
<td><%= user.user_name %></td>
<td><%= user.email %></td>
<td><%= user.password %></td>
<td><%= user.account_type %></td>
<td class="click_me" data-id="<%= dom_id user%>"><%= link_to 'Show', user, :remote=>true%></td>
<td><%= link_to 'Edit', edit_user_path(user), :remote => true %></td>
<td><%= link_to 'Destroy', user, :remote => true, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<tr style="display: none;" class="hidden_<%= dom_id user%>">
<td colspan="8">
<div class = "show-form" title="User Information">
<p>
<b>User name:</b>
<%= user.user_name %>
</p>
<p>
<b>Email:</b>
<%= user.email %>
</p>
<p>
<b>Password:</b>
<%= user.password %>
</p>
<p>
<b>Account type:</b>
<%= user.account_type %>
</p>
</div>
</td>
</tr>
JS
now,
$('td').on('click','.click_me', function(){
var id = $(this).data('id');
$('.hidden_'+id).slideToggle();
});
This isn't perfect... lol by any means, but at least you can work with it hopefully.
Upvotes: 2