2014-01-10 38 views
1

如果我有這樣一段代碼:如何從rails select_tag獲取回調響應?

<%= select_tag 'script-switcher', options_for_select(@scripts), prompt: "Choose" , data: { remote: true, url: url_for(mypath_path(current_store)) } %>

我怎樣才能HTML響應?

+0

只寫一個JavaScript來做到這一點,這不是一個軌道應用程序的常見行爲。 – BroiSatse

+0

如何?用戶將選擇一個項目,然後我會返回一些部分顯示,但使用Ajax,因爲我不想重新加載頁面。 – Cleyton

回答

1

我解決這樣做:

.js.coffee

$('#script-switcher').bind "ajax:success", (event,data) -> $('.value-copy').html(data)

.html.erb

<%= select_tag 'script-switcher', options_for_select(@scripts), data: { remote: true, url: url_for(my_path()) } %>

0

您無法通過remote:true獲取html響應。

remote:true發送一個js請求(ajax請求),您將得到一個js響應。刪除它以獲得html響應。

+0

但我需要這樣做通過ajax呈現部分。 用戶將選擇一個項目,然後根據選定的項目返回一個局部信息 – Cleyton

+0

yes只是在js.erb或js.haml文件中渲染與控制器操作同名的局部文件。例如:js.erb文件中的create.js.erb –

+0

$('#some_element')。html(「#{escape_javascript(render'partial_name')}」)。 –

相關問題