Ruby on Rails(v. 3.2.13)でちょっとしたレストランのウェブサイトを作ろうとしています。
Customer(Name,Email), Reservation(customer_id,table_id), Table(seats,area) の 3 つのテーブルを作成しました。
私のモデルは次のようになります。
class Reservation < ActiveRecord::Base
belongs_to :customer
belongs_to :table
end
class Customer < ActiveRecord::Base
has_many :reservations
has_many :tables, :through => :reservations
end
class Table < ActiveRecord::Base
has_many :reservations
has_many :customers, :through => :reservations
end
適当なテーブルを探すためのフォームを作りました。顧客が自分のテーブルを見つけたら、ボタンをクリックして予約します。このボタンは、「新しい顧客を追加する」ビューにつながります。ボタンは次のようになります。
<%= button_to "Book table #{table.id}" , customers_path(:table_id => table) %>
でCustomerController
、create メソッドを次のように編集しました。
def create
@customer = Customer.new(params[:customer])
table = Table.find(params[:table_id])
respond_to do |format|
if @customer.save
format.html { redirect_to @customer, notice: 'Customer was successfully created.' }
format.json { render json: @customer, status: :created, location: @customer }
@reservation = @customer.reservations.build(table: table).save!
else
format.html { render action: "new" }
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end
残念ながら、「Table.find」は機能しません。「Table.first」を使用すると予約が追加されますが、上記のようにテーブルを使用すると、「id=" のテーブルが見つかりませんでした。ただし、NewCustomer ビューに ID を表示することはできます。
create
メソッドでIDが利用できないのはなぜですか?
編集:
テーブルを検索するためのメインページのフォームは次のとおりです。さらに、テーブル検索用の新しいリソース メインページを作成したと言わざるを得ません。
<%= form_tag mainpages_path, :method => 'get' do %>
<p>
How many persons: <br/><%= number_field_tag(:search) %> <br/>
Date: <%= date_select :date, 'Date', use_short_month: true, order: [:day, :month, :year] %> <br/>
Beginn: <%= time_select :Beginn, 'Beginn' , default:Time.now %> <br/>
End : <%= time_select :Ende, 'Ende' , default: Time.now + 2.hours %> <br/>
<%params[:search]%>
<%= submit_tag "Search" %>
</p>
<% end %>
<% if @tables %>
<h2>Folgende Tische stehen zur Auswahl</h1>
<% @tables.each do |table| %>
<div class="entry" >
<h3>Seats: <%= table.seats %></h3>
<h3>Area: <%= table.area %></h3>
<%= link_to "Book this table #{table.id}" , new_customer_path(:table_id => table) %>
</div>
<% end %>
<% else %>
<p> Pls choose a table</p>
<% end %>
EDIT2: HTML コードへのリンク:
<a href="/customers/new?table_id=1" mode="post">Book this table 1</a>
そして、customer/new-view の HTML コードは次のとおりです。
<!DOCTYPE html>
<html>
<head>
<title>Restaurant</title>
<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/customers.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/mainpages.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/reservations.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/scaffolds.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/tables.css?body=1" media="all" rel="stylesheet" type="text/css" />
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/customers.js?body=1" type="text/javascript"></script>
<script src="/assets/mainpages.js?body=1" type="text/javascript"></script>
<script src="/assets/reservations.js?body=1" type="text/javascript"></script>
<script src="/assets/tables.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="ej1MgV2fad014SLkCv3dZXl8TknQH4JHZLoe56Xn/Kk=" name="csrf-token" />
</head>
<body>
<h1>New customer</h1>
<form accept-charset="UTF-8" action="/customers" class="new_customer" id="new_customer" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="ej1MgV2fad014SLkCv3dZXl8TknQH4JHZLoe56Xn/Kk=" /></div>
<div class="field">
<label for="customer_name">Name</label><br />
<input id="customer_name" name="customer[name]" size="30" type="text" />
</div>
<div class="field">
<label for="customer_email">Email</label><br />
<input id="customer_email" name="customer[email]" size="30" type="text" />
</div>
<input id="table_id" name="table_id" type="hidden" />
<div class="actions">
<input name="commit" type="submit" value="Create Customer" />
</div>
</form>
<a href="/customers">Back</a>
</body>
</html>