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>