I'm working on a restaurant system in ruby and currently working on a ticket controller the way its set up now every time the user clicks add to ticket it creates a new ticket every time. I want it to so when the user clicks add to ticket it checks if a ticket exists in the database and if it doesn't it creates a new one and if it exists it adds on to the same ticket. I'm not quite sure how to approach it.
class TicketController < ApplicationController
def addToTicket
session[:tableID] = "15"
unless defined? check
check = Ticket.create(
table: session[:tableID],
tax: "8.25",
tstatus: 0
)
session[:ticket] = check
puts("**********Ticket created************")
redirect_to guest_path
else
check.orderItems.create(
item: (MenuItem.find_by id: params[:item_id]),
ingredients: params[:good_ingredients],
notes: params[:notes],
istatus: 0
)
session[:ticket] = check
puts("**************Ticket added to***********")
redirect_to guest_path
end
end
You'd do this with validations in your model:
#app/models/user.rb
class User < ActiveRecord::Base
has_many :tickets
end
#app/models/ticket.rb
class Ticket < ActiveRecord::Base
belongs_to :user
belongs_to :table
validates :table, uniqueness: { scope: :user } # You didn't provide much context so this is quite generic
end
#app/models/table.rb
class Table < ActiveRecord::Base
has_many :tickets
has_many :users, through: :tickets
end
This allows you to use the following:
#config/routes.rb
resources :tables do
resources :tickets
end
#app/controllers/tickets_controller.rb
class TicketsController < ApplicationController
def new
@table = Table.find params[:table_id]
@ticket = current_user.tickets.new(table: @table)
end
def create
@table = Table.find params[:table_id]
@ticket = current_user.tickets.new ticket_params
@ticket.save
end
private
def ticket_params
params.require(:ticket).permit(:tax, :tstatus, :table_id).merge(table_id: @table.id)
end
end
It's not a good idea to output in your controller ( puts
)... unless you like checking your logs a lot.
I think you're trying to invoke the flash
method:
def create
flash[:success] = "Ticket Created"
end
--
You also need to make sure you're calling the correct actions from your views...
You're currently using addToticket
(which should be in snake_case
) - the equivalent to update
. Instead of what you have, I would recommend using new/create
and update
actions independently:
#app/controllers/tables_controller.rb
class TablesController < ApplicationController
def show
@table = Table.find params[:id]
@ticket = current_user.tickets.find_by(table_id: params[:id]) || current_user.tickets.new(table: @table)
end
end
#app/controllers/tickets_controller.rb
class TicketsController < ApplicationController
def create
@table = Table.find params[:id]
@ticket = current_user.tickets.new ticket_params
redirect_to @table if @ticket.save
end
def update
@table = Table.find params[:id]
@ticket = current_user.tickets.new ticket_params
## more logic here
redirect_to @table if @ticket.save
end
private
def ticket_params
params.require(:ticket).permit(:tstatus, :etc, :etc).merge(table_id: @table.id)
end
end
#app/views/tables/show.html.erb
<%= form_for [@table, @ticket] do |f| %>
<%= f.text_field :tstatus %>
....
<%= f.submit %>
<% end %>
The above will invoke a form and populate it with the appropriate fields according to the Table
model. The really cool thing is that if you set it so that if no ticket exists, a "new" instance of the model will be invoked, sending the submission to the create
action, else update
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.