Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Accessing multiple models in multiple forms and showing result in select field

select views rails4 controller

  • Please log in to reply
1 reply to this topic

#1 RBG54

RBG54

    Passenger

  • Members
  • 3 posts

Posted 16 May 2014 - 11:32 PM

I have a Tour, Day, and Reservation model. In my Tour controller, I can create a tour and manually enter in days that the tour is available on. I then want to access each tours reservation page (tours/1/reservation/new) and display the tour information along with the tour designated days(in a select field).

The problem:
The issue with I have so far is that no select field is showing up in my reservations #new (confirmed the tour has days in console and pry). I am not receiving any errors - the form just doesn't show up. Any ideas? Thank you in advance!

I'm using:

    <% f.fields_for :day do |builder| %>
          <%= builder.label :id, 'Days available:' %><br />
          <%= builder.select :id, options_from_colection_for_select(@tour.days) %>
          <pre><%= options_from_collection_for_select(@tour.days) %>:<%= @tour.days.inspect %></pre>
        <% end %>

Controller:
class ReservationsController < ApplicationController


  def index
  end


  def new
    @reservation = Reservation.new
    @tour = Tour.includes(:days).find(params[:tour_id])
  end


  def create
    @tour = Tour.find(params[:tour_id])
    @reservation = Reservation.new(reservation_params)


    @tour.amount = @reservation.passengers * @tour.amount


    if @reservation.option1 && @reservation.option2
       @tour.amount = @tour.amount + 3000
    elsif @reservation.option1
       @tour.amount = @tour.amount + 1500
    elsif @reservation.option2
       @tour.amount = @tour.amount + 1500
    end


    if @reservation.save
      Stripe::Charge.create(
        :amount => @tour.amount, # amount in cents, again
        :currency => "usd",
        :card => params[:stripeToken]
      )
      flash[:success] = "Your reservation has been booked for #{@reservation.passengers} person(s). Please save this info."
      redirect_to new_tour_reservation_path(@tour)
    else
      render 'new'
    end
  end


  private


  def reservation_params
    params.require(:reservation).permit(:date, :passengers, day_attributes: [:id, :name, :_destroy])
  end
end

Reservations view #new:
<%= javascript_include_tag "https://js.stripe.com/v2/" %>
<%= javascript_include_tag 'payment' %>
<div class="container">
  <div class="row">
    <div class="col-md-9">
      <h2>
        <%= @tour.name %>
      </h2>
    </div>
    <div class="col-md-3">
      <%= bootstrap_form_for([:tour, @reservation], html: { class: 'form-horizontal', id: 'payment-form'}) do |f| %>
        <%= f.alert_message 'Please fix the errors below:' %>
        <%= f.select :passengers, options_for_select( (1..10).map { |n| n %1 == 0 ? n.to_i : n } ) %>
        <% f.fields_for :day do |builder| %>
          <%= builder.label :id, 'Days available:' %><br />
          <%= builder.select :id, options_from_colection_for_select(@tour.days) %>
          <pre><%= options_from_collection_for_select(@tour.days) %>:<%= @tour.days.inspect %></pre>
        <% end %>
        <fieldset class="credit_card">
          <span class="payment-errors"></span>
        </fieldset>
        <div class="control-group">
          <%= label_tag :card_number, 'Credit card number:', class: 'control-label' %>
          <div class="controls">
            <%= text_field_tag :card_number, nil, name: nil, class: 'span3', data: {stripe: 'number'} %>
          </div>
        </div>
        <div class="control-group">
          <%= label_tag :security_code, 'Security code:', class: 'control-label' %>
          <div class="controls">
            <%= text_field_tag :security_code, nil, name: nil, class: 'span3', data: {stripe: 'cvc'} %>
          </div>
        </div>
        <div class="control-group">
          <%= label_tag :exp_date, 'Expiration:', class: 'control-label' %>
          <div class="controls">
            <%= select_month(Date.today, {add_month_numbers: true},  class: 'span2', data: {stripe: 'exp-month'}) %>
            <%= select_year(Date.today.year, {start_year: Date.today.year, end_year: Date.today.year + 4}, class: 'span1', data: {stripe: 'exp-year'}) %>
          </div>
        </div>
        <fieldset class="actions control-group">
          <div class="controls">
            <%= f.submit 'Sign up' %>
          </div>
        </fieldset>
      <% end %>
    </div>
  </div>
</div>

Models:
class Day < ActiveRecord::Base
  belongs_to :tour
  has_many :reservations, dependent: :destroy
  accepts_nested_attributes_for :reservations, allow_destroy: true, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end

class Reservation < ActiveRecord::Base
  belongs_to :tour
  belongs_to :day
  accepts_nested_attributes_for :day, allow_destroy: true, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end

class Tour < ActiveRecord::Base
  has_many :days, dependent: :destroy
  has_many :reservations
  accepts_nested_attributes_for :days, allow_destroy: true, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end


#2 RBG54

RBG54

    Passenger

  • Members
  • 3 posts

Posted 17 May 2014 - 03:23 AM

UPDATE:
The days of each tour are NOT being displayed on the reservations page IF they're in a form_helper or even something as simple as me applying a bootstrap dropdown-menu class to the surrounding element.

This works: (shows days)

<ul>
<% @tour.days.all.each do |day| %>
<li>
<%= day.name %>
</li>
<% end %>
</ul>

This DOES NOT:

<ul class='dropdown-menu'>
<% @tour.days.all.each do |day| %>
<li>
<%= day.name %>
</li>
<% end %>
</ul>

NOR this:

<% @tour.days.each do |day| %>
<%= f.fields_for :day do |builder| %>
<%= builder.label :id, 'Days available:' %><br />
<%= builder.select :id, options_from_collection_for_select(day, 'id', 'name') %>
<% end %>
<% end %>

I'm completely stumped.







Also tagged with one or more of these keywords: select, views, rails4, controller

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users