Friday, 23 August 2013

Initializing object for modal form

Initializing object for modal form

I have feedback a button that sticks to the bottom of every page. When
clicked, it displays a bootstrap modal with a form for a @feedback object.
Although the modal form remains hidden until the feedback button is
clicked, the form partial is rendered in the modal and is therefore loaded
on every page. This produces an error because a @feedback object is not
yet initialized.
I would simply initialize @feedback in the associated controller action
that renders the modal window, but there is none since it is displayed on
every page. Where should I initialize @feedback?
application.html.erb
...
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= yield %>
</div>
<%= render 'layouts/feedback' %>
</body>
...
_feedback.html.erb
<!-- Button to trigger modal -->
<a href="#feedback" role="button" class="btn btn-feedback"
data-toggle="modal">Feedback</a>
<!-- Modal -->
<div id="feedback" class="modal hide fade" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×</button>
<h3 id="myModalLabel">Provide Feedback</h3>
</div>
<div class="modal-body">
<%= render 'feedbacks/form'%>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal"
aria-hidden="true">Close</button>
<button class="btn btn-primary">Submit</button>
</div>
</div>

No comments:

Post a Comment