简体   繁体   中英

Can params be passed from the Rails Application Controller into Javascript?

I am currently passing an individual object (a :category value) into params in my Rails project.

What I am having a tough time understanding -- and haven't been able to confirm from the documentation -- is whether the Application Controller applies to all files within an application (including my javascript files) or if that only controls my views.

In short, my question is: in my Javascript, can I pass params using a method from my App Controller just as I am doing in my view?

Here's what is working now: My application_controller.rb includes this set_category method:

 def set_category
    session[:category] = params[:category] if params[:category].present?
  end

This allows me, in my views, to pass a category into params that I can specify in the link. Here's an example of how I am using a link_to helper for this to work.

 <%= link_to "Play Game".html_safe, game_path(category: 'game'), class: "btn btn-primary btn-md" %>

Great, so the category "game" is being passed into params, and it works.

On the page that loads with the "game" param, a javascript game is played. At the end of the game, I want to be able to set a category for the param again in the link (within the JS). Right now, I am using a static link though:

 Game.prototype.finish = function() {
   window.clearInterval(this.interval);
   this.container.html('Your score was ' + this.score + ' correct out of ' + (this.quizCurrent - 1) + ' questions.<br>
   <br>Now see if you won the <a href="/scoreboard">Scoreboard</a><br>');
  }

If I keep this link in the Javascript file, is it possible to make it dynamic by adding a category like I am doing with my link_to method? Changing it to the following has not triggered the method:

  <a href="/scoreboard(category: 'game')">Scoreboard</a>

I accepted the earlier answer, and I believe all three ways could do the trick. I wanted to detail out what I did in case anyone comes across this question.

Choosing strategy #3, I added a div to my html view to hide dynamic content:

<!-- hide dynamic content --> 
<%= content_tag :div, class: "temp_information", data: {temp:    game_path(category: "game1")} do %>
 <% end %>

In my javascript file, I then defined the temp data so I could call it (in this case, the params) in my link that also is in the javascript file.

 var para = $('.temp_information').data('temp');
  console.log(para); 

The console confirms that the javascript is pulling the category param from the html file, and we're good to go. My last step is to then place the variable in the expression:

 this.container.html('Your score was ' + this.score + ' correct out of ' + (this.quizCurrent - 1) + ' questions.<br>
 <br>Now see if you were winner in the' + '<a style= href="' + para + '"> Scoreboard</a>' + ' for this game.');

And it works.

If I am understanding correctly, you'd like to pass some information from your ruby script to javascript at runtime. You have two options: a) Parse your the params in js, or b) pass something to the javascript by writing js code. Let's look at both options.

The first one is actually pretty easy. If you have /scoreboard?category=games you can see this using location.search . More information on how to parse data can be found on How do I parse a URL query parameters, in Javascript? . I won't go in details, but it's pretty easy to pass this data using the URL.

The second option is a little tricky, mainly because there are a lot of ways to do it. You can pass data to the window object by actually writing javascript. So for example:

<head>
...
<script>
window.categoryId = '<%= @category.id %>';
</script>
</head>
...

If you need to pass a lot of data, you can also convert a lot of data to JSON. You can also pass data using the data attribute in HTML. Here is as example in Rails:

<%= content_tag :div, data: {data: @data} ... %>

You can read more about this Rails: pass data to javascript .

My recommendation is to keep it simple and use the first one. You can do this if your data is just ids or simple queries. If your data is complicated then you would have to use some of the latter options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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