控制器#creal

时间:2017-03-09 03:29:43

标签: ruby-on-rails model-view-controller restful-architecture

我试图在用户预订培训时间时制作应用。 当我尝试创建这本书时,我收到了以下方法:

  

Actionings:在BookingsController中创建的ParameterMissing #create

     

param缺失或值为空:预订

这是我的预订控制器:

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

  def new
    @booking = Booking.new
  end

  def create
    @booking = @training.bookings.build(booking_params)
    @booking.user = current_user
    if @booking.save
      flash[:success] = "Book created"
      redirect_to training_index_url
    else
      render 'new'
    end
  end


  def index
    @booking = Booking.all
  end


  def destroy
    @booking = Booking.find(params[:id])
    @booking.destroy
    flash[:success] = "Book deleted"
    redirect_to training_index_url
  end



private

  def booking_params
    params.require(:booking).permit(:user_id, :training_id)
  end

  def load_training
    @training = Training.find_by(params[:training_id])
  end
end

我不知道为什么它不会将user_id参数带入预订,我注意到因为我确实在创建方法中放了@user而结果是nil。当我用户做到这一点时,我得到了一个肯定的答案(身份证,小时,老虎机等)

这是我的预订模式:

class Booking < ApplicationRecord
  belongs_to :user
  belongs_to :training
  default_scope -> { order(created_at: :desc) }
  validates :user_id, presence: true
  validates :training_id, presence: true



end

我的路线rb:

Rails.application.routes.draw do

  root 'static_pages#home'
  get    '/signup',               to: 'users#new'
  get    '/contact',              to: 'static_pages#contact'
  get    '/about',                to: 'static_pages#about'
  get    '/login',                to: 'sessions#new'
  post   '/login',                to: 'sessions#create'
  delete '/logout',               to: 'sessions#destroy'
  get    '/book',                 to: 'bookings#new'
  post   '/book',                 to: 'bookings#create'
  delete '/unbook',               to: 'bookings#destroy'


  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]
  resources :bookings,            only: [:create, :destroy]
  resources :training
  resources :users
end

新预订视图:

<h1>Booking confirmation</h1>

<%= form_for (@booking) do |f| %>
  <%= f.submit "Book", class: "btn btn-primary" %>
<% end %>

我想现在为什么预订方法不会检索user_id参数。

这就是我在帖子中看到的内容:

Started POST "/bookings" for 127.0.0.1 at 2017-03-09 00:31:53 -0400
Processing by BookingsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"EUoo8M66O2XlRgeMoEsSqwH2i/EKKFzMMRsY9UilzcU+T+5n6/Gjx0abxlZ7nJlqgr5wsALtXW/UMf2Q01pNUg==", "commit"=>"Reservar"}
  Training Load (0.1ms)  SELECT  "trainings".* FROM "trainings" LIMIT ?  [["LIMIT", 1]]
Completed 400 Bad Request in 2ms (ActiveRecord: 0.1ms)



ActionController::ParameterMissing (param is missing or the value is empty: booking):

app/controllers/bookings_controller.rb:36:in `booking_params'
app/controllers/bookings_controller.rb:9:in `create'
  Rendering /home/cesar/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb wi

由于

1 个答案:

答案 0 :(得分:0)

您的代码问题是您没有从预订视图中发送user_idtraining_id

您应该有user_id可以是隐藏字段,training_id用于用户选择的培训。

<%= f.hidden_field :user_id, value: current_user.id %>

如果用户可以一次预订多个培训,您可以获得具有多选选项的培训列表。

用户只能在一次交易中预订一次培训,每次培训的复选框都应该可以解决问题。

<ul>
  <% @trainings.each do |training| %>
      <li>
        <%= check_box_tag 'training_ids[]', training.id -%>
        <%= h training.name -%>
      </li>
  <% end %>
</ul>

当用户提交此内容时,您将获得user_id以及training_ids training_ids = ["1", "2", "3"]