0

Users can make a reservation, and each reservation can have multiple occurrences. After they've entered their data, they get a preview page to confirm the information before it's saved to the database. The occurrence information is being passed to the preview page. I can see it in the parameter dump at the bottom of the page. I can't seem to access it to display it to the user.

Here's my code:

reservations_controller.rb

def new
  @reservation = Reservation.new
  @occurrence  = @reservation.occurrence.build
end

def preview
  if params[:reservation]
    @reservation = Reservation.new(reservation_params)
    @occurrence = @reservation.occurrence.build(params[:occurrence])
  end
end

private
  def reservation_params
    params.require(:reservation).permit(:title, :description, :is_public, :is_internal, :internal_organization, :contact_name,
      :contact_phone, :contact_email, :contact_present, :contact_present_name,
      :contact_present_phone, :contact_present_email, :notes)
  end

reservations/new.html.erb (I've put some blank lines around the nested form to make it easier to find):

<div id="addReservationForm_container" class="addReservationFormContainer formContainer">
  <%= form_for @reservation, :url => preview_path, :html => { :method => :get }  do |f| %>
    <% if @reservation.errors.any? %>
      <div class="error">
        Please address the <%= "#{'error'.pluralize(@reservation.errors.count)}" %> below to submit this reservation.
      </div>
        <% end %>
    <%= field_set_tag 'Reservation Information' do %>
      <div class="formField paragraph <%= 'error' if @reservation.errors.include?(:title) %>">
        <%= f.label :title, 'Title:' %>
        <%= f.text_field :title, size: "40", class: (@reservation.errors.include?(:title) ? 'error' : '') %>
      </div>
      <div class="formField">
        <%= f.label :description, 'Enter a short description of the event:' %>
        <%= f.text_area :description, size: "60x6" %>
      </div>
      <div class="formField">
        <%= f.label :is_public do %>
          Should this event be listed on public calendars?
          <%= f.select :is_public, options_for_select([['Yes', true], ['No', false]]) %>
        <% end %>
      </div>
      <hr />
      <div class="formField">
        <%= f.label :is_internal do %>
          Is this reservation for a related event?
          <%= f.select :is_internal, options_for_select([['Yes', true], ['No', false]]) %>
        <% end %>
      </div>
      <div class="internalDetails formField <%= 'error' if @reservation.errors.include?(:internal_organization) %>">
        <%= f.label :internal_organization, 'Please enter the name of the department, organization, or club hosting the event:' %>
        <%= f.text_field :internal_organization, size: '40', class: (@reservation.errors.include?(:internal_organization) ? 'error' : '') %>
      </div>
    <% end %>


    <%= field_set_tag 'Date and Time' do %>
      <div class="paragraph">
        Select the date(s) and time(s) you want to reserve:
        <div class="formField paragraph">
          <%= f.fields_for :occurrences do |o| %>
            <%= o.text_field :start_date, class: 'datepicker' %>
            <%= o.select :end_date, options_for_select(create_hours(:start_time => 7.5.hours, :end_time => 21.hours, :increment => 15.minutes)) %>
          <% end %>
        </div>
      </div>
    <% end %>


    <%= field_set_tag 'Contact Information' do %>
      <div class="paragraph">
        Please enter the contact information of the person responsible for the reservation:
      </div>
      <div class="formField paragraph <%= 'error' if @reservation.errors.include?(:contact_name) %>">
        <%= f.label :contact_name, 'Name:' %>
        <%= f.text_field :contact_name, size: "40", class: (@reservation.errors.include?(:contact_name) ? 'error' : '') %>
      </div>
      <div class="formField paragraph <%= 'error' if @reservation.errors.include?(:contact_phone) %>">
        <%= f.label :contact_phone, 'Phone:' %>
        <%= f.text_field :contact_phone, size: "40", class: (@reservation.errors.include?(:contact_phone) ? 'error' : '') %>
      </div>
      <div class="formField paragraph <%= 'error' if @reservation.errors.include?(:contact_email) %>">
        <%= f.label :contact_email, 'E-mail:' %>
        <%= f.text_field :contact_email, size: "40", class: (@reservation.errors.include?(:contact_email) ? 'error' : '') %>
        </div>
      </div>
    <% end %>
    <%= field_set_tag 'Additional Information' do %>
      <div class="internalDetails formField">
        <%= f.label :notes, 'Please enter any additional information or instructions:' %>
        <%= f.text_area :notes, size: "60x6" %>
      </div>
    <% end %>
    <%= f.submit 'Continue' %>
  <% end %>
</div>

The code below is where I can't seem to display the information from the occurrences nested form.

reservations/preview.html.erb

<div class="paragraph">
  Please look over the information you entered for your reservation, and then click either
  the "Submit Reservation" button to submit your request, or the "Make Changes" button to
  return to the previous form and edit the request.
</div>

<div style="margin-left: 3em;">
  <div class="paragraph">
    <div> <%= @reservation.title %></div>
    <div> <%= @occurrence.start_date %></div>
    <div> <%= @reservation.contact_name %> (<%= @reservation.contact_email %>)</div>
    <div> <%=@reservation.contact_phone %></div>
  </div>
</div>

reservation.rb

has_many :occurrence
accepts_nested_attributes_for :occurrence, :reject_if => :all_blank

I'm new to Rails, so it wouldn't surprise me to find I have multiple things wrong here.

  • Can you verify via `byebug` whether `occurrence` was actually built? You should build `occurrence` like so `@occurrence = @reservation.occurrence.build(params[:reservation][:occurrence])` as it was submitted nested within `:reservation`. – Fan Jin Aug 12 '15 at 23:30
  • Glad you were able to locate the `params`, what about `@occurrence = @reservation.occurrence.build(params[:reservation][:occurrence_attributes])` (updated for your comment)? – Fan Jin Aug 13 '15 at 21:08
  • occurrence isn't being built correctly even after changing the build line. I'm dumping the params on that page, and params[:reservation][:occurrence_attributes] contains the data. I can't use @occurrence = @reservation.occurrence.build(params[:reservation][:occurrence_attributes]). I get Forbidden Attributes Error where I build occurrence. If I change params.permit to use "occurrence_attributes" I get "argument out of range" on this line where I create reservation). I'm tempted to just reference params[:reservation][:occurrence_attributes] on the preview page. – NotThatDavidCross Aug 13 '15 at 21:12
  • Sorry, I was submitted the comment early and was editing while you responded. – NotThatDavidCross Aug 13 '15 at 21:13
  • If you're using `params.permit` to whitelist the `occurence_attributes`, you'll need to go a bit further in whitelisting all the attributes for the nested object, as described here: http://stackoverflow.com/questions/18436741/rails-4-strong-parameters-nested-objects – Fan Jin Aug 13 '15 at 21:17
  • I'm currently using 'occurrence_attributes: [:start_date, :end_date]'. I didn't type it out earlier because I ran into the character limit. – NotThatDavidCross Aug 13 '15 at 21:21
  • The occurrence_attributes param looks like this: `"occurrence_attributes"=>{"0"=>{"start_date"=>"8/14/2015 (Friday)", "end_date"=>" 7:30 AM"}}`. Lookin at the link you sent, it appears I should be doing something like this `{occurrence_attributes: [:start_date, :end_date]}` in my permit, but that isn't working. – NotThatDavidCross Aug 13 '15 at 21:26

0 Answers0