ruby - Rails 3.2 Rendering Associated Models in Same Form -
i'm working through hartl. ive got microposts section in chapter 10, instead of microposts have introduced hotels, , want add them separate address model, in this tutorial. having trouble - view field not appear enter address
<%= form_for(@hotel) |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 |item| %> <%= f.radio_button :star_rate_hotel, item %> <%= item %><br /> <% end %> <% f.fields_for :address |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 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 display addresses?
you should create associated addresses in new method, create associations @ same time new object created. (to more precise, after object created).
Comments
Post a Comment