【问题标题】:How to set enum from ticket controller when creating a new item创建新项目时如何从票证控制器设置枚举
【发布时间】:2016-02-12 17:16:21
【问题描述】:

我的目标是在加载页面时在票证上设置类别、状态和严重性(所有枚举都在票证模型上定义)。 正在加载的页面是 cable_new_1。 问题是提交表单时没有设置类别、状态或严重性。

这是完整的门票控制器

 class TicketsController < ApplicationController
  before_filter :authenticate
  before_action :set_ticket, only: [:show, :edit, :update, :destroy, :close]

  # GET /tickets
  # GET /tickets.json
  def index
    @tickets = @customer.tickets.opened
    @tickets_closed = @customer.tickets.closed
  end

  def closed
    @tickets = @customer.tickets.is_closed
  end


  # GET /tickets/1
  # GET /tickets/1.json
  def show
  end

  def step_1
  end

  def billing_new_1
    @ticket = Ticket.new(severity: 'low', category: 'billing', summary: 'I am having a billing issue')
    render 'new'
  end

  def billing_edit
  end

  def internet_step_1
  end

  def internet_step_2
  end

  def internet_step_3
  end

  def internet_new_1
    @ticket = Ticket.new(severity: 'medium', category: 'internet', summary: "My internet is down, but my tv is working")
    render 'new'
  end

  def internet_new_2
    @ticket = Ticket.new(severity: 'medium', category: 'internet_and_cable', summary: "My internet and TV are both not working")
    render 'new'
  end

  def internet_new_3
      @ticket = Ticket.new(severity: 'low', category: 'internet', summary: "My internet is working, but is having some problem")
      render 'new'
  end

  def cable_step_1
  end

  def cable_step_2
  end


  def cable_new_1
    @ticket = Ticket.new(severity: 'medium', category: 'cable', summary: "My cable is down, but my internet is working")
  render 'new'
  end

  def cable_new_2
    @ticket = Ticket.new(severity: 'medium', category: 'internet_and_cable', summary: "My cable and internet are both not working")
    render 'new'
  end

  def cable_new_3
    @ticket = Ticket.new(severity: 'low', category: "cable", summary: "My cable has an issue")
    render 'new'
  end

  # GET /tickets/1/edit
  def edit
  end

  # POST /tickets
  # POST /tickets.json
  def create
    @ticket = Ticket.new(ticket_params)
    @ticket.opened!
    @ticket.customer_id = @customer.id
    @ticket.neighborhood_id = @customer.neighborhood_id
    respond_to do |format|
      @ticket.save!
      format.html { redirect_to tickets_path, notice: 'Ticket was successfully created.' }
    end
  end

  # PATCH/PUT /tickets/1
  # PATCH/PUT /tickets/1.json
  def update
    respond_to do |format|
      if @ticket.update(ticket_params)
        format.html { redirect_to @ticket, notice: 'Ticket was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end

  # DELETE /tickets/1
  # DELETE /tickets/1.json
  def destroy
    @ticket.destroy
    respond_to do |format|
      format.html { redirect_to tickets_url, notice: 'Ticket was successfully deleted.' }
      format.json { head :no_content }
    end
  end

  def close
    @ticket.close!
    respond_to do |format|
      format.html { redirect_to tickets_path, notice: 'Ticket was successfully closed.' }
      format.json { head :no_content }
    end
  end


  private
  # Use callbacks to share common setup or constraints between actions.
  def set_ticket
    @ticket = Ticket.find(params[:id])
  end

  def set_neighborhood
    @neighborhood = @customer.neighborhood
  end




    # Never trust parameters from the scary internet, only allow the white list through.
    def ticket_params
      params.require(:ticket).permit(:neighborhood_id, :severity, :status, :category, :summary, :detail)
    end

end

这是票证模型:

class Ticket < ActiveRecord::Base
  belongs_to :customer
  belongs_to :tech
  belongs_to :neighborhood
  has_many :notes, as: :noteable
  has_many :appointments, as: :appointmentable


  scope :is_not_closed, ->            { where.not(status = closed)}
  scope :is_not_archived, ->          { where.not(status = archived)}
  scope :is_not_closed_or_archived, ->{ is_not_closed.is_not_archived}



  scope :hampton_park, ->             { where(neighborhood: Neighborhood.find(1))}
  scope :north_hampton, ->            { where(neighborhood: Neighborhood.find(2))}

  scope :today, ->                    { where(created_at: Date.today)}
  scope :one_week_ago, ->             { where('created_at >= ?', 1.week.ago)}
  scope :two_weeks_ago, ->            { where('created_at >= ?', 2.week.ago)}
  scope :four_weeks_ago, ->           { where('created_at >= ?', 4.week.ago)}

  # before_create :open!
  before_destroy :archived!
  # after_save :status_based_communications!


  # validates :customer_id, presence: true
  # validates_presence_of :summary

  enum status: [ :opened, :reopened, :customer_updated, :needs_to_be_scheduled, :scheduled, :in_progress, :waiting_on_customer, :closed, :archived]
  enum category: [ :billing, :plant, :cable, :internet, :internet_and_cable]
  enum severity: [ :low, :medium, :high]

  def status_based_communications!
    if self.opened?
      CustomerMailer.opened_ticket_email(@customer).deliver_now
      self.note.create(authorable_id: '999', authorable_type: 'automated', detail: 'customer was sent an email to confirm a ticket was opened' )
    elsif self.reopened
      CustomerMailer.reopened_ticket_email(@customer).deliver_now
      self.note.create(authorable_id: '999', authorable_type: 'automated', detail: 'customer was sent an email to confirm a ticket was re-opened' )
    elsif self.customer_updated
      #update slack
    elsif self.needs_to_be_scheduled
      #update slack
    elsif self.scheduled
      CustomerMailer.scheduled_ticket_email(@customer).deliver_now
      self.note.create(authorable_id: '999', authorable_type: 'automated', detail: 'customer was sent an email to confirm that an appointment was created' )
    elsif self.closed
      CustomerMailer.closed_ticket_email(@customer).deliver_now
      self.note.create(authorable_id: '999', authorable_type: 'automated', detail: 'customer was sent an email to confirm a ticket was closed' )
    else
      return true
    end
  end

  def archive!
    self.status = Status.archived
    self.save do
      return false
     end
   end

 # end

end

这是票证的架构:

create_table "tickets", force: :cascade do |t|
    t.integer  "customer_id",     limit: 4
    t.integer  "tech_id",         limit: 4
    t.integer  "note_id",         limit: 4
    t.integer  "neighborhood_id", limit: 4
    t.integer  "appointment_id",  limit: 4
    t.integer  "category",        limit: 4
    t.integer  "status",          limit: 4
    t.integer  "severity",        limit: 4
    t.string   "summary",         limit: 255
    t.text     "detail",          limit: 65535
    t.text     "resolution",      limit: 65535
    t.datetime "created_at",                    null: false
    t.datetime "updated_at",                    null: false
  end

如果您认为它相关,我可以发布该表格,但我怀疑它不相关。 更新:我在下面添加了表格。

new.html.erb

<% @title = "New Ticket" %>

<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">

      <br>
      <br>
      <div class='col-sm-12'>
          <div class="panel panel-default" id="buttons">
            <div class="panel-heading">

             <% if @ticket.category = 'billing' %>
               I am fresh out of questions.
               <br>
               Please add some details below and we will open up a ticket with all of the information that you have provided us.

               <% elsif @ticket.category = 'internet' && @ticket.severity = 'low'%>
                 Ok. Your internet is working, but is having some other problem.
                 <br>
                 Please add some details below and we will open up a ticket with all of the information that you have provided us.

               <% elsif @ticket.category = 'internet' && @ticket.severity = 'medium'%>
                 Ok. Your internet is down, but your cable is still working.
                 <br>
                 Do you have any more details to add? If so, add them here. If not, just hit sumbit and we will open up a ticket with all of the information that you have provided us.

               <% elsif @ticket.category = 'cable' && @ticket.severity = 'low'%>
                 Ok. Your cable is working, but is having some other problem.
                 <br>
                 Please add some details below and we will open up a ticket with all of the information that you have provided us.

               <% elsif @ticket.category = 'cable' && @ticket.severity = 'medium' %>
                 Ok. Your cable is down, but your internet is still working.
                 <br>
                 Do you have any more details to add? If so, add them here. If not, just hit sumbit and we will open up a ticket with all of the information that you have provided us.

               <% else @ticket.category = 'internet_and_cable' %>
                 Ok. Your cable and internet are both down.'%>
                 <br>
                 Do you have any more details to add? If so, add them here. If not, just hit sumbit and we will open up a ticket with all of the information that you have provided us.'%>

             <%end%>
              <br>
            </div>
            <%= render partial: 'new_form' %>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

_new_form.html.erb

<%= form_for(@ticket, html: { class: 'form-horizontal' }) do |f| %>
  <%= f.error_notification %>
    <%= f.hidden_field(:category) %>
    <%= f.hidden_field(:severity) %>
  <%= f.hidden_field(:status) %>


    <br>
    <%= f.form_group :summary do |f| %>
      <%= f.label :summary, class: 'control-label col-md-2' %>
      <div class='col-md-8'>
        <%= f.text_field :summary, class: 'form-control' %>
        <%= f.error_messages %>
      </div>
    <% end %>

    <%= f.form_group :detail do |f| %>
      <%= f.label :detail, class: 'control-label col-md-2' %>
      <div class='col-md-8'>
        <%= f.text_area :detail, class: 'form-control' %>
        <%= f.error_messages %>
      </div>
    <% end %>
    <br>


  </div>

  <div class="form-actions col-md-offset-2 col-md-10">
    <%= f.submit 'Create', class: 'btn btn-primary' %>
    <%= link_to "Cancel", tickets_path, class: 'btn' %>
  </div>
<% end %>

谢谢!

--提姆

【问题讨论】:

  • 当您提交表单时,它应该转到控制器的“创建”操作,在那里您将实例化一个新的 Ticket 对象并应用正确的类别
  • 我的目标是根据类别和严重性预先填充表单,因此我需要将这些传递给表单。我将添加表单,以便您查看我的目标。

标签: ruby-on-rails enums


【解决方案1】:

只需将value 分配给表单中的fields

<%= f.hidden_field( :category, :value => @activity.category ) %>
<%= f.hidden_field( :severity, :value => @activity.severity ) %>
<%= f.hidden_field( :status, :value => @activity.status ) %>

createupdateerroredit 上的工作方式也相同,它设置表单字段的value,无论它在数据库中收到或找到什么。在new 的情况下,因为它假设您正在创建一个新表单,所以它不会预设任何东西..

【讨论】:

    【解决方案2】:

    我的解决方案是这样的。 票务员

    def billing_new_1
        @ticket = Ticket.new(severity: 'low', category: 'billing', summary: 'I am having a billing issue')
        render 'new'
      end
    

    new.html.erb

    <% @title = "New Ticket" %>
    <head>
      <script type="text/javascript">
      $(document).ready(function(){
          $('.ticket-customer-select').select2({
            theme: 'bootstrap'
          });
      });
    </script>
    </head>
    <div class="container">
      <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
          
          <br>
          <br>
          <div class='col-sm-12'>
              <div class="panel panel-default" id="buttons">
                <div class="panel-heading">
                 <% if @ticket.category == 'billing' %>
                   I am fresh out of questions.
                   <br>
                   Please add some details below and we will open up a ticket with all of the information that you have provided us.
                 
                   <% elsif @ticket.category == 'internet' and @ticket.severity == 'low' %>
                     Ok. Your internet is working, but is having some other problem.
                     <br>
                     Please add some details below and we will open up a ticket with all of the information that you have provided us.
                 
                   <% elsif @ticket.category == 'internet' and @ticket.severity == 'medium'%>
                     Ok. Your internet is down, but your cable is still working.
                     <br>
                     Do you have any more details to add? If so, add them here. If not, just hit sumbit and we will open up a ticket with all of the information that you have provided us.
                 
                   <% elsif @ticket.category == 'cable' and @ticket.severity == 'low'%>
                     Ok. Your cable is working, but is having some other problem.
                     <br>
                     Please add some details below and we will open up a ticket with all of the information that you have provided us.
                 
                   <% elsif @ticket.category == 'cable' and @ticket.severity == 'medium' %>
                     Ok. Your cable is down, but your internet is still working.
                     <br>
                     Do you have any more details to add? If so, add them here. If not, just hit sumbit and we will open up a ticket with all of the information that you have provided us.
                 
                   <% else @ticket.category == 'internet_and_cable' %>
                     Ok. Your cable and internet are both down.'%>
                     <br>
                     Do you have any more details to add? If so, add them here. If not, just hit sumbit and we will open up a ticket with all of the information that you have provided us.'%>
                 
                 <%end%>
                  <br>
                </div>
                <%= render partial: 'new_form' %>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 2011-10-16
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多