ruby-on-rails - Rails 4 Controller 在脚手架后似乎不正确

标签 ruby-on-rails ruby bootstrapping scaffold

为了进行训练,我创建了一个简单的 Rails 应用程序。 然后

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

之后,我启动服务器,然后单击“Add 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

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

问候

最佳答案

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

"taches".singularize
# => "tach"

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

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

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

"taches".singularize
# => "tache"

关于ruby-on-rails - Rails 4 Controller 在脚手架后似乎不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25801828/

相关文章:

grails - 如何在 Bootstrap 中获取 grails 数据源 createdb 属性

javascript - 仅当在选择框中选择 "Others"时,显示文本字段并获取输入的最佳方式是什么?

ruby-on-rails - rails 3.0.3 - ActiveRecord::Relation,未定义的方法错误

python - 为什么 "End of File"(EOF) 是此处文档的实际分隔符?

Ruby PATH 未正确设置

linux - 尝试运行 faac 的引导脚本但遇到错误

jquery - Rails 3 respond_to 不响应

ruby-on-rails - 从不同的 View 调用模型方法

ruby-on-rails - 未定义的方法 `click' 用于 "2":String, 使用 Mechanize 时出现 Rails 错误

compiler-construction - CoffeeScript 如何用 CoffeeScript 编写?