简体   繁体   English

从JavaScript调用Rails Controller操作不显示

[英]Call Rails Controller Action from Javascript not displaying

I am trying to call a controller-action from javascript. 我正在尝试从javascript调用控制器动作。

The chrome debugger shows the javascript being stepped into ok (so the js click event is triggered ok). chrome调试器显示javascript进入了正常状态(因此js click事件被正常触发)。

The controller action works if I use a HTML button to call it ie the /home/benefits/ page renders ok (so the controller action code in itself is functional). 如果我使用HTML按钮来调用控制器动作,则该动作即可,即/ home / benefits /页面呈现正常(因此,控制器动作代码本身就可以正常工作)。

It does not seem to work when I try call it via javascript - the development.log seems to show it has rendered the page, but my website page does not change to the /home/benefits/ page 当我尝试通过javascript调用它时,它似乎不起作用-development.log似乎表明它已呈现了该页面,但是我的网站页面未更改为/ home / benefits /页面

I've read as many tutorials and related issues on OS but I'm stumped. 我已经阅读了关于OS的许多教程和相关问题,但是我很困惑。 Any help would be appreciated from this awesome SO community! 这个很棒的SO社区将为您提供任何帮助! ;-) ;-)

CONTROLLER CODE: 控制器代码:

  def set_analysis_input
     redirect_to home_benefits_path
  end    

ROUTES.RB: ROUTES.RB:

  get "trades/set_analysis_input"

HTML.ERB: (1st line using html button is ok, 2nd line via JS does not work) HTML.ERB :(使用html按钮的第一行可以,通过JS的第二行不起作用)

<%= link_to "Button to Call Action", trades_set_analysis_input_path, class: "btn btn-small btn-success" %>
<button id="button_to_call_controller_action">hello</button>

JS AT END OF HTML.ERB FILE: HTML.ERB文件末尾的JS:

console.log("Entered JS"); console.log(“ Entered JS”);

  $(document).on('click', '#button_to_call_controller_action', function() {
    url_string = "/trades/set_analysis_input";
    $.ajax({
       url:   url_string,
       type:  'get'
    });  
  }); 

END OF DEVELOPMENT.LOG FILE: 开发结束日志文件:

Started GET "/trades/set_analysis_input" for 127.0.0.1 at 2014-11-15 12:47:44 +0000
Processing by TradesController#set_analysis_input as */*
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 38 ORDER BY "users"."id" ASC LIMIT 1
Redirected to http://localhost:3000/home/benefits
Completed 302 Found in 3ms (ActiveRecord: 0.0ms)


Started GET "/home/benefits" for 127.0.0.1 at 2014-11-15 12:47:44 +0000
Processing by HomeController#benefits as */*
  Rendered home/benefits.html.erb within layouts/application (0.0ms)
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 38 ORDER BY "users"."id" ASC LIMIT 1
  Rendered layouts/_header.html.erb (0.0ms)
Completed 200 OK in 79ms (Views: 79.4ms | ActiveRecord: 0.0ms)

Welcome to StackOverflow. 欢迎使用StackOverflow。 The issue is that you are calling a controller action with a redirect_to in it, which is where the 问题是您正在调用其中带有redirect_to的控制器动作,这是

Completed 302 Found in 3ms (ActiveRecord: 0.0ms)

is coming from. 来自。 If the ajax call gets a 302, you will have that issue. 如果ajax调用得到302,则将出现该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM