为什么我的Tickets控制器创建操作失败?

时间:2014-11-22 20:06:43

标签: ruby-on-rails model-view-controller

下午好!

我似乎无法在我的门票上找到问题所在,我想也许再看看它会有所帮助:)。另外,非常感谢您利用时间帮助我。

除了“创建”操作之外,我的故障单的所有页面都能正常工作。在我点击New的Create 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

&#13;
&#13;
    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)
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您在发出POST请求时缺少user_id属性

这是因为这一行:

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

你打算写

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