2014-11-22 78 views
0

下午好!爲什麼我的門票控制器創建操作失敗?

我似乎無法找到問題出現在我的門票上,我想也許再來看看它會有所幫助:)。此外,非常感謝您利用您的時間幫助我。

除了創建操作之外,我的故障單的所有頁面都能正常工作。在我點擊Create New Ticket後,轉到tickets_path即可正確工作。但是,它一直閃爍,無法提交票證。並且不會在表中創建記錄。

但是沒有錯誤,控制檯在下面的日誌中顯示窗體的參數。

# Controller 
class TicketsController < ApplicationController 
    before_filter :authenticate_user! 
    before_action :set_ticket, only: [:show, :edit, :update, :create] 

    respond_to :html 

    def index 
    @tickets = Ticket.includes(:category, :user, :ticket_status) 
    #authorize User 
    end 

    def show 
    @ticket = Ticket.find(params[:id]) 
    #authorize @user 
    end 

    def new 
    @ticket = Ticket.new 
    @ticket.ticket_status = TicketStatus.find_by_name("Open") 
    end 

    def edit 
    @ticket = Ticket.find(params[:id]) 
    end 

    def create 
    @ticket = Ticket.new(ticket_params) 
    if @ticket.save 
     redirect_to tickets_path, :notice => "Ticket Submitted." 
    else 
     redirect_to tickets_path, :alert => "Unable to submit ticket." 
    end 
    end 

    def update 
    @ticket = Ticket.find(params[:id]) 
    if @ticket.update(ticket_params) 
     redirect_to tickets_path, :notice => "Ticket updated." 
    else 
     redirect_to edit_ticket_path, :alert => "Unable to update ticket." 
    end 
    end 

    def destroy 
    @ticket = Ticket.find(params[:id]) 
    @ticket.destroy 
    redirect_to tickets_path, :notice => "Ticket deleted." 
    end 

    private 

    def set_ticket 
     @ticket = Ticket.find(params[:id]) 
    end 

    def ticket_params 
     params[:ticket].permit(:ticket_status_id, :user_id, :category_id, :title, :description) 
    end 

end 

# Model 
class Ticket < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :category 
    belongs_to :ticket_status 
    validates_presence_of :title, presence: true 
    validates_presence_of :description, presence: true 
    validates_presence_of :user_id, presence: true 
    validates_presence_of :category_id, presence: true 
    validates_presence_of :ticket_status_id, presence: true 
end 

# Ticket _form partial 
<div class="container"> 
    <div class="row"> 
    <hr> 
    <h3><%= content_for?(:title) ? content_for(:title) : "Progressor Admin" %> 
    <span><%= link_to current_user.email, edit_user_registration_path, :class => 'glyphicon glyphicon-user btn btn-primary btn-sm pull-right', id: 'tooltip-profile', data: {toggle: 'tooltip', placement: 'right'}, :'data-original-title' => 'View Profile' %></span> 
    </h3> 
    <hr> 

<%= render 'sidebar' %> 

<div class="col-sm-9"> 
    <%= form_for @ticket do |f| %> 
    <% if @ticket.errors.any? %> 
     <div class="alert alert-error"> 
     <b><%= pluralize(@ticket.errors.count, "error") %> prohibited this ticket from being saved:</b> 

     <ul> 
      <% @ticket.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
      <% end %> 
     </ul> 
     </div> 
    <% end %> 
    <table class="table panel"> 
    <tbody> 
     <tr> 
     <td><%= f.label :user_id, "User Name" %></td> 
     <td><%= f.label current_user.name %></td> 
     </tr> 
     <tr> 
     <td><%= f.label :category_id, "In which area are you having an issue?" %></td> 
     <td><%= f.select(:category_id, Category.order("name ASC").map { |c| [c.name, c.id] }, {}, {:class => 'form-control'}) %></td> 
     </tr> 
     <tr> 
     <td><%= f.label :ticket_status_id %></td> 
     <td><%= f.select(:ticket_status_id, TicketStatus.order("position ASC").map{|c[c.name, c.id] }, {}, {:class => 'form-control'}) %></td> 
     </tr> 
     <tr> 
     <td><%= f.label :title, "Ticket Title" %></td> 
     <td><%= f.text_field :title, :class => 'form-control' %></td> 
     </tr> 
     <tr> 
     <td><%= f.label :description, "Ticket Description" %></td> 
     <td><%= f.text_area :description, :class => 'form-control', :rows => "10"%></td> 
     </tr> 
    </tbody> 
    </table> 
    <div class="form-actions"> 
     <% if @ticket.id %> 
     <%= link_to 'Cancel', @ticket, :class => "btn btn-danger" %> 
     <% else %> 
     <%= link_to 'Cancel', tickets_path, :class => "btn btn-danger" %> 
     <% end %> 
     <%= f.submit :class => "btn btn-success pull-right" %> 
    </div> 
    <% end %> 
</div> <!-- col-sm-9 --> 

# Development Log 

Started POST "/tickets" for ip-address at 2014-11-22 13:41:59 -0600 
 
    Processing by TicketsController#create as HTML 
 
    Parameters:  {"utf8"=>"✓","authenticity_token"=>"N9rBPw6v+T2OMnn7dQp8NsFC5saYuUQsP5pcXe5gfhQ=", "ticket"=> {"category_id"=>"5", "ticket_status_id"=>"8", "title"=>"asdf", "description"=>"asdf"}, "commit"=>"Create Ticket"} 
 
    [1m[36mUser Load (0.5ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 13 ORDER BY "users"."id" ASC LIMIT 1[0m 
 
    [1m[35m (0.2ms)[0m BEGIN 
 
    [1m[36m (0.1ms)[0m [1mROLLBACK[0m 
 
    Redirected to /tickets 
 
    Completed 302 Found in 30ms (ActiveRecord: 5.3ms)

+0

嘗試從'rails console'創建=>'t = Ticket.create({「category_id」=>「5」,「ticket_status_id」=>「8」,「title」=>「asdf」,「描述 「=>」 ASDF「}); t.erros'和看錯誤。 – 2014-11-22 20:22:11

+0

請在您的機票類中添加您的驗證列表。並嘗試以下命令來檢查錯誤消息是什麼。 在else塊中引入@ ticket.errors.inspect。 – Rubyrider 2014-11-22 20:26:40

回答

1

你讓POST請求

時缺少user_id的屬性,這是因爲這行:

<td><%= f.label :user_id, "User Name" %></td> 

你的意思是寫

<td><%= f.hidden_field :user_id, value: current_user.id %></td> 
+0

謝謝!這確實起作用,並允許POST成功完成。我故意只使用它的原因是在表格的第一行顯示用戶名稱,但我已經將其更改爲在其他地方顯示當前用戶的用戶名。再次感謝! – jriley88 2014-11-22 22:33:29

相關問題