2017-04-19 60 views
0

我試着去使用AJAX輸出到paramas文本文件的代碼,但不工作sinatra輸出參數爲文本文件?

我不知道,因爲它不工作

我使用的打印參數驗證碼

f.write PARAMS [ :名字]

require 'sinatra' 
require 'pp' 

get '/' do 
    erb :index 
end 



get '/2' do 
    content_type 'text/javascript' 
    File.read(File.join('public', 'jquery-1.7.1.js')) 

end 


post '/greeting' do 
    puts "params" 
    #pp params 

    "<p>#{params[:salutation]}, #{params[:name]}</p>" 
end 


post '/greeting' do 
    puts "params" 
    #pp params 
    File.read(File.join('public', 'name_of_the_file.txt')) 
    File.open('name_of_the_file.txt', 'w') do |f| 
    f.write params[:name] 
    end 
end 

ZZ

<html> 
    <head> 
    <script src="/2"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
ax=2; 
$('body').on("click reset",function(e){ ax= e.target.value ; }); 
ax=2; 
     $.post("greeting", { salutation: ax, name: "Friend" }, 
      function(result) { $("#greeting").html(result); 
     }); 
     }); 
    </script> 
    </head> 
    <body> 
    <h1>Sinatra Ajax Demo using jQuery</h1> 
<label for="male">Male</label> 
    <input type="radio" name="gender" id="male" value="male"><br> 
    <label for="female">Female</label> 
    <input type="radio" name="gender" id="female" value="female"><br> 
    <label for="other">Other</label> 
    <input type="radio" name="gender" id="other" value="other"><br><br> 
    <input type="submit" value="Submit"> 
    <div id="greeting"> 
     <p>Hello, World</p> 
    </div> 
    </body> 
</html> 

請幫我解決這個問題

+0

您可以使用解決'把params'看名字是否不存在? –

+0

是的,我在控制檯中看到{「salutation」=>「2」,「name」=>「Friend」} – zzero

回答

0

我這個

f = File.open('text.txt', 'w') 

old_out = $stdout 
$stdout = f 

    puts params[:salutation] 
f.close 

$stdout = old_out 
puts 'goodbye'