gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 08:46:43 24 4
gpt4 key购买 nike

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

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

之后,我启动服务器,然后单击“添加任务”,填写 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com