Koxzi
Koxzi

Reputation: 1051

Ruby on Rails Difficult Form/Associations

I have a Rails 4.2 app which has 'Rooms', 'Bookings' and 'Extras'.

When making a booking it is for a room e.g. website.com/rooms/1/bookings/1

I have extras which I want to be associated with the booking for that room via check-boxes.

How can this be implemented? I've been reading about has_many :foo, :through => :bar associations but I'm not sure if that's the way to go.

The relevant code looks like this:

<!-- app\views\bookings\_form.html.erb -->

<%= form_for([@room, @booking]) do |f| %>
  <p>
    <%= f.label 'Select Customer:' %>
    <%= f.collection_select :user_id, User.all, :id, :customer_name %>
  </p>
  <p>
    <%= f.label 'start_time', 'Start Date and Time:' %>
    <%= f.datetime_select :start_time, { minute_step: 15 } %>
  </p>
  <p>
    <%= f.label 'length', 'Length of booking in hours:' %>
    <%= f.number_field 'length', min: 1 %>
  </p>
  <p>
    <%= f.label 'Room Price:' %>
    <%= number_to_currency @room.price, unit: "£" %>
  </p>
  <p>
    <%= f.label 'Extras:' %>
    <%= f.collection_check_boxes :extra_ids, Extra.all, :id, :extra_info %>
  </p>
  <%= f.submit 'Submit' %>
<% end %>

# app\models\booking.rb
class Booking < ActiveRecord::Base
  belongs_to :room
  belongs_to :user
  has_many :additions
  has_many :extras, :through => :additions
end

# app\models\extra.rb
class Extra < ActiveRecord::Base
  belongs_to :extracat
  has_many :additions
  has_many :bookings, :through => :additions

  def extra_info
    "#{name}"
  end
end

# This model is for the has_many through testing I tried
# app\models\addition.rb
class Addition < ActiveRecord::Base
  belongs_to :booking
  belongs_to :extra
end

# Relevant section of schema
create_table "additions", force: :cascade do |t|
  t.integer  "booking_id"
  t.integer  "extra_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

create_table "bookings", force: :cascade do |t|
  t.datetime "start_time"
  t.datetime "end_time"
  t.integer  "length"
  t.integer  "room_id"
  t.integer  "user_id"
  t.integer  "extra_id"
end

EDIT - The section within the bookings show page.

# app\views\bookings\show.html.erb
<% @booking.extras.each do |e| %>
  <%= e.name %>,
<% end %>

EDIT - Adding bookings controller

class BookingsController < ApplicationController
  respond_to :html, :xml, :json

  before_action :find_room

  def index
    @bookings = Booking.where("room_id = ? AND end_time >= ?", @room.id, Time.now).order(:start_time)
    respond_with @bookings
  end

  def new
    @booking = Booking.new(room_id: @room.id)
  end

  def create
    @booking =  Booking.new(params[:booking].permit(:room_id, :start_time, :length))
    @booking.room = @room
    if @booking.save
      redirect_to room_bookings_path(@room, method: :get)
    else
      render 'new'
    end
  end

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

  def destroy
    @booking = Booking.find(params[:id]).destroy
    if @booking.destroy
      flash[:notice] = "Booking: #{@booking.start_time.strftime('%e %b %Y %H:%M%p')} to #{@booking.end_time.strftime('%e %b %Y %H:%M%p')} deleted"
      redirect_to room_bookings_path(@room)
    else
      render 'index'
    end
  end

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

  def update
    @booking = Booking.find(params[:id])
    # @booking.room = @room

    if @booking.update(params[:booking].permit(:room_id, :start_time, :length))
      flash[:notice] = 'Your booking was updated succesfully'

      if request.xhr?
        render json: {status: :success}.to_json
      else
        redirect_to resource_bookings_path(@room)
      end
    else
      render 'edit'
    end
  end

  private

  def save booking
    if @booking.save
        flash[:notice] = 'booking added'
        redirect_to room_booking_path(@room, @booking)
      else
        render 'new'
      end
  end

  def find_room
    if params[:room_id]
      @room = Room.find_by_id(params[:room_id])
    end
  end

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

end

How is it possible to associate the extras with a booking? As so far they are not being saved with the booking into the database. Is this a controller issue?

Upvotes: 0

Views: 67

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84114

You're not permitting the parameters correctly - the name is extra_ids. In addition since the parameter is an array you need to permit it like so:

params.require(:booking).permit(:room_id, :start_time, :length, :extra_ids => [])

Personally I recommend setting action controller to raise an error when unpermitted parameters are encountered in development or tests - very easy otherwise to miss the log messages

Upvotes: 2

Related Questions