Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use params in another action?
    primarykey
    data
    text
    <p>Im trying to do a little Restaurant-Website in Ruby on Rails(v. 3.2.13).</p> <p>I made 3 tables: Customer(Name,Email), Reservation(customer_id,table_id), Table(seats,area).</p> <p>My Models look like this.</p> <pre><code>class Reservation &lt; ActiveRecord::Base belongs_to :customer belongs_to :table end class Customer &lt; ActiveRecord::Base has_many :reservations has_many :tables, :through =&gt; :reservations end class Table &lt; ActiveRecord::Base has_many :reservations has_many :customers, :through =&gt; :reservations end </code></pre> <p>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:</p> <pre><code>&lt;%= button_to "Book table #{table.id}" , customers_path(:table_id =&gt; table) %&gt; </code></pre> <p>In the <code>CustomerController</code>, I edited the create method like the following:</p> <pre><code>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 </code></pre> <p>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.</p> <p>Why is the ID not available in the <code>create</code> method?</p> <p>EDIT:</p> <p>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.</p> <pre><code>&lt;%= form_tag mainpages_path, :method =&gt; 'get' do %&gt; &lt;p&gt; How many persons: &lt;br/&gt;&lt;%= number_field_tag(:search) %&gt; &lt;br/&gt; Date: &lt;%= date_select :date, 'Date', use_short_month: true, order: [:day, :month, :year] %&gt; &lt;br/&gt; Beginn: &lt;%= time_select :Beginn, 'Beginn' , default:Time.now %&gt; &lt;br/&gt; End : &lt;%= time_select :Ende, 'Ende' , default: Time.now + 2.hours %&gt; &lt;br/&gt; &lt;%params[:search]%&gt; &lt;%= submit_tag "Search" %&gt; &lt;/p&gt; &lt;% end %&gt; &lt;% if @tables %&gt; &lt;h2&gt;Folgende Tische stehen zur Auswahl&lt;/h1&gt; &lt;% @tables.each do |table| %&gt; &lt;div class="entry" &gt; &lt;h3&gt;Seats: &lt;%= table.seats %&gt;&lt;/h3&gt; &lt;h3&gt;Area: &lt;%= table.area %&gt;&lt;/h3&gt; &lt;%= link_to "Book this table #{table.id}" , new_customer_path(:table_id =&gt; table) %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;% else %&gt; &lt;p&gt; Pls choose a table&lt;/p&gt; &lt;% end %&gt; </code></pre> <p>EDIT2: The link to HTML-Code:</p> <pre><code>&lt;a href="/customers/new?table_id=1" mode="post"&gt;Book this table 1&lt;/a&gt; </code></pre> <p>And here is the HTML-Code of the customer/new-view:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Restaurant&lt;/title&gt; &lt;link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" /&gt; &lt;link href="/assets/customers.css?body=1" media="all" rel="stylesheet" type="text/css" /&gt; &lt;link href="/assets/mainpages.css?body=1" media="all" rel="stylesheet" type="text/css" /&gt; &lt;link href="/assets/reservations.css?body=1" media="all" rel="stylesheet" type="text/css" /&gt; &lt;link href="/assets/scaffolds.css?body=1" media="all" rel="stylesheet" type="text/css" /&gt; &lt;link href="/assets/tables.css?body=1" media="all" rel="stylesheet" type="text/css" /&gt; &lt;script src="/assets/jquery.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/jquery_ujs.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/customers.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/mainpages.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/reservations.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/tables.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/application.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;meta content="authenticity_token" name="csrf-param" /&gt; &lt;meta content="ej1MgV2fad014SLkCv3dZXl8TknQH4JHZLoe56Xn/Kk=" name="csrf-token" /&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;New customer&lt;/h1&gt; &lt;form accept-charset="UTF-8" action="/customers" class="new_customer" id="new_customer" method="post"&gt;&lt;div style="margin:0;padding:0;display:inline"&gt;&lt;input name="utf8" type="hidden" value="&amp;#x2713;" /&gt;&lt;input name="authenticity_token" type="hidden" value="ej1MgV2fad014SLkCv3dZXl8TknQH4JHZLoe56Xn/Kk=" /&gt;&lt;/div&gt; &lt;div class="field"&gt; &lt;label for="customer_name"&gt;Name&lt;/label&gt;&lt;br /&gt; &lt;input id="customer_name" name="customer[name]" size="30" type="text" /&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;label for="customer_email"&gt;Email&lt;/label&gt;&lt;br /&gt; &lt;input id="customer_email" name="customer[email]" size="30" type="text" /&gt; &lt;/div&gt; &lt;input id="table_id" name="table_id" type="hidden" /&gt; &lt;div class="actions"&gt; &lt;input name="commit" type="submit" value="Create Customer" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;a href="/customers"&gt;Back&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload