简体   繁体   中英

Active Admin: Form with datepicker on custom page

Alright I made a custom page in Active Admin called "Newest Rooms" and it shows a table with the Hotel Rooms of the current date.

Now I want to add a form the this custom page where I can pick the Date. I've managed to make the form appear with the Datepicker through:

<%= semantic_form_for :newest_rooms, :builder => ActiveAdmin::FormBuilder do |f|
   f.inputs do
     f.input :Datum, :as => :datepicker
   end
   f.buttons
end %>

But no idea how to send this to the right controller and to the method HotelRoom.newest_rooms

I hope someone can explain to me how to do this. I've added the code below:

newest_room.rb

ActiveAdmin.register_page "Newest Rooms" do

  menu :label => "Newest Rooms"


  content do
      render "newest_rooms"
  end
end

_newest_room.html.erb

<% @cities = Hotel.cities %>

<%= semantic_form_for :newest_rooms, :builder => ActiveAdmin::FormBuilder do |f|
   f.inputs do
     f.input :Datum, :as => :datepicker
   end
   f.buttons
end %>

<ul class="room_list">
<% @cities.each do |c| %>
<li>
    <table>
    <tr>
        <td>
            <h2><%= c.City %></h2>
        </td>
    </tr>
    <tr class="room_column">
        <td>Hotel</td>
        <td>Free Rooms</td>
        <td>BN-Price</td>
        <td>Old Price</td>
    </tr>
    <% @rooms = HotelRoom.newest_rooms(c.City) %>
    <% @rooms.each do |r| %>
    <tr>
        <td><%= r.hotel.Hotelname %></td>
        <td><%= r.FreeRooms %></td>
        <td><b><%= r.Price %>€</b></td>
        <td><%= r.OldPrice %>€</td>
    </tr>   
    <%end%>

</table>
</li>
<% end %>
</ul>

hotel_room.rb

class HotelRoom < ActiveRecord::Base
  validates :title, :presence => true
  self.table_name = "hotel_room"

  belongs_to :hotel, :foreign_key => 'H_ID'
  accepts_nested_attributes_for :hotel 


  def to_key
    [self.ID]
  end


  def self.newest_rooms(city)

      HotelRoom.find(:all, :joins => :hotel, :conditions => ["hotel.City = ? and hotel_room.Date = ?", city, Date.today])

  end

end

在你的语义形式中添加一个url,比如......

<%= semantic_form_for :newest_rooms, :url => hotel_newest_room_path, :builder => ActiveAdmin::FormBuilder do |f| %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM