Rinykia Robshi
Rinykia Robshi

Reputation: 9

undefined method `country' for nil:NilClass

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

This is my hotels#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

Where is wrong?

This is fields in form( hotels#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'

Upvotes: 0

Views: 301

Answers (2)

Vasfed
Vasfed

Reputation: 18464

Your HotelAddress does not save along with @hotel so in show action it is nil

Try adding accepts_nested_attributes_for :addresss to Hotel model and corresponding fields_for in hotel form.

Upvotes: 0

RightAngle
RightAngle

Reputation: 101

use this link with button class and it will work.

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

Here's JsFiddle Example

Upvotes: 1

Related Questions