user3334207
user3334207

Reputation: 155

How to capture selection from collection_select

I have the following in the view:

<%= 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%'}) %>

The selection menu displays correctly with the list of items from the model. But I have not been able to figure out how to collect the selected item. Can anyone help?

class MenusController < ApplicationController

    def create
        @menus = Menu.all
    end

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

 end

View:

<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} %>

Another day and I read the suggestion from Mischa where he pointed me to an excellent article on symbols. That got me thinking on the use of symbols. I have two models Menu and Rent, schema.rb below:

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

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

end `
I am using same symbols in both Menu and Rent. Can that cause problems?

I have changed the code based on the suggestions to this post and spent some days now on reading and focusing on the routes file. I am still not getting this to work. When I submit the form the result method is not being run and the value from the collection_select helper is not being picked up. I am wondering if anyone could help with some further suggestions? The code is as follows:
class MenusController < ApplicationController

def create
    @menus = Menu.all
end

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

end

View:
<%= form_tag menus_path do %>

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

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


<%= submit_tag "Hefja leit", :action => "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

Upvotes: 1

Views: 224

Answers (1)

Mischa
Mischa

Reputation: 43308

Instead of:

@results = params[menu][postnumer_id]

You should use:

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

menu and postnumer_id are variables (which are undefined) and :menu and :postnumer_id are symbols. You can read up on symbols in Ruby here.

Upvotes: 1

Related Questions