2

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
1
  • may you post your error. Commented Jan 16, 2013 at 7:29

4 Answers 4

2
for i in ...
  eval("@button#{i}.save")
end
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

i add .to_i to the params and make it correct. thanks! params[:button_number].to_i
0

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

2 Comments

how to use the constantize function?
you can use constanize function in function call statement
-1

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

4 Comments

thanks man, but it shows "bad value for range", how to deal with it?
this is because the params[:button_number] that you are getting is an object and not a number. Please check my updated post above.
good to know that the solution worked. :) but did eval solved it too as you accepted that as a answer and in below post you commented that .to_i to the params made it correct.?
ya, the problem is the .to_i affect the most

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.