Rails 3.1 Ajax 表单不显示验证

发布于 2024-11-08 04:58:05 字数 988 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少钕鈤記 2024-11-15 04:58:18

我不相信 Ruby on Rails 会自动包含您请求的部分。您需要添加一些 JavaScript 代码才能使其正常工作。我想象这样的事情:

// Application.js
$("form").bind("ajax:success", function (event, data, status, xhr) {
    $(event.target).replaceWith(data);
});

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:

// Application.js
$("form").bind("ajax:success", function (event, data, status, xhr) {
    $(event.target).replaceWith(data);
});
Spring初心 2024-11-15 04:58:05

您缺少调用这些错误。执行如下操作:

<%= form_for @project do |f| %>
<%= render :partial => 'my_error_messages_for', :locals => {:collection => @project} %>

在“_my_error_messages_for.html.erb”中:

<% if collection.errors.any? %>
  <ul>
    <% collection.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  </ul>
<% end %>

您可以为此工作创建一个助手。

You are missing to call these errors. Do something like this:

<%= form_for @project do |f| %>
<%= render :partial => 'my_error_messages_for', :locals => {:collection => @project} %>

In "_my_error_messages_for.html.erb":

<% if collection.errors.any? %>
  <ul>
    <% collection.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  </ul>
<% end %>

You can create a helper for this work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文