为了进行训练,我创建了一个简单的 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/