【问题标题】:Trying to create an index for bookings in a hour training尝试在一小时培训中为预订创建索引
【发布时间】:2017-08-01 04:50:45
【问题描述】:

我正在制作一个应用程序,用户可以在其中预订一小时的培训。我想让用户选择查看谁在培训中预订(小时),我在培训中进行索引预订,这是我的代码:

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

  def new
    @booking = Booking.new
    @training = Training.find(params[:training_id])
    @booking.training_id
  end

  def create
    @booking = @training.bookings.build(booking_params)
    @booking.user = current_user

    if @booking.save
      flash[:success] = "Book created"
      redirect_to trainings_path
    else
      render 'new'
    end
  end


  def index
    @bookings = Booking.all
  end


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



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

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

end

预订模式:

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 :trainings do
    resources :bookings
  end
  resources :users
end

当我参加培训表演(特定时间的培训)时,代码如下:

<div class="row">
    <section>
      <h1>
HOUR: <%= @training.hour %>
      </h1>
    </section>
    <section>
      <h1>
SLOTS: <%= @training.slots %>
      </h1>
    </section>
    <center>
    <%= render 'bookings/booking_form' if logged_in? %>
    <%= render 'bookings/index_bookings' if logged_in? %>
  </center>

_index_bookings.html.erb 是:

<ul class="bookings">
<% if current_user.bookings(@training) %>
  <li>
<%= link_to @training_id, training_bookings_path %>
</li>
<% end %>
</ul>

应用程序给了我错误:

显示 /home/cesar/Apps/boxApp/app/views/bookings/_index_bookings.html.erb 提出第 4 行的位置:

没有路线匹配 {:action=>"index", :controller=>"bookings", :id=>"7"} 缺少必需的键:[:training_id]

我想知道为什么它不采用 training_id,如果它采用的是 7 类的 id。以及如何解决它。

【问题讨论】:

  • 尝试添加更改为:training_bookings_path(@training)
  • 谢谢,它成功了。上帝保佑你
  • 很高兴为您提供帮助!我添加它作为答案,现在我们知道它有效。如果您有机会接受答案,它将帮助其他有同样问题的用户。谢谢!

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


【解决方案1】:

使用嵌套资源url时,应将父资源作为第一个参数传递,如下所示:

training_bookings_path(@training)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2019-09-28
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多