【发布时间】:2014-04-04 19:00:04
【问题描述】:
我在视图中有以下内容:
<%= 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(版本: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
结束
`
我在菜单和租金中使用相同的符号。这会导致问题吗?
我已经根据这篇文章的建议更改了代码,现在花了几天时间阅读并专注于路由文件。我仍然没有让这个工作。当我提交表单时,结果方法没有被运行,并且来自 collection_select 帮助器的值没有被拾取。我想知道是否有人可以提供一些进一步的建议?代码如下:
类 MenusController
def create
@menus = Menu.all
end
def result
@results = params[:menu][:postnumer_id]
end
结束
查看:
"Veldu póstnúmer:"}, html_options = {:class=> "menus", :multiple => true , :style => 'width:15%'}) %>
"result", :controller => "menus"%>
<% 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
【问题讨论】:
-
如果你选择一个条目并提交表单,参数是什么?
-
也许我最初的问题是错误的。我不知道如何提交表格?这就是我的问题。
-
能否分享完整的表单代码和对应的Controller动作。
-
我认为这个链接会帮助你guides.rubyonrails.org/form_helpers.html。但我建议你阅读一些初学者教程,如ruby.railstutorial.org/chapters/beginning
-
我已经阅读了 Apress.Beginning.Ruby。几乎完成了Beginning Rails 4。对不起,我是个新手。
标签: ruby-on-rails