nil的未定义方法`country':NilClass

时间:2015-12-06 15:12:30

标签: ruby-on-rails haml

class HotelsController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :show]
def index
        @hotels = Hotel.where(:star_rating => 5).limit(5)
    end
    def all_hotels
        @hotels = Hotel.order("star_rating")
    end
    def new
        @hotel = Hotel.new
        @hotel.build_address
    end
    def create
        @hotel = Hotel.new(hotel_params)
        if @hotel.save
            redirect_to hotels_path
        else
            render 'new'
        end
    end
    def show
        @hotel = Hotel.find(params[:id])
    end
    def edit
        @hotel = Hotel.find(params[:id])
    end
    def update
        @hotel = Hotel.find(params[:id])

        if @hotel.update_attributes(hotel_params)
            redirect_to hotel_path
        else
            render 'edit'
        end
    end
    def destroy
        @hotel = Hotel.find(params[:id])
        @hotel.destroy
        redirect_to hotels_path
    end
    private
    def hotel_params
        params.require(:hotel).permit(:title, :star_rating, :breakfast, :room_description, :image, :price)
    end
end

这是我的酒店#show

.jumbotron
.container
.col-md-6
  =image_tag @hotel.image, :class => "img-rounded", :width => "500", :height => "400"
.col-md-5.pull-right
  %h2= @hotel.title
  %p= @hotel.room_description
  %p
    Rating:
    = @hotel.star_rating

  -if @hotel.breakfast == true
    %p Breakfast included
  -else
    %p Breakfast not included

  %h3 Address:
  %p
    Country:
    = @hotel.address.country
  %p
    City:
    = @hotel.address.city
  %p
    State:
    = @hotel.address.state
  %p
    Street:
    = @hotel.address.street

  %p= link_to "Back", hotels_path

哪里错了?

这是表格中的字段(酒店#new)

.container
= simple_form_for @hotel, :html => { :class => "form-horizontal", :type => "form", :multipart => true } do |f|

-if f.error_notification
  .alert.alert-danger
    =f.error_notification

.form-group
  .col-sm-10
    = f.input :title, input_html: { class: 'form-control col-sm-2' }
.form-group
  .col-sm-10
    = f.input :room_description, as: :text, input_html: { class: 'form-control col-sm-2' }
.checkbox
  = f.input :breakfast, as: :check_boxes, as: :boolean
.form-group
  .col-sm-10
    =f.input :star_rating, as: :radio_buttons, collection: 1..5
.form-group
  .col-sm-10
    =f.input :image, as: :file, input_html: { class: 'form-control col-sm-2' }
.form-group
  .col-sm-10
    =f.simple_fields_for :address do |addr|
      =addr.input :country, collection: @country, input_html: { class: 'form-control col-sm-2' }
      =addr.input :city, input_html: { class: 'form-control col-sm-2' }
      =addr.input :state, input_html: { class: 'form-control col-sm-2' }
      =addr.input :street, input_html: { class: 'form-control col-sm-2' }
.form-group
  .col-sm-10
    = f.input :price, input_html: { class: 'form-control'}
.form-group
  .col-sm-10
    = f.button :submit, :class => 'btn-primary'

2 个答案:

答案 0 :(得分:1)

将此链接与按钮类一起使用,它将起作用。

    <a href="#myModal" class="btn btn-default" data-toggle="modal" role="button"> Launch demo modal </a>

此处JsFiddle Example

答案 1 :(得分:0)

HotelAddress @hotelshow一起保存,nil行动accepts_nested_attributes_for :addresss

尝试将Hotel添加到fields_for模型以及酒店表单中的相应java.exe -jar C:\Users\****\Documents\NetBeansProjects\WindowsSecurity\dist\WindowsSecurity.jar

相关问题