未初始化的常数保留

时间:2012-03-27 12:27:11

标签: ruby-on-rails ruby-on-rails-3.1

嗨,我的应用程序有点麻烦,我在ROR开发中的新功能 我做了一个静态页面,我的预订功能室 应用程序显示,可以添加 reservation_function_room(预订功能室的行项目)它 当我路由到<%= link_to "add functionrooms", reservation_pages_functionroom_path(@reservation) %>时,无法预测未初始化的常数保留错误 非常感谢提前谢谢

页面静态页面

class PagesController < ApplicationController

  def functionroom
    @reservation = Reservation.find(params[:reservation_id])
    @function_room = FunctionRoom.all
  end
end

functionroom.html.erb

<% if notice %>
<p id = "notice"><%= notice%></p>
<%end%>

  <h1>functionRooms</h1>
  <%@function_room.each do |functionroom|%>
  <h3><%= functionroom.name%></h3>
  <p><%= number_to_currency(functionroom.price)%></p>
  <%= button_to 'add function room',
reservation_reservation_function_room_path(:function_room_id =>
functionroom), :method => :post,:remote => true%>
  <%end%>

reservation_contoller.rb

def index
    @reservations = Reservation.all
  end

  def show
    @reservation = Reservation.includes(:reservation_function_rooms =>
:function_room,:reservation_package => :package).find(params[:id])
  end

class ReservationFunctionRoomsController < InheritedResources::Base
 def show
   @reservation_function_room =
ReservationFunctionRoom.find(params[:id])
 end


def new
  @reservation_function_room = ReservationFunctionRoom.new
end

def create
  @reservation = Reservation.find(params[:reservation_id])
  function_room = FunctionRoom.find(params[:function_room_id])
  @reservation_function_room =
@reservation.add_function_room(function_room.id)

  if @reservation_function_room.save
    redirect_to @reservation, :notice => "function room successfuly
added"
  end
end
end

路由

  get "pages/menu"

  resources :reservation_function_rooms




  resources :services

  resources :reservations do
     get "pages/functionroom"
  end

  resources :reservation_packages

  resources :package_line_items


  resources :packages do
    resources :package_crews
  end

  resources :function_rooms

reservation.rb

class Reservation < ActiveRecord::Base
  has_one :reservation_package
  belongs_to :service
  has_many :reservation_function_rooms
  has_many :package_line_items
  has_many :menus , :through => :package_line_items, :uniq => true
  has_many :function_rooms, :through =>:reservation_function_rooms

    def add_function_room(function_room_id)
     current_function_room =
reservation_function_rooms.find_by_function_room_id(function_room_id)
     if current_function_room
       redirect_to @reservation, :notice => "function room already
added"
     else
     current_function_room =
reservation_function_rooms.build(:function_room => function_room_id)
     current_function_room.price =
current_function_room.function_room.price
     end
     current_function_room
    end

end

预订/ show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <b>Name:</b>
  <%= @reservation.name %>
</p>

<%= display_package @reservation%>
<p>
  <b>Address:</b>
  <%= @reservation.address %>
</p>

<p>
  <b>Contact:</b>
  <%= @reservation.contact %>
</p>

<p>
  <b>Date:</b>
  <%= @reservation.date %>
</p>

<p>
  <b>Timestart:</b>
  <%= @reservation.timeStart %>
</p>

<p>
  <b>Timeend:</b>
  <%= @reservation.timeEnd %>
</p>

<p>
  <b>Numguest:</b>
  <%= @reservation.numGuest %>
</p>

<p>
    <%= link_to "add functionrooms", reservation_pages_functionroom_path(@reservation) %>
</p>



<table>
    <tr>
        <th>Function room</th>
        <th>Price</th>
    </tr>
    <% @reservation.reservation_function_rooms.each do |room|%>
    <tr>
        <td><%= room.function_room.name%></td>
        <td><%= room.function_room.price%></td>
    </tr>



<%end%>


</table>





<%= link_to 'Edit', edit_reservation_path(@reservation) %> |
<%= link_to 'Back', reservations_path %>

如果需要任何事情,请提前提前致谢:)

1 个答案:

答案 0 :(得分:0)

我知道它已经4年了,但我遇到了同样的问题,我想我可能已经找到了它的来源。

你有依赖&#34; has_one:reservation_package&#34; 并且你有'34'资源:reservation_packages&#34;

我认为&#34; s&#34;这是问题:要么做单个资源要么做has_many关系。这就是他无法找到的原因。 这就是我如何解决我的问题。