I'm a bit stuck in trying to implement AJAX where on "link_to" view is rendered and in that view I have another AJAX call.
I have menu sidebar where there is something like this:
<li class="<%= is_active_action('positions') %>"><%= link_to "Positions", positions_path, remote: true %></li>
In application.html.erb I have this:
<!-- Main view -->
<div id="yield">
<%= yield %>
</div>
Actually not sure if this is the right way to do, but so far it worked.
Then in views/../positions/index.js.coffee I have this
$("#yield").empty()
.append("<%= escape_javascript(render 'inventory/positions/index') %>")
which is redering /positions/_index.html.erb partial. In there I have <div id="media_list"> and <div id='positions_table'>
In assets/javascripts I have positions.coffee
$ ->
$(document).on 'change', '#media_list', (evt) ->
$.ajax 'update_positions',
type: 'GET'
dataType: 'script'
data: {
media_id: $("#media_list option:selected").val()
}
error: (jqXHR, textStatus, errorThrown) ->
console.log("AJAX Error: #{textStatus}")
success: (data, textStatus, jqXHR) ->
console.log("Dynamic media select OK!")
This is basically listening to drop-down change and then passing media_id to controller, then this /views/../positions/update_positions.js.coffee renders second partial (/views/../positions/_positions.html.erb):
$("#positions_table").empty()
.append("<%= escape_javascript(render 'inventory/positions/positions', locals: {positions: @positions}) %>")
At the moment on dropdown change I cannot make second partial (_positions.html.erb) to render. Any ideas what could be wrong? Thank you for any help.
Update
I've set in my routes this:
get '/update_positions', to: 'inventory/positions#update_positions'
and then in controller I have this:
def update_positions
@positions = Positions::Positions.where('media_id = ?', params[:media_id])
When :media_id is passed, I get this in console Started GET "/update_positions?media_id=1" and result visually is as expected. But then when I do several similar requests in a row, strange things happen. It looks like requests ar multiplying and seems that there are much historic requests repeating again and again. My assumption is that is because of this /update_positions?media_id=1 Please, help, what am I doing wrong?
Update 2
First of all I adjusted route to actual Controller I have:
get '/inventory/positions/update_positions', to: 'inventory/positions#update_positions'
Previous route did not work correctly when I jumped from routes like /users/1/edit.
In my /views/inventory/positions/update_positions.js.coffee I have this:
$("#positions_table").empty()
.append('<%=j render 'inventory/positions/positions'%>');
Multiple queries look like this:
Started GET "/inventory/positions/update_positions?media_id=1" for 78.84.173.112 at 2016-12-20 05:29:39 +0000
Parameters: {"media_id"=>"1"}
#several queries follow
Started GET "/inventory/positions/update_positions?media_id=1" for 78.84.173.112 at 2016-12-20 05:29:39 +0000
Parameters: {"media_id"=>"1"}
#several queries follow
This happens when I open particular view, select let' say "value: 1" from dropdown and particular partial is rendered. Then I go to another AJAX view in different section, then come back and again select "value: 1". If I do it multiple times, then queries happen to copy more and more. It stops, when I do full page refresh - then everything starts from normal position, but again queries grow, when I repeat select multiple times.
update_positionsis controller action.