param丢失或值为空:

时间:2015-04-24 09:34:04

标签: ruby-on-rails-4

这是预约控制器:

class BookingsController < ApplicationController
    before_action :logged_in_user, only: [:create]

    def new
        @booking = Booking.new
    end

    def index
        @bookings = Booking.all
    end

    def create
        @booking = current_user.bookings.build(booking_params)
        if @booking.save
            flash[:success] = "Booking created!"
            redirect_to root_url
        else
          render 'static_pages/home'
        end
    end


    def show
        @booking = Booking.find(params[:id])
    end

    def destroy
        @booking = Booking.find(params[:id])
        @booking.destroy
        redirect_to bookings_path
    end

    private
        def booking_params
            params.require(:booking).permit(:date, :hour, :game)
        end   
end

当我在utf8和真实性令牌后发帖时,我不明白为什么&#34;#&lt; \ Booking:0x000001061928a8&gt; \&#34;而不仅仅是&#34;预订&#34;然后嵌套的哈希包含我在控制器中允许的日期,小时和游戏。

以下是观点:

<%= form_for(@booking) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

    <%= f.label :day %>
    <%= date_field(@booking, :date) %>

    <%= f.label :hour %>
    <%= number_field(@booking, :hour, in: 8..19) %>

    <%= f.label :game %>
    <%= select(@booking, :game, [['Singles', 1], ['Doubles', 2]]) %>

  <%= f.submit "Book", class: "btn btn-primary" %>    
<% end %>

任何帮助都非常感激。

1 个答案:

答案 0 :(得分:1)

好吧,我只是没有使用表单构建器的f变量来选择框,最初我是,但我在渲染选择框时遇到了问题,所以我把它拿出来了。一定是由于语法错误。 这是带有日期选择器和其他选择框的工作form_for:

<div class="field">
<%= form_for(@booking) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

    <%= f.label :day %>
    <%= f.date_field :day %>

    <%= f.label :hour %>
    <%= f.number_field :hour, in: 8..19 %>

    <%= f.label :game %>
    <%= f.select :game, [['Singles', 1], ['Doubles', 2]] %>

  <%= f.submit "Book", class: "btn btn-primary" %>    
<% end %>

</div>