簡體   English   中英

如何從collection_select中捕獲選擇

[英]How to capture selection from collection_select

我認為以下內容:

<%= collection_select(:menu, :postnumer, @menus, :postnumer_id, :postnumer,
    options = {:prompt => "Veldu póstnúmer", :include_blank => "Ekkert valið"},
    html_options = {:class =>"menus", :multiple => true, :style => 'width:15%'}) %>

選擇菜單正確顯示,並帶有模型中的項目列表。 但是我還無法弄清楚如何收集選定的物品。 有人可以幫忙嗎?

class MenusController < ApplicationController

    def create
        @menus = Menu.all
    end

    def result
        @results = params[menu][postnumer_id]
    end

 end

視圖:

<th>Póstnúmer:</th></br>
<%= collection_select(:menu, :postnumer, @menus, :postnumer_id, :postnumer, options =         {:prompt => "Veldu póstnúmer", :include_blank => "Ekkert valið"}, html_options = {:class =>"menus", :multiple => true, :style => 'width:15%'}) %></br>
<%= button_to "Hefja leit", {:controller => :menus, :action => :result}, {:method =>    :post} %>

改天,我讀了Mischa的建議,他為我指出了一篇有關符號的出色文章。 那讓我開始思考符號的使用。 我下面有兩個模型Menu和Rent,schema.rb:

`ActiveRecord :: Schema.define(version:20140403200844)做

create_table "menus", force: true do |t|
    t.string   "postnumer"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "postnumer_id"
end

create_table "rents", force: true do |t|
    t.string   "fastanumer"
    t.string   "postnumer"
    t.string   "gotuheiti"
    t.float    "flatarmal"
    t.float    "fermetraverd"
    t.float    "upr_fermetraverd"
    t.float    "verd"
    t.datetime "dagsetning"
    t.string   "sveitarfelagsnumer"
    t.integer  "byggingarar"
    t.integer  "fjoldiherbergja"
    t.string   "matssvaedi"
    t.string   "ist120"
    t.float    "visitala_start"
    t.float    "visitala_end"
    t.string   "undirmatssvaedi"
    t.float    "fasteignamat"
    t.datetime "created_at"
    t.datetime "updated_at"
end

結束`
我在“菜單”和“租用”中都使用相同的符號。 會引起問題嗎?

我已根據本文的建議更改了代碼,並且花了幾天的時間閱讀和關注route文件。 我仍然沒有使這個工作。 當我提交表單時,結果方法沒有運行,並且沒有從collection_select幫助器中獲取值。 我想知道是否有人可以提供進一步的建議? 代碼如下:
class MenusController <ApplicationController

def create
    @menus = Menu.all
end

def result
    @results = params[:menu][:postnumer_id]
end

結束

視圖:
<%= form_tag menus_path做%>

<%= label_tag'Póstnúmer:'%>

<%= collection_select(:menu,:postnumer,@menus,:postnumer_id,:postnumer,options = {:提示=>“ Veldupóstnúmer:”},html_options = {:class =>“ menus”,:multiple => true ,:style =>'width:15%'})%>


<%= Submit_tag“ Hefja leit”,:action =>“結果”,:controller =>“菜單”%>

<% end %>  


Pricetorent::Application.routes.draw do
    resources :menus, :only => [:result]
    get "menus/create"
    post "menus/result"
    resources :menus do
        put :result, :on => :collection
    end
end

代替:

@results = params[menu][postnumer_id]

您應該使用:

@results = params[:menu][:postnumer_id]

menupostnumer_id是變量(未定義),而:menu:postnumer_id是符號。 您可以在此處閱讀Ruby中的符號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM