Ruby on Rails:Reload a View Inside other -
i has 2 views named _show.html.erb
, index.html.erb
.
_show.html.erb
rendered inside index.html.erb
.
want reload _show.html.erb
part inside index.html.erb
.
when dropdown selected.
tests/_show.html.erb
file:
<div class="main_content" > <% @tests.each |x| %> <%# if x.name == @str %> <p>here <%= x.name %> rating in programming languages</p> <% x.languages.each |a,b| %> <%= %> <strong> : </strong> <%= b*10 %></br> <% end %> <p>state: <strong><%=x.state%></strong></p> <%# end %> <% end %> </div>
tests/index.html.erb
file:
<p>find me in app/views/tests/index.html.erb</p> <%= select_tag :name,options_for_select(@tests.map{|u| [u.name]}.uniq), remote: true, id: "update_select" %> <% @str = "<span id=\"product-modal\">ashrith</span>".html_safe %> <%= @str %> <%= render "show" %>
application.js
file:
$(document).ready(function(){ $("#update_select").change(function() { drop = $(this).val(); alert(drop); $("#product-modal").html(drop); }); });
from above files.as dropdown selected value of dropdown should equated in _show.html.erb
.any methodology,other ok.more rubist,more good.
let's assume rendering posts. let posts#index action respond html , js. on html renders entire page, on js replaces posts on page , not filters (which 1 dropdown in case). furthermore since tend need multiple filters on page, place filters in form auto submits. example:
<%= form_tag posts_path, remote: true, method: :get, data: {bind: true} %> ... place filtering dropdowns , other controls here... <% end %> <div id="posts"> <%= render @posts %> </div> <script type="text/javascript"> $("form[data-bind]").find('input').on('change', function () { $(this).closest('form').submit); }); </script>
note js above doesn't have placed in view file, it's there make example simple. it's js can place , use project wide. controller:
class postscontroller def index @posts = post.all @posts.where(some_field: params[:some_form_filter) if params[:some_form_filter] respond_to |format| format.html format.js end end end
the js response replaces places page content jquery in app/views/posts/index.js.erb:
$('#posts').html("<%= j(render @posts %>");
nb: writeup done hip might need correct minor things here , there.
Comments
Post a Comment