我在Rooms#index中得到NoMethodError,特别是未定义的方法`boolean_array_from_amenities_integer'用于#。我在Room.rb模型中定义了“boolean_array_from_amenities_integer”但是我没有把它拿起来。任何帮助将不胜感激。
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
私有
def room_params params.require(:room).permit(:amenities,:amenities_indices) 结束 端
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>
答案 0 :(得分:0)
控制器中存在语法错误
你忘了用“结束”
关闭索引方法 def index
@room = Room.all
end
def show
@room = Room.find(params[:id])
end
被修改
在索引视图中,您使用@room变量,它是从索引操作计算的数组,您需要获取房间模型的实例。
@room = Room.all
答案 1 :(得分:0)
遵循命名(和其他)约定通常很好。
您的视图中的内容是尝试从房间实例数组中检索boolean_array,而您的目的是从每个实例中检索它。 / p>
在控制器中:
def index
@rooms = Room.all # <=== use the plural
end
在索引视图中:
<% @rooms.each do |room| %>
...
<% room.boolean_array_from_amenities_integer.each_with_index do |amenity_available,index| %>
...
<% end %>
...
<% end %>
答案 2 :(得分:0)
你应该让amenities_list
在模型上保持不变:
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"]
然后,您可以使用Room::AMENITIES_LIST
不确定你在这里要做什么:
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