Reputation: 67
Im trying to do a little Restaurant-Website in Ruby on Rails(v. 3.2.13).
I made 3 tables: Customer(Name,Email), Reservation(customer_id,table_id), Table(seats,area).
My Models look like this.
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
I made a form for searching a suitable table. If the customer finds his table, he clicks a button to book it. This button leads to the "add a new customer" view. The button looks like this:
<%= button_to "Book table #{table.id}" , customers_path(:table_id => table) %>
In the CustomerController
, I edited the create method like the following:
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
Sadly the "Table.find" does not work. When I use "Table.first" a Reservation is added, but when I use table like above I get the message: "Couldn't find Table with id=". I can, however, display the ID on the NewCustomer view.
Why is the ID not available in the create
method?
EDIT:
Here is the form of the mainpage to search a table. Furthermore I have to say that i made a new resource mainpage for the table-search.
<%= 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: The link to HTML-Code:
<a href="/customers/new?table_id=1" mode="post">Book this table 1</a>
And here is the HTML-Code of the customer/new-view:
<!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>
Upvotes: 0
Views: 277
Reputation: 67
Hello after 1 million tries I found the mistake! The solution with the hidden_field_tag was partly correct. All the time I tried <%= hidden_field_tag :table_id %>
. But with this code the create action knows the variable :table_id
but it has an empty string. Now I added params[:table_id]
to the hidden_field and now the value of the variable :table_id
is availlable.
Long story made short, variable name AND value have to be in the hidden_field_tag
!
<%= hidden_field_tag :table_id, params[:table_id] %>
Thanks to everyone who tried helping me!
Upvotes: 0
Reputation: 290
Based on your last bit of code you posted, you need to have:
<%= link_to "Book this table #{table.id}" , customer_path(:table_id => table), mode: :post %>
Upvotes: 0
Reputation: 671
The problem here is the use of button_to
...
From the Url Helpers documentation:
Generates a form containing a single button that submits to the URL created by the set of options.
The action attribute of the form needs to look similar to this:
/customers/new?table_id=<table_id>
When you click this button it should route to the Customer new
action that renders the new
view. You need to pass table_id
to the new
view and then add it to your Customer form. Something like this:
def new
...
@table_id = params[:table_id]
end
Then, when you submit the Customer form
- which will route to the create
action, you should have access to params[:table_id]
Upvotes: 0
Reputation: 7225
try this out
<%= button_to "Book table #{table.id}" , customers_path(:table_id => table), method: :post %>
Upvotes: 1
Reputation: 563
change in the controller (just add an@ symbol)
@table = Table.find(params[:table_id])
in view
<%= button_to "Book table #{table.id}" , customers_path(:table_id => @table) %>
Upvotes: 1