rails_developer
rails_developer

Reputation: 149

rails 3.2 collection_select not loading values

I'm having an issue using collection_select to create a drop down menu. all the guides say that this

<%= collection_select(:page, :user_id, @users, :id, :full_name) %>

should work but when I run the server, instead of a list of users appearing it's just a blank list. The associations are that page belongs a user and a user has many pages, and there are users in the database that should be obtained in the controller when I call @users = User.all

Any idea on how to get the drop down list to populate?

Upvotes: 0

Views: 296

Answers (2)

rewritten
rewritten

Reputation: 16435

Do you load anything into the @users variable in your PageController ?

If it does not belong (semantically) to the controller, make it a helper method

module PagesHelper
  def users_for_select
    User.all
  end
end

and in the view

<%= collection_select(:page, :user_id, users_for_select, :id, :full_name) %>

Check also in the console that :full_name is a proper method of a User instance

User.first.full_name

Edit: proposal for a full_name method with fallback

class User < ActiveRecord::Base
  def full_name
    "#{first_name} #{last_name}".presence or name
  end
end

Upvotes: 1

user1455116
user1455116

Reputation: 2144

i too went through the apidocuments,

somewhere it says that no selection is made without including :prompt or :include_blank if your calling method is nil,

try <%= collection_select(:page, :user_id, @users, :id, :full_name, :prompt=>true) %>

Upvotes: 1

Related Questions