2

我在导轨上总共有九个按钮。我已经通过手动输入@button_1.save函数将数据输入数据库。

我的问题是:

我怎样才能在 Rails 中使用 @button_i.save 功能?我已经完成了for循环中的东西,剩下的就是按钮保存功能。

非常感谢!

      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
4

4 回答 4

2

instance_variable_setis的反面instance_variable_get,我认为这会引导你找到正确的答案:

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
于 2013-01-16T07:41:00.293 回答
2
for i in ...
  eval("@button#{i}.save")
end
于 2013-01-16T07:41:09.183 回答
0

尝试使用constantizeruby​​ 函数,因为我认为您的函数调用语句是字符串。

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
于 2013-01-16T07:27:12.930 回答
-1

可能是你想要的——

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
于 2013-01-16T07:30:58.340 回答