我很抱歉这是一个如此简单的问题,但我一直在努力解决这个问题。
我有两个相关的模型 - Tour&保留。 "游"有一个"天"属性。我想列出选择标记中的日期,供用户在我的预约"中使用#34;查看
我认为这可能有用:
(预约控制器)@tour_days = Tour.where(:days => params [:days])
(预订#new)= f.select:days,@ tours_days
但是,我收到错误undefined method
天'
`
class Reservation < ActiveRecord::Base
belongs_to :tour
end
class Tour < ActiveRecord::Base
has_many :reservations
end
class ReservationsController < ApplicationController
def index
end
def new
@reservation = Reservation.new
@tour = Tour.find(params[:tour_id])
@tour_days = Tour.where(:days => params[:days])
end
def create
@tour = Tour.find(params[:tour_id])
if @reservation.update_attribute(:t_shirt, params[:t_shirt]) == true || @reservation.update_attribute(:hat, params[:hat]) == true
@tour.amount = @tour.amount + 15
else
@tour.amount = @tour.amount
end
@reservation = Reservation.new(reservation_params)
if @reservation.save
Stripe.api_key = ENV["STRIPE_SECRET_KEY"]
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(:passengers, :t_shirt, :hat)
end
end
class ToursController < ApplicationController
def index
@tours = Tour.all
end
def new
@tour = Tour.new
end
def create
@tour = Tour.new(tours_params)
if @tour.save
flash[:success] = "Tour #{@tour.name} has been successfully added."
redirect_to new_tour_path
else
flash[:error] = "The tour #{@tour.name} was not successfully saved. Please try again"
render 'new'
end
end
def show
@tour = Tour.find_by(id: params[:id])
@reservation = Reservation.new
end
def edit
@tour = Tour.find_by(id: params[:id])
end
def update
@tour = Tour.find_by(id: params[:id])
if @tour.update_attributes(tours_params)
flash[:success] = "#{@tour.name} has been successfully updated."
redirect_to tours_path
else
flash[:error] = "#{@tour.name} has not been updated. Please try again."
render 'edit'
end
end
def delete
@tour = Tour.find_by(id: params[:id])
end
def destroy
@tour = Tour.find_by(id: params[:id])
if @tour.destroy
flash[:success] = "The #{@tour.name} has been successfully deleted."
redirect_to tours_path
else
flash[:error] = "The #{@tour.name} has not been deleted. Please try again."
render 'edit'
end
end
private
def tours_params
params.require(:tour).permit(:name, :amount, :days)
end
end
。
= 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.select :days, @tours_days
%fieldset.credit_card
%span.payment-errors
.control-group
= label_tag :card_number, 'Credit card number:', class: 'control-label'
.controls
= text_field_tag :card_number, nil, name: nil, class: 'span3', data: {stripe: 'number'}
.control-group
= label_tag :security_code, 'Security code:', class: 'control-label'
.controls
= text_field_tag :security_code, nil, name: nil, class: 'span3', data: {stripe: 'cvc'}
.control-group
= label_tag :exp_date, 'Expiration:', class: 'control-label'
.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'})
%fieldset.actions.control-group
.controls
= f.submit 'Sign up'
答案 0 :(得分:2)
考虑使用accepts_nested_attributes_for
创建另一个模型来封装days
。然后将其与Reservation
模型相关联。
class Reservation < ActiveRecord::Base
belongs_to :tour
has_and_belongs_to_many :days
accepts_nested_attributes_for :days, allow_destroy: true
end
class Day < ActiveRecord::Base
has_and_belongs_to_many :reservations
end
Day
模型将有一个属性:name
,其中包含names of the seven days
class ReservationsController < ApplicationController
def create
@reservation = Reservation.new(reservation_params)
if @reservation.save
redirect_to @save
else
render :new
end
end
private
#add the `days_attributes` to the `reservations_params`
def reservation_params
params.require(:reservation).permit(:passengers, :t_shirt, :hat, days_attributes[:id, name])
end
end
然后在new.html.erb
创建预订时,您可以选择下拉菜单以选择特定日期。你可以这样做:
f.select :days
如果您选择使用nested_forms
,则必须使用boostrap_nested_form_for
作为documentation建议。