Ben Smith Ben Smith - 4 months ago 18
Ruby Question

Ruby on Rails: Populate dropdown with two fields of database record

In my database I have a Users table that looks something like:

User_ID Firstname Surname Company
1 Steve Jobs Apple
2 Bill Gates Microsoft


What I am trying to do is make a drop down menu in a form that would allow a user to choose from selecting their name or their company, e.g. when Steve Jobs is logged in he can either select "Steve" or "Apple" in the drop down menu.

What I have tried so far is the following:

<%= f.select :from_name, [session[:user_id],session[:user_id]] %>


Which obviously didn't work because it only returns the user id of the logged in user.

<%= f.select :from_name, [@user.firstname,@user.company] %>


Which gave me the error
undefined method
firstname for nil:NilClass`

My Users controller is as follows:

class UsersController < ApplicationController
before_filter :check_authorization, :except => [:show, :new, :create, :search ]

def index
@users = User.all
end

def show
@user = User.find(params[:id])
end

def new
@user = User.new
end

def edit
@user = User.find(params[:id])
end

def create
@user = User.new(user_params)
@user.role = "customer"
if @user.save
session[:user_id] = @user.id
# Save a copy of the email address entered by the user into the Accounts table
@account = Account.create(email: params[:user][:primaryemailaddress], user_id: session[:user_id])
redirect_to root_path
else
render 'new'
end
end

def update
@user = User.find(params[:id])

if @user.update(user_params)
redirect_to @user
else
render 'edit'
end
end

def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_path
end

private

def user_params
params.require(:user).permit(:title, :firstname, :surname, :housenumber, :street, :city, :postcode, :company, :primaryemailaddress, :password)
end
end


And my _form.html.erb is:

<%= form_for(@email) do |f| %>
<% if @email.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@email.errors.count, "error") %> prohibited this email from being saved:</h2>

<ul>
<% @email.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<p>
<%= f.label :from_name %><br>
<%= f.select :from_name, [current_user.firstname, current_user.company] %>
</p>

<p>
<%= f.label :From_Email_Address %><br>
<%= f.collection_select :account_id, Account.where(user_id: session[:user_id]),
:id,:email %>
</p>

<p>
<%= f.label :to %><br>
<%= f.text_field :to %>
</p>

<p>
<%= f.label :cc %><br>
<%= f.text_field :cc %>
</p>

<p>
<%= f.label :bcc %><br>
<%= f.text_field :bcc %>
</p>

<p>
<%= f.label :subject %><br>
<%= f.text_field :subject %>
</p>

<p>
<%= f.label :message %><br>
<%= f.text_field :message %>
</p>

<div class="actions">
<%= f.submit %>
</div>
<% end %>


I'm not too sure how to solve this issue, can someone please help.

Emu Emu
Answer

@BenSmith I guess you are accessing EmailsController. And on that controller's new or edit method there is no @user variable.

In your edit and new method add

@user = User.find(session[:user_id])

Comments