0

I have a view with three checkboxes:

<div class="tourny-games">

    <h4>Games</h4>

    <div class="checkboxOptions games show-tabs">

      <div class="show-tab">
        <div class="checkbox uncheckBoxed">
          <input id="" name="" onclick="toggleCheckboxImage(this, &#x27;&#x27;);" type="checkbox" value="1" />
        </div>
        <img src="https://s3.amazonaws.com/...iconlol.png.jpg" class="game-logo-label" />
      </div>

      # two more checkboxes here

    </div>

</div>

which create tabs:

<div class="tabs">
  <% @games.each do |game| %>  
    <a href="#<%= game.striped_name %>" game="<%= game.id %>" game-type="<%= Game::TEAM_GAMES.include?(game.name) ? "team" : "single" %>" class="tab" style="<%= "display:none;" unless @tournament.game_ids.include?(game.id) %>">
      <%= game.name %>
    </a> 
  <% end %>  
</div>

via this jquery:

$('.show-tabs .show-tab').live('click', function(){
  var tab = $('.tabs .tab:nth-of-type(' + ( $(this).index() + 1 ) + ')');
  ...      
  if ($(this).find('.checkbox').hasClass('checkBoxed')){
    tab.show();
    ...
  } else {
    tab.hide();
  }
});

Based on this answer, I have the following code to set @game_type

controller:

def game_type
  @game_type = params[:game_type]
end

jquery:

if ($(this).find('.checkbox').hasClass('checkBoxed')){
  tab.show();
  $.get('tournaments/game_type?game_type='+tab.attr("game-type"))

I've tested tab.attr("game-type") with alert so I know it's pulling the variable I want - it returns either team or single - but I need to do some conditional checking in other rails code further down the page - how can I do that?

for example, this pseudocode demonstrates one of the things I'd need to do:

if @game_type == "team"
  "Team Name"
else
  "Player Name"
end

I know that the @game-type variable is loaded with Rails, and therefore would just be blank - but that's where the AJAX comes in, and that's also where I'm a bit confused. This question seems pertinent to what I'm asking, but it's different enough that I can't seem to get it working in my own code.

4
  • 1
    Did not get . you want to this controller action or in jquery? Commented Oct 20, 2013 at 12:49
  • honestly i'm not sure - i want a variable that i can access from the view and i don't much care how i get at it Commented Oct 20, 2013 at 19:20
  • Then you should do it in controller Commented Oct 20, 2013 at 19:35
  • right but how is it accessible from the controller when the checkbox is checked after the rails forms are loaded? Commented Oct 20, 2013 at 19:53

1 Answer 1

1

Looking at what you've written, it seems to me that you're confusing some aspects of your system. I'll briefly explain what I mean, and hopefully a solution:

  1. I would call MVC "modular" - it works best when each element does its job
  2. The "Controller" aspect of MVC is there to handle logic from the view
  3. Therefore, if you need to create a logic-based solution, you'd need to use your controller
  4. Ajax, in my opinion, acts as a pseudo-view -- it creates requests without reloading the page; and should only send & handle responses from the controller (not perform logic)

From what you've said, it seems that you'd be better loading your conditional JS into its own game-type.js.erb from your controller; like this:

def game_type
  respond_to do |format|
      format.js { @game_type = params[:game_type] }
  end
end


#game_type.js.erb
#handle your conditional JS here
alert(<%=j @game_type %>)

You could tie this into your JS like this:

$(document).on('click', '.show-tabs .show-tab', function(){
  var tab = $('.tabs .tab:nth-of-type(' + ( $(this).index() + 1 ) + ')');
  ...      
  if ($(this).find('.checkbox').hasClass('checkBoxed')){
    tab.show();
    $.get('tournaments/game_type?game_type='+tab.attr("game-type"))
  } else {
    tab.hide();
  }
});

That should output the alert box, which means you're getting the variable from the controller. Then it's up to you to do what you want with it!

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your answer! I am admittedly confused, so thanks for explaining things so clearly. I tried out what you suggested and unfortunately it didn't work - no alert, but i'll keep trying.
No problem that it didn't work - I believe the fundamental idea is ok, it's just a case of getting it all to gel. The first question is - is your $.get() code correct? I.E does it actually call the action we need?
that question solved :) had to add get "tournaments/game_type" to routes.rb as per stackoverflow.com/questions/17344045/…
Nice, I was going to recommend something like that, but I figured you'd have done that already
No problem! Hope everything is great for you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.