ユーザーが単語を入力できるフォームを作成しています。ユーザーがタブをオフにすると、辞書(Wordnik.com)にクエリを実行して定義を取得し、ラジオボタンの選択としてフォームに定義を追加します。javascriptは単語を取得し、WordsControllerでget_definition関数を呼び出し、辞書APIをクエリしますが、部分的にレンダリングすることができないようです(現在、「<%= escape_javascript .....%」と出力されます。 > ")
PS:jqueryを使用してAPIに直接クエリを実行しようとしましたが(get_definitions関数をヒットしていません)、送信するリクエストを取得できなかったため、今のところ上記のメソッドを使用しています。
words_controller:
def index
@words = Word.where(user_id: current_user)
@word = current_user.words.new
@defs = Array.new
end
def get_definition
@defs = Wordnik.word.get_definitions(params['word'])
render nothing: true
end
javascript:
$('input#word_name').blur(function(){
var word = $('input#word_name').val();
$.ajax({
url: '/words/get_definition',
data: 'word='+word
})
$('#addDefinitionContainer').html("<%= escape_javascript(render partial: 'words/definitions', locals: {f: f}) %>")
})
_definitions部分的:
<% @defs.each do |definition| %>
<div class="definition_choice">
<%= f.label definition %>
<%= f.radio_button :definition, "yes" %>
</div>
<% end %>