0

addressAjaxを使用してテキストエリアを更新しようとしています。

アプリケーション.js

$("#invoice_project_id").change(function() {
    $.ajax({
        url: '/invoices/get_recipient',
        data: 'project_id=' + this.value,
        dataType: 'script'
    })
});

請求書_controller.rb

def get_recipient
  project = Project.find(params[:project_id])
  @recipient = project.person.address
end  

get_recipient.js.erb

$('#invoice_recipient').val("<%= @recipient %>");

どうやら、おそらくaddress.

たとえば、別の属性を使用するfirst_nameと、全体がかなりうまく機能します。

@recipient = project.person.first_name

どうすればこれをaddress同様に動作させることができますか?

アドレス内の改行は何らかの方法でエスケープする必要があると思いますが、まだその方法を見つけていません。

助けてくれてありがとう!

4

1 に答える 1

1

返されるjQueryをその中のテキストに置き換えて、ストレートテキストを送信できる場合は、次のことができます。

$("#invoice_project_id").change(function() {
   var value=$(this).val();
    $.get('/invoices/get_recipient', {project_id= : value}  function(response) {
        $('#invoice_recipient').val(response);
    })
});​

改行をスクリプトではなくストレートテキストとして送信すると、テキストエリアに追加されたときに改行が保持されるはずです

于 2012-10-21T21:37:12.177 に答える