使用按钮向Rails表添加新行

时间:2014-09-29 15:30:01

标签: ruby-on-rails ruby ruby-on-rails-4

希望我能以一种有意义的方式解释这一点:在我的rails应用程序中,我有一张票证表。我正在尝试为用户尽可能简单地添加票证。因此,当他们点击“+ Ticket”时,会弹出一个模式,其中包含所有员工的列表作为按钮。当他们点击带有名称的按钮时,Rails应该创建一个新的票证并填写:created_employee_id字段:

%div.modal.fade{:id => "add_ticket", "aria-hidden" => "true", :tabindex => "-1"}
  .modal-dialog.modal-lg
    .modal-content
      .modal-header
        %button.close{"data-dismiss" => "modal", :type => "button"}
          %span{"aria-hidden" => "true"} ×
          %span.sr-only Close
        %h4.modal-title New Ticket
      .modal-body
        .row
          .col-md-12
            %h3 Employee creating ticket:
            - Employee.where(deleted: [false, nil]).each do |employee|
              - if (employee_check(employee))
                .col-md-4
                  %button.btn.btn-default.btn-block
                    = button_to employee.name, {:controller => "tickets", :action => "create", :created_employee_id => employee.id}, :method=>:post

现在,我收到以下错误:

ActionController::ParameterMissing in TicketsController#create

param is missing or the value is empty: ticket


# Never trust parameters from the scary internet, only allow the white list through.
def ticket_params
  params.require(:ticket).permit(:created_employee_id)
end

def load_ticketable

我明白这意味着它实际上并没有创建新票证,所以如何让它做到呢?

这是与ticket_controller.rb相关的内容:

  def new
    @ticket = Ticket.new
  end

  def create
    @ticket = Ticket.new(ticket_params)

    respond_to do |format|
      if @ticket.save
        format.html { redirect_to @tickets, notice: 'Ticket was successfully created.' }
        format.json { render action: 'show', status: :created, location: @tickets }
      else
        format.html { render action: 'new' }
        format.json { render json: @ticket.errors, status: :unprocessable_entity }
      end
    end
  end

private

  def ticket_params
    params.require(:ticket).permit(:created_employee_id)
  end

1 个答案:

答案 0 :(得分:0)

目前,您的控制器希望params字典的结构如下:

{ :ticket => { :created_employee_id => 123 } }

我相信您的按钮是以以下形式提交数据:

{ :created_employee_id => 123 }

尝试将ticket_params更改为:

def ticket_params
    params.permit(:created_employee_id)
end