我在视图中有以下内容:
<%= 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} %>
另一天,我读了米沙的建议,他向我指出了一篇关于符号的优秀文章。 这让我想到了符号的使用。 我有两个模型Menu and Rent,schema.rb如下:
`ActiveRecord :: Schema.define(版本: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
端
`
我在Menu和Rent中使用相同的符号。这会导致问题吗?
我已经根据对这篇文章的建议更改了代码,现在花了几天时间阅读并关注路径文件。我仍然没有让这个工作。当我提交表单时,结果方法没有运行,并且没有拾取来自collection_select助手的值。我想知道是否有人可以提供一些进一步的建议?代码如下:
class MenusController&lt; ApplicationController中
def create
@menus = Menu.all
end
def result
@results = params[:menu][:postnumer_id]
end
结束
查看:
&lt;%= form_tag menus_path do%&gt;
&lt;%=label_tag'Póstnúmer:'%&gt;
&lt;%= collection_select(:menu,:postnumer,@ menus,:postnumer_id,:postnumer,options = {:prompt =&gt;“Veldupóstnúmer:”},html_options = {:class =&gt;“menus”,:multiple =&gt; true,:style =&gt;'width:15%'})%&gt;
&lt;%= submit_tag“Hefja leit”,:action =&gt; “结果”,:controller =&gt; “菜单” %&GT;
<% 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
答案 0 :(得分:1)
而不是:
@results = params[menu][postnumer_id]
您应该使用:
@results = params[:menu][:postnumer_id]
menu
和postnumer_id
是变量(未定义),:menu
和:postnumer_id
是符号。您可以阅读Ruby here中的符号。