2013-12-11 41 views
0

我正在使用simple_form_for。在這個我想要一個下降與幾個值。這些值是從控制器中返回這些值的散列的方法返回的。錯誤使用'路徑'收集:在simple_form_for

在視圖中我有:

= f.input :address_id, collection: address_string_gift_cards_path 

控制器的方法是:

def address_string 
    #Some code here that returns hash of values 
end 

在我的路線文件我有:

resources :gift_cards do 
    collection do 
     get :address_string 
    end 
    end 

在我收到錯誤的輸出:

undefined method `to_a' for "/gift_cards/address_string":String 

我不知道我在做什麼錯誤。任何幫助將非常感激。在此先感謝:)

+0

你可以發表你的表格 – Kaleidoscope

回答

1

collection:需要一個對象數組。它將這些對象轉換爲可選擇的選項。

你給它一個字符串 - 一個控制器方法的路徑,它將返回值。所以你期待它「去那裏爲我獲取這些價值」,但那不是simple_form_for知道如何去做的。

取而代之,您需要直接將數據傳遞到collection:。你可以在你的控制器聲明address_string是一個輔助方法:

class GiftCardsController < ApplicationController 
    helper_method :address_string 

    def address_string 
    .. 
    end 
end 

helper方法可以直接從視圖中調用:在collection:

= f.input :address_id, collection: address_string 

更多信息,請訪問https://github.com/plataformatec/simple_form#collections

+0

我明白我在做什麼錯誤。 Thnx爲你提供幫助:) – Rads