i'm working through Hartl. Ive got to the microposts section in Chapter 10, but instead of microposts I have introduced Hotels, and now I want to add them to a separate address model, like in this tutorial. I am having some trouble - my view field does not appear to enter the address
<%= form_for(@hotel) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :title, placeholder: "New hotel title..." %>
<%= f.text_area :room_description, placeholder: "About room..." %>
<%= f.text_field :price, placeholder: "Price?" %>
<%= f.check_box :include_breakfast%>
<%= f.label "Include breakfast?", class: "checkbox inline" %>
<br><% ["1 star", "2 star", "3 star", "4 star", "5 star"].each do |item| %>
<%= f.radio_button :star_rate_hotel, item %> <%= item %><br />
<% end %>
<% f.fields_for :address do |address| %>
<%= f.text_field :street, placeholder: "Street-address..." %>
<%= f.text_field :extended, placeholder: "Extended-address..." %>
<%= f.text_field :locality, placeholder: "Locality..." %>
<%= f.text_field :region, placeholder: "Region..." %>
<%= f.text_field :country, placeholder: "Country..." %>
<% end %>
<p> Upload photo hotel(optionaly)
<%= f.file_field :photo %>
</div>
<p>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
My hotel_controller.rb
class HotelsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def create
@hotel = current_user.hotels.build(params[:hotel])
@hotel.build_address
if @hotel.save
flash[:success]="Hotel created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@hotel.destroy
redirect_to root_url
end
private
def correct_user
@hotel = current_user.hotels.find_by_id(params[:id])
redirect_to root_url if @hotel.nil?
end
end
My user_controller.rb
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:edit, :update, :index, :destroy]
before_filter :correct_user, only: [:edit, :update]
before_filter :admin_user, only: :destroy
def show
@user = User.find(params[:id])
@hotels = @user.hotels.paginate(page: params[:page], per_page: 3)
end
def new
if signed_in?
redirect_to root_path
else
@user = User.new
end
end
def create
if signed_in?
redirect_to root_path
else
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success]="Welcome to the Hotel Advisor!"
redirect_to @user
else
render 'new'
end
end
end
def edit
end
def update
if @user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
def index
@users = User.paginate(page: params[:page], per_page: 7)
end
def destroy
user = User.find(params[:id])
if (current_user == user) && (current_user.admin?)
flash[:error] = "Can not delete own admin account!"
else
user.destroy
flash[:success]="User destroyed"
end
redirect_to users_url
end
private
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
def admin_user
redirect_to(root_path) unless current_user.admin?
end
end
My question is: how do I display addresses?
You should create your associated addresses in your new method, to create the associations at the same time as the new object is created. (to be more precise, just after the object is created).