Reputation: 335
I am getting NoMethodError in Rooms#index, specifically undefined method `boolean_array_from_amenities_integer' for #. I defined "boolean_array_from_amenities_integer" in the Room.rb model however I am not picking it up. Any help would be appreciated.
room.rb
class Room < ActiveRecord::Base
validates :amenities, presence: true
def self.amenities_list
["Smoking Allowed",
"Pets Allowed",
"TV",
"Cable TV",
"Internet",
"Wireless Internet",
"Air Conditioning",
"Heating",
"Elevator in Building",
"Handicap Accessible",
"Pool",
"Kitchen",
"Free parking on premise",
"Doorman",
"Gym",
"Hot Tub",
"Indoor Fireplace",
"Buzzer/Wireless Intercom",
"Breakfast",
"Family/Kid Friendly",
"Suitable for Events",
"Washer",
"Dryer"]
end
def boolean_array_from_amenities_integer
[].tap do |amenities_list|
Room.amenities_list.length do |order|
amenities_list << (self.amenities & 2 ** order > 0)
end
end
end
end
rooms_conroller.rb
class RoomsController < ApplicationController
before_filter :require_current_user!, only: [:new, :create]
def index
@room = Room.all
end
def show
@room = Room.find(params[:id])
end
def new
@room = Room.new
end
def create
@room = Room.new(params[:room])
@room.set_amenities_from_options_list!(params[:room_amenities_indicies])
if @room.save
redirect_to @room
else
flash.now[:errors] = @room.errors if @room.errors
render :new
end
end
private
def room_params params.require(:room).permit(:amenities, :amenities_indices) end end
new.html.erb
<label for="room-amenities" class="top">Amenities</label>
<ul class="group" id="room-amenities">
<% Room.amenities_list.each_with_index do |amenity, index| %>
<li class="checkbox-li">
<input type="checkbox" name="room_amenities_indicies[]" value="<%= index %>">
<%= amenity %>
</input>
</li>
<% end %>
</ul><br>
<input type="submit" class="button blue input-large label-offset-button" value="Create room">
</form>
index.html.erb
<label for="amenities-tab-button">Amenities</label>
<div class="tab-content group">
<% amenities_list = Room.amenities_list %>
<ul>
<% @room.boolean_array_from_amenities_integer.each_with_index do |amenity_available,index| %>
<% if amenity_available %>
<li class="available-amenity">
<% else %>
<li class="unavailable-amenity">
<% end %>
<%= amenities_list[index] %>
</li>
<% end %>
</ul>
</div>
Upvotes: 0
Views: 478
Reputation: 1599
You should make amenities_list
a constant on the model:
AMENITIES_LIST = ["Smoking Allowed",
"Pets Allowed",
"TV",
"Cable TV",
"Internet",
"Wireless Internet",
"Air Conditioning",
"Heating",
"Elevator in Building",
"Handicap Accessible",
"Pool",
"Kitchen",
"Free parking on premise",
"Doorman",
"Gym",
"Hot Tub",
"Indoor Fireplace",
"Buzzer/Wireless Intercom",
"Breakfast",
"Family/Kid Friendly",
"Suitable for Events",
"Washer",
"Dryer"]
You can then call it in the view or controller with Room::AMENITIES_LIST
Not sure what you are trying to do here:
def boolean_array_from_amenities_integer
[].tap do |amenities_list|
Room.amenities_list.length do |order|
amenities_list << (self.amenities & 2 ** order > 0)
end
end
end
Upvotes: 0
Reputation: 1136
It is often good to follow naming (and other) conventions.
What you have in your view is an attempt to retrieve an the boolean_array from an array of room instances, when your intention is to retrieve it from each instance.
In the controller:
def index
@rooms = Room.all # <=== use the plural
end
And in the index view:
<% @rooms.each do |room| %>
...
<% room.boolean_array_from_amenities_integer.each_with_index do |amenity_available,index| %>
...
<% end %>
...
<% end %>
Upvotes: 0
Reputation: 7288
there is a syntax error in controller
you forgot to close the index method with "end"
def index
@room = Room.all
end
def show
@room = Room.find(params[:id])
end
Edited
in Index view you use @room variable which is array calculated from index action, you need to get the instance of room model.
@room = Room.all
Upvotes: 0