0

我正在通过 Hartl 工作。我已经到了第 10 章的微博部分,但我介绍了酒店而不是微博,现在我想将它们添加到单独的地址模型中,就像在本教程中一样。我遇到了一些问题 - 我的视图字段似乎没有输入地址

<%= 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 %>

我的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

我的 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

我的问题是:如何显示地址?

4

1 回答 1

0

您应该在新方法中创建关联地址,以便在创建新对象的同时创建关联。(更准确地说,就在对象创建之后)。

检查关于这件事的优秀 Railscast第 1 部分 第 2 部分

于 2013-05-02T21:03:29.710 回答