mercredi 18 octobre 2017

undefined method `count' for nil:NilClass rails

i am trying to have a photo upload section in my room listing. when i try to click on the photos i get this error undefined method `count' for nil:NilClass rails <% if @photos.count > 0 %> . i have added a photo_upload.html.erb page and a _room_menu partial but still i get the error.

here's my code:

photos_controller.rb

       class PhotosController < ApplicationController
        def create
          @room = Room.find(params[:room_id])
          if params[:images]
            params[:images].each do |img|
              @room.photos.create(image:img)
            end
            @photos = @room.photos
            redirect_back(fallback_location:request.referer, notice: "Saved...")
        end
      end


    end

**views/rooms/photo_upload.html.erb**

<div class="row">
  <div class="col-md-3">
    <%= render 'room_menu' %>
  </div>
  <div class="col-md-9">
    <div class="panel panel-default">

      <div class="panel-heading">
        Photos
      </div>

      <div class="panel-body">
        <div class="container">
          <div class="row">
            <div class="col-md-offset-3 col-md-6">
              <!-- PHOTOS UPLOAD GOES HERE -->

              <%= form_for @room, url: room_photos_path(@room), method: 'post', html: {multipart: true} do |f| %>
                <div class="row">
                  <div class="form-group">
                    <span class="btn btn-default btn-file text-babu">
                      <i class="fa fa-cloud-upload" aria-hidden="true"></i> Select Photos
                      <%= file_field_tag "images[]", type: :file, multiple: true %>
                    </span>
                  </div>
                </div>

                <div class="text-center">
                  <%= f.submit "Add Photos", class: "btn btn-form" %>
                </div>

              <% end %>
            </div>
          </div>

          <div id="photos"><%= render 'photos/photos_list' %></div>
        </div>
      </div>

    </div>
  </div>
</div>

views/rooms/_room_menu.html.erb

<ul class="sidebar-list">
  <li class="sidebar-item">
    <%= link_to "Listing", listing_room_path, class: "sidebar-link active" %>
    <span class="pull-right text-babu"><i class="fa fa-check"></i></span>
  </li>
  <li class="sidebar-item">
    <%= link_to "Pricing", pricing_room_path, class: "sidebar-link active" %>
    <% if !@room.price.blank? %>
      <span class="pull-right text-babu"><i class="fa fa-check"></i></span>
    <% end %>
  </li>
  <li class="sidebar-item">
    <%= link_to "Description", description_room_path, class: "sidebar-link active" %>
    <% if !@room.listing_nam.blank? %>
      <span class="pull-right text-babu"><i class="fa fa-check"></i></span>
    <% end %>
  </li>

    <li class="sidebar-item">
    <%= link_to "Photos", photo_upload_room_path, class: "sidebar-link active" %>
    <% if !@room.photos.blank? %>
      <span id="photo_check" class="pull-right text-babu"><i class="fa fa-check"></i></span>
    <% end %>
  </li>

  <li class="sidebar-item">
    <%= link_to "Amenities", amenities_room_path, class: "sidebar-link active" %>
    <span class="pull-right text-babu"><i class="fa fa-check"></i></span>
  </li>
  <li class="sidebar-item">
    <%= link_to "Location", location_room_path, class: "sidebar-link active" %>
    <% if !@room.address.blank? %>
      <span class="pull-right text-babu"><i class="fa fa-check"></i></span>
    <% end %>
  </li>
</ul>
<hr/>

Aucun commentaire:

Enregistrer un commentaire