0
這裏是我的index.erb文件: 如何使用西納特拉我改變紅寶石HTML元素和引導
<a class="btn" href='javascript:TestFunction()' role="button">TestFunction</a>
<script language="javascript">
function TestFunction() {
$.post("test_function", { action: "foo", name: "bar" });
}
</script>
</body>
而且我app.rb文件看起來像這樣
class ConfigureApp < Sinatra::Base
get '/' do
erb :index
end
post '/test_function' do
button_action = params.fetch("action")
test_class = TestClass.new
test_class.action(button_action)
end
end
識別TestClass。 rb文件:
class TestClass
def action(button_action)
if button_action == "foo"
new_button_label = "something else"
end
end
end
有沒有辦法讓我從TestClass更改TestFunction按鈕的標籤?因此,使用上面的示例,我可以重命名TestFunction按鈕,「其他」嗎?
您需要在客戶端使用jQuery或JavaScript做些什麼,我認爲這樣才能根據AJAX調用的返回動態重置按鈕標籤? (TL; DR:這聽起來像是一個jQuery問題....) –
你想在用戶瀏覽頁面時動態地改變它,還是你想要不同的調用導致不同的標籤?在第一種情況下,您需要javascript,就像Andy所說的那樣,但在第二種情況下,可以從ruby中完成。在這種情況下,你需要傳遞params到'erb'。 – froderik