无法使用表单助手生成表单

时间:2013-12-12 13:09:52

标签: ruby ruby-on-rails-4

我试图生成一个带有html.erb文件的表单,看起来像这样:

<form class="edit-booking" method="post" action="/users/<%= current_user.id %>/accounts/1/bookings/new" data-remote="true">
    <header><h1>Edit booking</h1></header>
    <input class="booking-name" name="booking[name]" placeholder="Name of the user" type="text" />
    <fieldset class="booking-list">
        <h2>Booking details</h2>
        <ul>
            <li>
                <input name="booking[name]" type="text" value="Unit name" disabled/>
                <input name="booking[check_in]" type="date" value="" />
                <input name="booking[check_out]" type="date" value="" />
            </li>
        </ul>
    </fieldset>     
    <input name="submit" type="submit" value="Save" />
</form>

我有这个红宝石代码:

<%= form_for [current_user] do |f| %>

    <header>
        <h1>Edit booking</h1>
    </header>

    <%= field_set_tag 'Booking details' do %>
        <%= f.fields_for :bookings do |booking_form| %>
            <%= booking_form.text_field :name,     placeholder: "Name" %>
            <%= booking_form.text_field :check_in,   placeholder: "Check-in" %>
            <%= booking_form.text_field :check_out,    placeholder: "Check-out" %>
        <% end %>       
    <% end %>
    <%= f.submit "Save" %> 
<% end %>

但是,即使在chrome dev工具中它显示了一个表单标签,它也不会在浏览器上呈现任何内容。

编辑:

生成的html如下:

<form accept-charset="UTF-8" action="/users/1/accounts/1" class="edit_account" id="edit_account_1" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="patch"><input name="authenticity_token" type="hidden" value="fzA+hkk3o+itJOI0UDj9F01DuI+A5+zgQ1EVGlwrPww="></div>

    <header>
        <h1>Edit booking</h1>
    </header>

    <fieldset><legend>Booking details</legend>



            <input id="account_bookings_name" name="account[bookings][name]" placeholder="Name" type="text">
            <input id="account_bookings_check_in" name="account[bookings][check_in]" placeholder="Check-in" type="text">
            <input id="account_bookings_check_out" name="account[bookings][check_out]" placeholder="Check-out" type="text">

</fieldset>
    <input name="commit" type="submit" value="Save"> 
</form>

我的帐户模型:

class Account < ActiveRecord::Base

  belongs_to :user

  has_many :products
  has_many :bookings
  accepts_nested_attributes_for :bookings
  accepts_nested_attributes_for :products

end

1 个答案:

答案 0 :(得分:0)

在您的模型中添加:

#Account model

accepts_nested_attributes_for :bookings

另外,您的current_user应该是帐户吗?