I am trying to allow users to type in an ISBN number and receive data about the book. This works when a user enters an ISBN number the first time, but when he/she tries to change it the value cannot be updated (after trying to change the value, it changes itself back to the first ISBN number).
I have a _form.html.erb
partial which is updated when something is typed into input#isbn_field
. If the ISBN is valid, then info is updated to replace other information (author name, title, etc.) in the div with id: 'isbn-lookup-results-container'
.
How can I allow the ISBN to be changed after the isbn_lookup_result partial has been loaded?
listings_controller.rb
def lookup
@listing = Listing.new
@isbn_lookup_result = Listing.amazon_lookup(params[:isbn])
render partial: 'isbn_lookup_result'
end
lookup.js.coffee
$(document).ready ->
TIMEOUT = null
$(document).on 'keyup', 'input#isbn_field', ->
clearTimeout TIMEOUT
TIMEOUT = setTimeout((->
ajaxResponse = $.ajax(
url: '/listings/lookup'
type: 'GET'
data:
isbn: $('input#isbn_field').val())
ajaxResponse.success (data) ->
$('#isbn-lookup-results-container').html data
$('#form-unfilled').hide()
return
ajaxResponse.error (data) ->
alert 'Not a valid ISBN'
return
return
), 2000)
return
return
_isbn_lookup_result.html.erb
<%= form_for(@listing, :html => {class: "form-horizontal" ,
role: "form"}, method: :get) do |f| %>
<div class="form-group">
<%= f.text_field :isbn , class: "form-control" ,
id: "isbn_field", placeholder: "ISBN (10 or 13 digits - no dashes)",
value: @isbn_lookup_result[:isbn], autofocus: true %>
</div>
# More stuff
<% end %>