dax
dax

Reputation: 10997

How to send two variables when using respond_to

I have some ajax that uses the following action.

  def game_type
    if params[:game_type] == "team"
      @game_for = "Team"
      @match_partial = "team_match_fields.html.erb"
    else
      @game_for = "Player"
      @match_partial = "player_match_fields.html.erb"
    end  

    respond_to do |format|
        format.js { @game_for }
    end
  end 

If I use just one variable in the respond_to block, things are fine, but I want to be able to pass both @game_for and @match_partial to my js file. How can I do that? I haven't found a good explanation of syntax for using respond_to.

Upvotes: 0

Views: 604

Answers (2)

usha
usha

Reputation: 29349

All the instance variables are available in the template by default. No need to pass it explicitly.

With just the following code, you should be able to access both @game_for and @match_partial in your js template.

def game_type
    if params[:game_type] == "team"
      @game_for = "Team"
      @match_partial = "team_match_fields.html.erb"
    else
      @game_for = "Player"
      @match_partial = "player_match_fields.html.erb"
    end  

    respond_to do |format|
        format.js
    end
  end 

Upvotes: 3

amit karsale
amit karsale

Reputation: 755

You dont have to pass any variable to your format.js block. Whichever condition is satisfied those particular variables are set and are accessible in your .js.erb file

just mention format.js that's it

Upvotes: 1

Related Questions