Reputation: 23
Okay so you can see my models and controller below. What I want to be able to do is when a user is adding a new lease, the user can select from a list of the properties already in the database for the property_id, and also select from a list of the tenants on file for the tenant_id. I am relatively new to rails and really don't have a clue how I would go about doing this. In my controller I put @property = Property.all @tenant = Tenant.all to make them accessible but I don't know how to leverage them in the way that I want.
Lease Model
class Lease < ActiveRecord::Base
attr_accessible :lease_end, :lease_start, :property_id, :tenant_id
belongs_to :tenant
belongs_to :property
end
Property Model
class Property < ActiveRecord::Base
attr_accessible :address_line_1, :city, :county, :image, :rent
belongs_to :lease
end
Tenant Model
class Tenant < ActiveRecord::Base
attr_accessible :email, :name, :phone
belongs_to :lease
end
Lease controller methods for adding a new lease and editing a lease
def new
@lease = Lease.new
@property = Property.all
@tenant = Tenant.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: @lease }
end
end
# GET /leases/1/edit
def edit
@lease = Lease.find(params[:id])
@property = Property.all
@tenant = Tenant.all
end
EDIT: Drop down box work but the options arn't wahat I want. I am getting options like # for tenants and # for properties. I would like if I could get the tenants names and the address of the properties instead
_form file code updated with teresko's suggestions
<%= form_for(@lease) do |f| %>
<% if @lease.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@lease.errors.count, "error") %> prohibited this lease from
being saved:</h2>
<ul>
<% @lease.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :tenant_id %><br />
<%= f.select :tenant, options_for_select(@tenants) %>
</div>
<div class="field">
<%= f.label :property_id %><br />
<%= f.select :property, options_for_select(@properties) %>
</div>
<div class="field">
<%= f.label :lease_start %><br />
<%= f.date_select :lease_start %>
</div>
<div class="field">
<%= f.label :lease_end %><br />
<%= f.date_select :lease_end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Upvotes: 1
Views: 96
Reputation: 3371
First tip : the good practice is to use @properties
when you have many properties, not @property
. Idem with @tenants
.
Then, you can set this in the new or edit page :
<% form_for @lease do |f| %>
<%= f.select :property, options_for_select(@properties) %>
<%= f.select :tenant, options_for_select(@tenants) %>
<% end %>
Next tip is to use a partial named app/views/leases/_form.html.erb
to set the previous form, when the new
and edit
are rendering the same form. Then, your new and edit views will become
<%= render :partial => 'form' %>
To have specific option display, you can read the options_for_select or options_from_collection_for_select docs
<%= f.select :property, options_from_collection_for_select(@properties, 'id', 'name') %>
Choose the best method to your case.
Upvotes: 1