【问题标题】:Rails 4 controller does not seem correct after scaffold脚手架后 Rails 4 控制器似乎不正确
【发布时间】:2014-11-06 06:02:11
【问题描述】:

为了培训,我创建了一个简单的 rails 应用程序。 那么

rails g scaffold Tache titre:string desc:text --skip-stylesheets
rake db:migrate
rails g bootstrap:install static

之后,我启动服务器,然后单击“添加 Tache”,填写 2 个字段,然后出现此错误:

参数丢失或值为空:tach

   # Never trust parameters from the scary internet, only allow the white list through.
    def tach_params
      params.require(:tach).permit(:titre, :desc)
    end
end

所以我查看了 taches_controller.rb,我注意到 tache 被截断了。 如果我改变:

      params.require(:tach).permit(:titre, :desc)

      params.require(:tache).permit(:titre, :desc)

它有效。而且这行代码并不是唯一一个最后一个字符被截断的代码。

例子:

    def update
    respond_to do |format|
      if @tach.update(tach_params)
        format.html { redirect_to @tach, notice: 'Tache was successfully updated.' }
        format.json { render :show, status: :ok, location: @tach }
      else
        format.html { render :edit }
        format.json { render json: @tach.errors, status: :unprocessable_entity }
      end
    end
  end

你能告诉我为什么它被这样截断吗?我一定错过了什么,但我看不到什么。

问候

【问题讨论】:

    标签: ruby-on-rails ruby bootstrapping scaffold


    【解决方案1】:

    当 Rails 尝试使用复数 taches 时,就会发生这种情况。您可以在 Rails 控制台中尝试一下:

    "taches".singularize
    # => "tach"
    

    您可以通过将其放入初始化程序来纠正此行为(最好是config/initializers/inflections.rb):

    ActiveSupport::Inflector.inflections do |inflect|
      inflect.irregular 'tache', 'taches'
    end
    

    请务必重新启动 Rails 控制台和服务器。然后你可以再试一次:

    "taches".singularize
    # => "tache"
    

    【讨论】:

    • 谢谢!我有这个想法,但我忘记了“es”也可以被认为是复数。
    • 不客气!如果此解决方案适合您,请接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 2012-01-04
    • 2015-04-15
    • 2014-08-30
    相关资源
    最近更新 更多