没有刷新页面如何显示闪存错误?

时间:2021-06-25 01:49:03

I have a submit form that is shown in a lightbox. I validate the form pretty heavily but there are circumstances where a playlist is already submitted, in which case I can't tell before a form is submitted.

我有一个提交表单,显示在灯箱中。我非常认真地验证了表单,但是有些情况下已经提交了播放列表,在这种情况下,在提交表单之前我无法分辨。

Currently I have :disable_with => 'Submitting...'

目前我有:disable_with =>'正在提交......'

It would be completely awesome if the error was sent back to the form, and the submit button would re-enable.

如果错误被发送回表单,并且提交按钮将重新启用,那将是非常棒的。

Would this be possible without too much complication?

这可能没有太多的复杂性吗?

I also have a second related question... Because my form is in a lightbox, it's code is actually ON the index page (my app is just one page pretty much). This means that there really is no 'new' action, but rather a @playlist = Playlist.new(:title => ...) in my index action (along with a @playlists = Playlist.all, etc). In my routes I did this: resources :playlists, :only => [:create]

我还有第二个相关的问题...因为我的表单在灯箱中,它的代码实际上是在索引页面上(我的应用程序只是一页)。这意味着我的索引操作中没有'new'动作,而是@playlist = Playlist.new(:title => ...)(以及@playlists = Playlist.all等)。在我的路线中,我这样做:资源:播放列表,:only => [:create]

Does this sound about right the way I did it?

这听起来跟我做的一样吗?

EDIT: HEre is some code, although it's basically about as simple as you can imagine it.

编辑:HEre是一些代码,虽然它基本上就像你想象的那么简单。

The following kind of works... it creates the playlist if its valid, otherwise it does nothing. Both times create.js.erb is called.. i just dont now how to make this work to completion now. on success i need to close the window, on failure i need to load the errors into the form thats already on the screen. Im not sure where that goes though :/

以下类型的作品......如果有效,则创建播放列表,否则无效。两次调用create.js.erb ..我现在不知道如何使这项工作完成。在成功我需要关闭窗口,失败时我需要将错误加载到已经在屏幕上的形式。我不知道那是怎么回事:/

  before_filter :load_genres, :only =>[:index, :user]
  before_filter :new_playlist, :only => [:index, :new]

  def index
    @playlists = Playlist.order('created_at DESC').page(params[:page]).per(30)
  end

  def create
    @playlist = Playlist.new(params[:playlist])
    respond_to do |format|
      if @playlist.save
        format.html { redirect_to root_path, :notice => "Playlist submitted" }
        format.js   {}
      else
        format.html { render :action => :new, :layout => !request.xhr? }
        format.js {}
      end
    end
  end

  def new

  end

  def load_genres
    @genres = Genre.order(:name)
  end

  def new_playlist
    @playlist = Playlist.new(:title => params[:title], :url => params[:url], :description => params[:description])
  end

Heres the first like of my form (located in index.html.erb):

下面是第一个喜欢我的表单(位于index.html.erb):

<%= form_for @playlist, :remote => true do |f| %>

<%= form_for @playlist,:remote => true do | f | %>

I currently have no html or code in create.html.erb

我目前在create.html.erb中没有html或代码

2 个解决方案

#1


0  

Without seeing your code I can only make broad suggestions.

没有看到你的代码,我只能提出广泛的建议。

One would be to set up a js.erb response to the controller action-- or you could do a js script tag in an if clause inside your HTML. Either way you would use jQuery to update the element.

一种方法是设置对控制器动作的js.erb响应 - 或者你可以在HTML中的if子句中执行js脚本标记。无论哪种方式,您都可以使用jQuery来更新元素。

Inside the js/html erb file:

在js / html erb文件中:

jQuery('#flash_area).html(<flash message>);

The the flash area in the view would need an id of flash_area (or whatever you want to name it).

视图中的flash区域需要一个flash_area的id(或者你想要命名的任何名称)。

#2


1  

Here is my solution to this Issue.... I put these in all my js.erb files

这是我对这个问题的解决方案....我把这些放在我所有的js.erb文件中

$('#flash').html("<%= escape_javascript raw(flash_display) %>");

With this Helper

有了这个助手

def flash_display
  response = ""
  flash.each do |name, msg|
    response = response + content_tag(:div, msg, :id => "flash_#{name}")
  end
  flash.discard
  response
end

this works well with the flash div already set up in layout

这适用于已经在布局中设置的flash div

<div id="flash">
  <% flash.each do |name, msg| %>
    <%= content_tag :div, msg, :id => "flash_#{name}" %>
  <% end %>
</div>

Hope this Helps

希望这可以帮助

#1


0  

Without seeing your code I can only make broad suggestions.

没有看到你的代码,我只能提出广泛的建议。

One would be to set up a js.erb response to the controller action-- or you could do a js script tag in an if clause inside your HTML. Either way you would use jQuery to update the element.

一种方法是设置对控制器动作的js.erb响应 - 或者你可以在HTML中的if子句中执行js脚本标记。无论哪种方式,您都可以使用jQuery来更新元素。

Inside the js/html erb file:

在js / html erb文件中:

jQuery('#flash_area).html(<flash message>);

The the flash area in the view would need an id of flash_area (or whatever you want to name it).

视图中的flash区域需要一个flash_area的id(或者你想要命名的任何名称)。

#2


1  

Here is my solution to this Issue.... I put these in all my js.erb files

这是我对这个问题的解决方案....我把这些放在我所有的js.erb文件中

$('#flash').html("<%= escape_javascript raw(flash_display) %>");

With this Helper

有了这个助手

def flash_display
  response = ""
  flash.each do |name, msg|
    response = response + content_tag(:div, msg, :id => "flash_#{name}")
  end
  flash.discard
  response
end

this works well with the flash div already set up in layout

这适用于已经在布局中设置的flash div

<div id="flash">
  <% flash.each do |name, msg| %>
    <%= content_tag :div, msg, :id => "flash_#{name}" %>
  <% end %>
</div>

Hope this Helps

希望这可以帮助