如何在创建新项目时从故障单控制器设置枚举

时间:2016-02-12 17:16:22

标签: ruby-on-rails enums

我的目标是在加载页面时在故障单上设置类别,状态和严重性(故障单模型上定义的所有枚举)。 正在加载的页面是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 %>

谢谢!

- 添

2 个答案:

答案 0 :(得分:0)

只需将%i分配给表单中的value即可。

fields

<%= f.hidden_field( :category, :value => @activity.category ) %> <%= f.hidden_field( :severity, :value => @activity.severity ) %> <%= f.hidden_field( :status, :value => @activity.status ) %> createupdateerror上的工作方式也相同,它会设置表单字段的edit,无论收到的是什么,在db中找到。如果是value,因为它假设您正在创建新表单,它不会预设任何内容..

答案 1 :(得分:0)

我的解决方案就是这样。 ticketscontroller

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>