用户"管理员"创建活动。因此,每个事件都有自己的URL,例如" events / 1"。在公共站点上(即,不需要身份验证)。公众可以购买活动门票。管理员所要做的就是发布公共网址,即" event / 1"并为票务买家提供表格填写
我无法让ticket_buyers与他们各自的活动(即活动/ 1)相关联。以下是在命令提示符下运行Event.first.ticket_buyers的结果,该命令提示符在数组中没有显示任何内容,即使我使用应用程序表单(而不是命令api)使用ticket_buyer数据填充了第一个事件:
irb(main):001:0> Event.first.ticket_buyers
Event Load (0.0ms) SELECT "events".* FROM "events" ORDER BY "events"."id" ASC LIMIT 1
TicketBuyer Load (1.0ms) SELECT "ticket_buyers".* FROM "ticket_buyers" WHERE "ticket_buyers"."event_id" = ? [["event_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy []>
活动模式
class Event < ActiveRecord::Base
belongs_to :admin
has_many :ticket_buyers, dependent: :destroy
end
TicketBuyer模型
class TicketBuyer < ActiveRecord::Base
belongs_to :event
end
事件控制器
class EventsController < ApplicationController
before_filter :authenticate_admin!, :except => :show
def index
@events = current_admin.events
end
def show
@event = Event.find(params[:id])
@ticket_buyers = TicketBuyer.new
end
def new
@event = current_admin.events.build
@event.admin = current_admin
end
def create
@event = current_admin.events.build(event_params)
if @event.save
redirect_to @event
else
render 'new'
end
end
private
def event_params
params.require(:event).permit(:name_of_event, ticket_buyer_attributes: [:full_name, :number_of_tickets])
end
end
门票买家控制器
class TicketBuyersController < ApplicationController
before_action :authenticate_admin!, :except => [:new, :create]
def index
@ticket_buyer = TicketBuyer.all
end
def show
@ticket_buyer = TicketBuyer.find(params[:id])
@event = Event.find(params[:event_id])
end
def new
@ticket_buyer = TicketBuyer.new
end
def create
@ticket_buyer = TicketBuyer.new(ticket_buyer_params)
if @ticket_buyer.save
redirect_to localhost:3000
else
render('new')
end
end
private
def set_ticket_buyer
@ticket_buyer = Ticket_buyer.find(params[:id])
end
def ticket_buyer_params
params.require(:ticket_buyer).permit(:full_name, :number_of_tickets)
end
end
数据库架构
ActiveRecord::Schema.define(version: 20160705212350) do
create_table "admins", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "full_name"
end
add_index "admins", ["email"], name: "index_admins_on_email", unique: true
add_index "admins", ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true
create_table "events", force: :cascade do |t|
t.text "name_of_event"
t.integer "admin_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "events", ["admin_id"], name: "index_events_on_admin_id"
create_table "ticket_buyers", force: :cascade do |t|
t.text "full_name"
t.integer "event_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "ticket_buyers", ["event_id"], name: "index_ticket_buyers_on_event_id"
end
的routes.rb
Rails.application.routes.draw do
devise_for :admins
resources :events do
resources :ticket_buyers do
end
end
ticket_buyers / index.html.erb
<p id="notice"><%= notice %></p>
<table class='table table-striped table-hover'>
<thead>
<tr>
<th>Name</th>
<th>Number of tickets</th>
<th colspan="4"></th>
</tr>
</thead>
<tbody>
<% @ticket_buyer.each do |ticket_buyer| %>
<tr class="<%= cycle('list_line_odd', 'list_line_even') %>">
<tr>
<td><%= link_to ticket_buyer.full_name %></td>
<td><%= link_to ticket_buyer.number_of_tickets %></td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'Back', events_path %>
答案 0 :(得分:0)
在TicketBuyer创建操作中,您需要在创建事件后将记录与事件相关联:
class TicketBuyersController < ApplicationController
before_action :authenticate_admin!, :except => [:new, :create]
def create
@ticket_buyer = TicketBuyer.new(ticket_buyer_params)
if @ticket_buyer.save
# This line below links it to your event
Event.find(params[:event_id]).ticket_buyers << @ticket_buyer
redirect_to localhost:3000
else
render('new')
end
end
您还可以使用事件本身创建ticket_buyer,如下所示:
def create
@ticket_buyer = Event.find(params[:event_id]).ticket_buyers.new(ticket_buyer_params)
if @ticket_buyer.save
redirect_to localhost:3000
else
render('new')
end
这将在构建模型时自动将正确的event_id添加到模型中。另外,请确保已运行将event_id列添加到ticket_buyers表的迁移。
更新:
要在索引操作中显示正确的ticket_buyers,您需要加载该事件,然后使用您可以循环关闭该事件的ticket_buyers:
TicketBuyersController:
def index
@event = Event.find(params[:event_id])
end
索引视图:
<tbody>
<% @event.ticket_buyers.each do |ticket_buyer| %>
<tr class="<%= cycle('list_line_odd', 'list_line_even') %>">
<tr>
<td><%= link_to ticket_buyer.full_name %></td>
<td><%= link_to ticket_buyer.number_of_tickets %></td>
</tr>
<% end %>
答案 1 :(得分:0)
这是因为您没有在ticket_buyer记录中保存事件ID。我想知道为什么你也没有验证ticket_buyers的存在。
立即快速解决方法是将您的event_id添加到ticket_buyer_params
,如下所示:
def ticket_buyer_params
params.require(:ticket_buyer).permit(:full_name, :number_of_tickets, :event_id)
end