1

我已经包含了 inflections.rb

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.irregular 'leaves', 'leaves'
end

和我的控制器 Leaves_controller.rb

class LeavesController < ApplicationController
  def new
    @leave = Leave.new
  end

  def create
    @leave = Leave.create(leave_params)
    if @leave.errors.present?
      render :action => :new
    else
      redirect_to root_path, notice: I18n.t('leave_applied')
    end
  end

  private

  def leave_params
    params.require(:leave).permit(:start_date,:end_date)
  end
end

在叶子/new.html.erb

<%= form_for @leave do |f| %>

  <%= f.text_field :start_date, :placeholder => 'From Date',
                   :class => 'datepicker' %>

  <%= f.text_field :end_date, :placeholder => 'To Date',
                   :class => 'datepicker' %>
  <%= f.submit%>
<% end %>

所以当我去叶子/新路径时,它给了我这个错误:

NameError (uninitialized constant Leaves):

在此处输入图像描述

请指导我如何解决这个问题。

4

1 回答 1

2

问题是:当它试图根据你的新变形器leaves/new搜索常量时。Leaves

将其更改为

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.irregular 'leave', 'leaves'
end
于 2015-06-18T05:57:38.637 回答