0

I try to pass in a variable in a active record query:

class UsersController < ApplicationController
    def show
       @user = User.find_by_username(params[:username])
        @tags = @user.tag_list
        i = 0
        @tags.each do |tag|
          i= i + 1
          @tag"#{i}" = Info.tagged_with(tag.name)
        end
      end
    end

As you can see, I try to pass in a variable in @tag. I tried @tag"#{i}", but this doesn't work. What do I need to type in, to get @tag with the number i? My goal is the following output:

@tag1
@tag2
@tag3

Thanks in advance!

1 Answer 1

1

You can use the instance_variable_set() method:

instance_variable_set(:"@tag#{i}", Info.tagged_with(tag.name))
Sign up to request clarification or add additional context in comments.

Comments

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.