我想在controllers / static_pages_controller.rb中对文件执行动作写入:
def fileopen
my_file = File.new("public/CHNAME1.txt","w")
my_file.write "\tfasf"
my_file.close
end
(当我在帮助器中定义它并在视图中调用它时,它工作得很好。)
在myview.html.erb中,我想要一些像我能做到的事情吗?我试过application.jsfunction readfile() {
alert('readfile work')
$.ajax({
alert('ajax work')
url: "/fileopen",
type: "POST",
##don't know what to to to call fileopen
}
});
}
的routes.rb
match '/fileopen', to:'static_pages#fileopen', via: 'get'
似乎什么也没发生。只有第一个警报工作
答案 0 :(得分:3)
你有语法错误,试试这个:
function readfile() {
alert('readfile work');
$.ajax({
url: "/fileopen",
type: "POST",
success: function(){
alert('ajax work');
}
});
}
我不知道红宝石,但是路线是“GET”请求而你正在发出“POST”请求:
match '/fileopen', to:'static_pages#fileopen', via: 'get' // should be via: 'post'?
答案 1 :(得分:2)
你不能在ajax中使用alert,如果你想使用它,那么使用成功和错误方法