George Jor
George Jor

Reputation: 135

How to loop rails variable?

I have totally nine buttons in rails. I have input the data into the database by manually typing the @button_1.save function.

My question is:

How can i have the @button_i.save function in rails? I have finished the things in the for loop, what is left is the button save functions.

Many thanks!

      button_number = params[:button_number]

      for i in (1..button_number)
         instance_variable_set("@button#{i}", 
                               Button.new(:title => params["button_title_#{i}".to_sym], 
                               :order => i, 
                               :icon_url => params["button_icon_#{i}".to_sym], 
                               :navigation_id => @navigation.id, 
                               :next_navigation => params["selected_navigation_#{i}".to_sym].to_i, 
                               :next_page => params["selected_page_#{i}".to_sym].to_i))
         instance_variable_set("@button#{i}")
      end

      @button1.save
      @button2.save
      @button3.save
      @button4.save
      @button5.save
      @button6.save

Upvotes: 2

Views: 132

Answers (4)

accounted4
accounted4

Reputation: 1705

The opposite of instance_variable_set is instance_variable_get, which I think will lead you to the correct answer:

1.upto(params[:button_number].to_i) do |i|
  instance_variable_set("@button#{i}", 
    Button.new(
      :title => params["button_title_#{i}".to_sym], 
      :order => i, 
      :icon_url => params["button_icon_#{i}".to_sym], 
      :navigation_id => @navigation.id, 
      :next_navigation => params["selected_navigation_#{i}".to_sym].to_i, 
      :next_page => params["selected_page_#{i}".to_sym].to_i
    )
  )
  instance_variable_get("@button#{i}").save
end

Upvotes: 2

sjain
sjain

Reputation: 23344

May be this that you want -

button_number = params[:button_number].to_i

for i in (1..button_number)
             instance_variable_set("@button#{i}", 
                                   Button.new(:title => params["button_title_#{i}".to_sym], 
                                   :order => i, 
                                   :icon_url => params["button_icon_#{i}".to_sym], 
                                   :navigation_id => @navigation.id, 
                                   :next_navigation => params["selected_navigation_#{i}".to_sym].to_i, 
                                   :next_page => params["selected_page_#{i}".to_sym].to_i))
             instance_variable_set("@button#{i}")

      "@button#{i}".save

      end

Upvotes: -1

Ganesh Kunwar
Ganesh Kunwar

Reputation: 2653

Try by using constantize ruby function because I think your function call statement is in string.

button_number = params[:button_number]

      for i in (1..button_number)
         instance_variable_set("@button#{i}", 
                               Button.new(:title => params["button_title_#{i}".to_sym], 
                               :order => i, 
                               :icon_url => params["button_icon_#{i}".to_sym], 
                               :navigation_id => @navigation.id, 
                               :next_navigation => params["selected_navigation_#{i}".to_sym].to_i, 
                               :next_page => params["selected_page_#{i}".to_sym].to_i))
        "@button#{i}".constantize.save();
      end

Upvotes: 0

Valery Kvon
Valery Kvon

Reputation: 4496

for i in ...
  eval("@button#{i}.save")
end

Upvotes: 2

Related Questions