Snažím se používat ajax tak pouze částečné načte a ne celé webové stránky, ale data se vrací undefined. Dělám něco špatně? Tady je kód:
/* form.js */
$(document).ready(() => {
$('#stock-lookup-form').on('ajax:complete', (e, data, status) => {
/*$('#results').html(data.responseText)*/
console.log(data) /* --> this returns undefined*/
})
})
/* stocks_controller.rb */
class StocksController < ApplicationController
def search
if params[:stock].present?
@stock = Stock.new_from_lookup(params[:stock])
if @stock
render partial: 'users/result'
else
flash[:danger] = "You have entered an incorrect symbol"
redirect_to my_portfolio_path
end
else
flash[:danger] = "You have entered an empty search string"
redirect_to my_portfolio_path
end
end
end
/* výsledek.html.erb částečné */
<%if @stock%>
<div class="well results-block">
<strong>Symbol: </strong><%[email protected]%>
<strong>Name: </strong><%[email protected]%>
<strong>Last price: </strong><%[email protected]_price%>
</div>
<%end%>
/* my_portfolio.html.erb (kde je umístěn formulář) */
<h1>My portfolio</h1>
<h3>Search for stocks</h3>
<div id="stock-lookup">
<%=form_tag search_stocks_path, remote: true, method: :get, id: 'stock-lookup-form' do%>
<div class="form-group row no-padding text-center col-md-12">
<div class="col-md-10">
<%=text_field_tag :stock, params[:stock], placeholder:'Stock ticker symbol', autofocus:true, class:'form-control search-box input-lg'%>
</div>
<div class="col-md-2">
<%=button_tag(type: :submit, class:'btn btn-lg btn-success', id:'search-btn') do%>
<i class="fa fa-search"></i> Look up a stock
<%end%>
</div>
</div>
<%end%>
</div>
<div id="results">
<%=render 'users/result'%>
</div>