jquery - How to call action on onclick-javascript in ruby on rails - Stack Overflow

I want to perform an action do file in controllersstatic_pages_controller.rb:def fileopenmy_file = Fil

I want to perform an action do file in controllers/static_pages_controller.rb:

def fileopen
        my_file = File.new("public/CHNAME1.txt","w") 
        my_file.write "\tfasf"
        my_file.close

    end

(it work well when i define it in helper and call it in view.)

in myview.html.erb, i want some thing like <button id="button" onclick="readfile()" /> How can I do that? I tried in application.js

function readfile() {
  alert('readfile work')
  $.ajax({
  alert('ajax work')
      url: "/fileopen",
      type: "POST",
      ##don't know what to do to make fileopen work 
      }
  });
}

routes.rb

 match '/fileopen', to:'static_pages#fileopen', via: 'get'

and it's seem nothing happen. Only the first alert work.

I want to perform an action do file in controllers/static_pages_controller.rb:

def fileopen
        my_file = File.new("public/CHNAME1.txt","w") 
        my_file.write "\tfasf"
        my_file.close

    end

(it work well when i define it in helper and call it in view.)

in myview.html.erb, i want some thing like <button id="button" onclick="readfile()" /> How can I do that? I tried in application.js

function readfile() {
  alert('readfile work')
  $.ajax({
  alert('ajax work')
      url: "/fileopen",
      type: "POST",
      ##don't know what to do to make fileopen work 
      }
  });
}

routes.rb

 match '/fileopen', to:'static_pages#fileopen', via: 'get'

and it's seem nothing happen. Only the first alert work.

Share Improve this question edited Nov 23, 2013 at 4:44 Midori asked Nov 22, 2013 at 9:12 MidoriMidori 251 silver badge10 bronze badges 1
  • Have you done trying with JQuery? I think that was really help my development time, and much easy. – ksugiarto Commented Nov 22, 2013 at 9:29
Add a ment  | 

1 Answer 1

Reset to default 3

In answer to your question directly, you have to be able to handle the JS request in the controller. This is typically done by using the respond_to block in Rails, like this:

def fileopen
    respond_to do |format|
        format.js {
            my_file = File.new("public/CHNAME1.txt","w") 
            my_file.write "\tfasf"
            my_file.close
        }
    end
end

This code may give you some sort of a response with your current code, but it might be the case that you need to appreciate better how Ajax & Rails work in order to help you better


How Ajax Works

Ajax is a javascript technology which sends an "asynchronous" request to other pages on your website. By their nature, asynchronous requests are done pletely independently of your main HTTP request, and basically act like a "pseudo" browser -- working in the background

Ajax is used to pull data from JS-enabled endpoints (which are handled with the respond_to function in Rails, which you can then use to modify your page in some way. A lot of people get confused with Ajax, but it's actually quite simple -- it's just javascript which pulls data from another page, allowing you to manipulate your page with that data


Using Ajax In Your Views

The reason why this is important for you is because you mentioned you didn't know what to do with the success callback of your app. Hopefully my explanation will show you that the success part of the $.ajax call should be used to append the data you receive from the controller on your page

This can be done in this way:

$("#button").click(function() {
  $.ajax({
      url: "/static_pages/fileopen",
      type: "POST",
      data: {name: $(this).val()},
      success: function (data) { 
          // append data to your page
          $("page_element").html(data);
      }
  });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745170627a4614903.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信