简体   繁体   English

Rails-使用AJAX通过link_to设置会话变量

[英]Rails - use AJAX to set session variable with link_to

In my rails application I have a view with two links: 在我的rails应用程序中,我有一个带有两个链接的视图:

authenticate.html.erb:
<%= link_to t('.start_new'), edit_registration_path, id: "new-button", class: "btn btn-default" %>
<% unless @organisation_survey.blank? %>
  <%= link_to t('.start_edit'), edit_registration_path, id: "edit-button", class: "btn btn-default" %>
<% end %>

The first one should send the user to a new survey while the second one should let him edit his previous survey. 第一个应将用户发送到新调查,而第二个应让用户编辑其先前的调查。 Both are supposed to send the user to the edit_registration_path first because in both cases the user is logged in and can edit his name, email etc. In case the user clicks the second link to edit his previous survey a session variable should be set: 这两种方法都应该首先将用户发送到edit_registration_path,因为在两种情况下,用户都可以登录并且可以编辑其姓名,电子邮件等。如果用户单击第二个链接来编辑其先前的调查,则应设置会话变量:

authenticate.html.erb:  
<script type="text/javascript">
  $('edit-button').observe('click', function (event) {
    <% session[:edit_survey] = "y" %>
    event.stop();
  });
</script>

I know it's not good practice to have javascript in the html.erb file but I'm not able to set the session[:edit_survey] in the js.coffee file. 我知道在html.erb文件中使用javascript不是一个好习惯,但是我无法在js.coffee文件中设置session [:edit_survey]。 In that file I obviously don't have access to this session variable and I can't set it to "y". 在该文件中,我显然无权访问此会话变量,因此无法将其设置为“ y”。 But I really need to set this session variable because in another view I need to decide between new and edit path: 但是我确实需要设置此会话变量,因为在另一种视图中,我需要在新建路径和编辑路径之间做出决定:

questions_info.html.erb:
<% if session[:edit_survey] == "y" %>
  <%= link_to t('.start'), edit_question_path(1), class: "btn btn-default" %>
<% else %>  
  <%= link_to t('.start'), new_question_path(1), class: "btn btn-default" %>
<% end %> 

What I want is to go to the edit_question_path(1) if the user clicked the edit button on the first page and to go to the new_question_path(1) if he clicked the new button. 我想要的是,如果用户单击第一页上的编辑按钮,则转到edit_question_path(1),如果他单击新按钮,则转到new_question_path(1)。

Currently the session[:edit_survey] is set to "y" even if the edit button is not clicked, I guess the click event fires no matter which button is clicked. 目前,即使未单击编辑按钮,session [:edit_survey]仍设置为“ y”,我想无论单击哪个按钮,都会触发click事件。 When the user decides to hit the new button he is going to end in editing his previous survey instead of taking a new survey - I don't want this. 当用户决定点击新按钮时,他将结束编辑先前的调查而不是进行新的调查-我不希望这样做。 So I am searching for a solution to let the user decide if he wants to take a new survey or edit his previous survey and then he should be sent to the correct question_path. 因此,我正在寻找一种解决方案,以让用户决定是要进行新调查还是编辑先前的调查,然后将其发送到正确的question_path。 Because there are some pages between the first page with these two buttons and the questions I can't just use params... Any ideas are appreciated. 因为在具有这两个按钮的第一页和我不能仅使用参数的问题之间存在一些页面,所以不胜感激。

EDIT: Now I know the javascript is not the problem, instead of it I should use some AJAX. 编辑:现在我知道javascript不是问题,相反,我应该使用一些AJAX。 I tried many solutions now but the variable is never set now. 我现在尝试了许多解决方案,但现在从未设置变量。

authenticate.html.erb: authenticate.html.erb:

<%= link_to t('.start_edit'), edit_registration_path, id: "edit-button", class: "btn btn-default" %>

dashboard.js.coffee: dashboard.js.coffee:

`$(document).ready(function(){
  $("#edit-button").click(function() {  
    $.post('/authenticate');
  });
});`

routes.rb: routes.rb中:

post '/authenticate' => 'dashboard#set_edit_var'

dashboard_controller.rb: dashboard_controller.rb:

def set_edit_var
  session[:edit_survey] = "y"
end

URL where the edit button is: 修改按钮位于的网址:

http://localhost:3000/dashboard/XT0Epg0BAWRktZ-By-xsKw/authenticate

If you need to know: authenticate is a method of dashboard, so it is: views > dashboard > authenticate.html.erb 如果您需要知道:authenticate是仪表板的一种方法,则它是:视图>仪表板> authenticate.html.erb

Any help is appreciated because I just don't manage to get this AJAX request working for me, no matter how many threads I am reading. 感谢您提供任何帮助,因为无论我正在读取多少线程,我都无法使这个AJAX请求为我工作。 I tried "get" instead of "post" but it doesn't make anything better... The method call is working, when I click the button and add an alert the alert is shown, but the $.post and the routes seem to be wrong. 我尝试使用“ get”代替“ post”,但是它并没有使任何事情变得更好...方法调用正在工作,当我单击按钮并添加警报时,显示警报,但是$ .post和路线似乎是错误的。

It is not the case that the click event fires when the link is not clicked. 如果未单击链接,则不会触发click事件。

Your problem is that the javascript defined, even in the html.erb file will run on the browser on the client, and the session is only accessible on the server. 您的问题是,即使在html.erb文件中定义的javascript也将在客户端的浏览器上运行,并且只能在服务器上访问该会话。 In fact the Ruby on Rails code only runs on the server. 实际上,Ruby on Rails代码仅在服务器上运行。

So when you write: 所以当你写:

<script type="text/javascript">
  $('edit-button').observe('click', function (event) {
    <% session[:edit_survey] = "y" %>
    event.stop();
  });
</script>

The erb is interpreted on the server. erb在服务器上解释。 When it encounters the code: 当遇到代码时:

<% session[:edit_survey] = "y" %>

the session is set to "y". 会话设置为“ y”。 This is not included in the javascript code for the page which gets sent to the client and used in the browser. 发送到客户端并在浏览器中使用的页面的javascript代码中不包含此代码。

One way to see this more clearly is load the page and do a show source in your browser. 一种更清晰地看到这种情况的方法是加载页面并在浏览器中显示节目。 You will notice that the javascript looks like the following: 您会注意到javascript如下所示:

<script type="text/javascript">
  $('edit-button').observe('click', function (event) {
    event.stop();
  });
</script>

What you will need to do is make an AJAX call to the server, and have the web service for the AJAX call set the session data. 您需要做的是对服务器进行AJAX调用,并让AJAX调用的Web服务设置会话数据。

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

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