如何将对象id传递给另一个控制器rails 3

时间:2013-03-26 11:12:37

标签: ruby-on-rails ruby activerecord devise ransack

我有问题 我将对象id传递给另一个控制器到另一个控制器之间我使用devise gem执行sign_in操作,当sign_in完成后,下一页显示错误" Active record :: RecordNotFound BookingController #index"并且"找不到没有身份证的房间"  ##我执行步骤中的操作 1:当我浏览" guest_search"页面我使用" Ransack gem"进行了搜索操作。当搜索完成后,我使用params [:id]获取特定id并发送给另一个控制器l.e roomcontroller.rb。

2:当发送另一个控制器执行" before_filter:authenticate_user!"在新控制器上如果用户sing_in并呈现新的控制器动作则给出错误"活动记录:: RecordNotFound BookingController #index"和#34;找不到没有身份证的房间"

RoomController.rb

class RoomsController < ApplicationController   
  def guest_search 
  @search = Room.search(params[:q])
  @roome = @search.result(:distinct => true)
  @room= @roome.where("status IS ?", true).order("room_type_id desc")
 end  
end

guest_search.html.erb

<%= search_form_for @search, url: guest_search_rooms_path, html: {:method =>:post} do |f| %>

  <%= f.label :start_date_eq , "Start Date"%>   
  <%= f.text_field :start_date_eq, class: 'release_date' %>   

  <%=f.label :end_date_lteq, "End Date" %>   
  <%= f.text_field :end_date_lteq, class: 'release_date' %>   

  <%= f.label :Location %>
  <%= f.collection_select :location_id_eq, Location.all, :id, :locationname, :include_blank => true %>

  <%= f.label :RoomType %>
  <%= f.collection_select :room_type_id_eq, RoomType.all,:id,:room_type, :include_blank => true %>

  <%= f.submit "search" %>
  <% end %>

<table class="table table-bordered" border=1 align="center">
  <tr>
      <th style="width: 5%">No.</th>
          <th style="width: 20%">room_no</th>     
      <th style="width: 20%">status</th>      
      <th style="width: 10%">room_type</th> 
      <th style="width: 20%">StartDate</th>   
      <th style="width: 10%">EndDate</th>  
          <th></th>
  </tr>

<% $i=0 %>
<% @room.each do |room| %>

   <tr class="<%= cycle('odd','even') %>"> 
         <td><%= room.room_no %></td>
         <td><%= room.status %></td>         
         <td><%= room.room_type.room_type %></td>
         <td><%= room.start_date %></td>
         <td><%= room.end_date %></td>      
                 <td><%= link_to("Book Now", bookings_path(@room) ,:class=> "btn btn-info") %></td>             
  </tr> 
<% end %>
</table>

BookingController.rb

class BookingsController < ApplicationController

def index
@room = Room.find(params[:id])
end
end

index.html.erb

<h2>Welcome to booking</h2>
<%= @room.id %>

0 个答案:

没有答案