如何在数组中存储params的值

时间:2017-03-02 10:43:13

标签: ruby-on-rails ruby

我的网址是:

location=Berlin&location=Georgia&location=Kopenhagen

此URL由以下格式生成:

<form action="/posts" method="get">
  <input name="location" value="Berlin" type="checkbox">
  <input name="location" value="Georgia" type="checkbox">
  <input name="location" value="Kopenhagen" type="checkbox">

  <button type="submit">Search</button>
</form>

我的控制器是params[:location]

输出:

Kopenhagen #=> I need all like: Berlin,Georgia,Kopenhagen

如何显示上述所有示例?

3 个答案:

答案 0 :(得分:3)

我认为最好在html(视图)中这样做。

internal func wireUpUI() {

        self.selectedSegmentIndex = 0

        //DispatchQueue.main.async {
            self.tintColor = Color.white

            let segAttributesNormal: NSDictionary = [
                NSForegroundColorAttributeName: Color.white,
                NSFontAttributeName:  UIFont(name: set(), size: fontSize(.medium))!
            ]

            let segAttributesSelected: NSDictionary = [
                NSForegroundColorAttributeName: Color.theme,
                NSFontAttributeName:  UIFont(name: set(), size: fontSize(.medium))!
            ]

            self.setTitleTextAttributes(segAttributesNormal as? [AnyHashable : Any], for: UIControlState.normal)
            self.setTitleTextAttributes(segAttributesSelected as? [AnyHashable : Any], for: UIControlState.selected)
       // }

        // Now bind the data
        bindData(any: [])
    }

然后,在Controller中,您可以像这样获得<form action="/posts" method="get"> <input name="location[]" value="Berlin" type="checkbox"> <input name="location[]" value="Georgia" type="checkbox"> <input name="location[]" value="Kopenhagen" type="checkbox"> <button type="submit">Search</button> </form>

location

location = params[:location] // as array

答案 1 :(得分:0)

location=Berlin&location=Georgia&location=Kopenhagen

我不认为这是有效的网址。您可以将表单更改为

<form action="/posts" method="get">
  <input name="location1" value="Berlin" type="checkbox">
  <input name="location2" value="Georgia" type="checkbox">
  <input name="location3" value="Kopenhagen" type="checkbox">

  <button type="submit">Search</button>
</form>

然后你可以单独访问params [:location1],params [:location2]和params [:location3]

答案 2 :(得分:0)

试试这个location=Berlin,Georgia,Kopenhagen并在控制器中你会得到

params[:location]= "Berlin,Georgia,Kopenhagen"

如果你想要

,你可以在数组中转换它
params[:location]= "Berlin,Georgia,Kopenhagen".join(',')