Rails 3.1 Ajax 表单不显示验证
我正在 Ruby 1.9.2(带有 rvm)上使用更新的预版 Ruby on Rails,并创建了一个新的测试应用程序,
$ rails generate scaffold Project name:string
并将
class Project < ActiveRecord::Base
validates_presence_of :name
end
其更改
<%= form_for @project do |f| %>
为
<%= form_for @project, :remote => true do |f| %>
现在仍然可以(无需对控制器进行任何更改)向项目添加新项目。如果我尝试在名称字段中添加空,它不会添加任何内容(validates_presence_of :name 停止此操作),但我没有收到任何验证错误消息。我在从 3.0 转换而来的应用程序上尝试了相同的操作,得到了相同的结果。这里我有:
class KursController < ApplicationController
# GET /kurs
# GET /kurs.xml
respond_to :js, :html
和:
def update
@kur = Kur.find(params[:id])
@kur.update_attributes(params[:kur])
flash[:notice] = "Lagret" if @kur.save
respond_with( @kur, :layout => !request.xhr? )
end
在 3.1 中我没有收到任何验证错误消息。这是因为 Ruby on Rails 3.1 中的错误还是我应该采取不同的措施?
I'm using updated pre Ruby on Rails on Ruby 1.9.2 (with rvm), and made a new test application with
$ rails generate scaffold Project name:string
and
class Project < ActiveRecord::Base
validates_presence_of :name
end
I change
<%= form_for @project do |f| %>
to
<%= form_for @project, :remote => true do |f| %>
I can now still (without any changes in the controller) add new items to the project. If I try to add with empty in the name field, it will not add anything (validates_presence_of :name stops this), but I don't get any validation error messages. I have tried the same on an application converted from 3.0 I'm working on with same results. Here I had:
class KursController < ApplicationController
# GET /kurs
# GET /kurs.xml
respond_to :js, :html
and:
def update
@kur = Kur.find(params[:id])
@kur.update_attributes(params[:kur])
flash[:notice] = "Lagret" if @kur.save
respond_with( @kur, :layout => !request.xhr? )
end
In 3.1 I get no validation error messages. Is this because of a bug in Ruby on Rails 3.1 or something I should do different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不相信 Ruby on Rails 会自动包含您请求的部分。您需要添加一些 JavaScript 代码才能使其正常工作。我想象这样的事情:
I don't believe that Ruby on Rails will automatically include the partial that you've requested. You'll need to add some JavaScript code to get it to work correctly. Something like this I imagine:
您缺少调用这些错误。执行如下操作:
在“_my_error_messages_for.html.erb”中:
您可以为此工作创建一个助手。
You are missing to call these errors. Do something like this:
In "_my_error_messages_for.html.erb":
You can create a helper for this work.