如果票证存在ruby,如何检查数据库?

时间:2021-09-06 04:26:32

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.

我正在使用ruby中的餐馆系统,目前正在设置票据控制器,就像每次用户点击添加到票证时一样,它每次都会创建新票证。我想要它,所以当用户单击添加到票证时,它会检查数据库中是否存在票证,如果不存在,则会创建一个新票证,如果存在,则会添加到同一票证。我不太清楚如何处理它。

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

1 个解决方案

#1


0  

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

Notes

It's not a good idea to output in your controller (puts)... unless you like checking your logs a lot.

在控制器(put)中输出不是一个好主意......除非你喜欢检查你的日志。

I think you're trying to invoke the flash method:

我想你正在尝试调用flash方法:

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:

您当前正在使用addToticket(应该是snake_case) - 相当于更新。而不是你拥有的,我建议独立使用新的/创建和更新操作:

#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.

上面将调用一个表单并根据Table模型用适当的字段填充它。非常酷的是,如果你设置它,如果不存在票证,将调用模型的“新”实例,将提交发送到创建操作,否则更新。

#1


0  

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

Notes

It's not a good idea to output in your controller (puts)... unless you like checking your logs a lot.

在控制器(put)中输出不是一个好主意......除非你喜欢检查你的日志。

I think you're trying to invoke the flash method:

我想你正在尝试调用flash方法:

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:

您当前正在使用addToticket(应该是snake_case) - 相当于更新。而不是你拥有的,我建议独立使用新的/创建和更新操作:

#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.

上面将调用一个表单并根据Table模型用适当的字段填充它。非常酷的是,如果你设置它,如果不存在票证,将调用模型的“新”实例,将提交发送到创建操作,否则更新。