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