我用Rails 3,我有一个Ajax的形式(如:远程=>真正的)。这打在我的控制器中的插入行动
I'm using Rails 3 and I have an ajax form (i.e. :remote => true) that hit's a "insert" action on my controller.
我要的是为用户留在索引页上,但能够显示在控制器中的插入动作设置提示信息。
What I want is for the user to stay on the index page but be able to display flash messages set by the "insert" action in the controller.
目前我的插入行动确实是这样的:
Currently my "insert" action does something like:
def insert
begin
# perform insert of stuff
flash[:success] = "Successfully..."
rescue
flash[:error] = "failed ..."
end
render :nothing => true
end
这使形式(上index.html.erb)应提交,并保持在该网页的用户,但没有闪光灯的信息显示出来。我怎样才能出现的提示信息? (猜测它做的渲染:没有什么东西,但没有我得到的服务器日志缺失模板的错误)。
This allows the form (on index.html.erb) to be submitted and keeps the user on that page but no flash messages show up. How can I get the flash messages to appear? (guessing it has to do with the render :nothing stuff, but without that I get "missing template" errors in the server log).
您将需要一个JavaScript应答:
You'll need a javascript responder:
def insert
begin
# perform insert of stuff
flash[:success] = "Successfully..."
rescue
flash[:error] = "failed ..."
end
end
# insert.js.erb
$('#flash').html(<%= flash[:notice].to_json %>);
使用闪光灯展现形式的错误消息肯定是不规律的行为。你会更好地与错误信息的表单中更新的元素或完全替代形式。
Using flash to show form error messages is definitely irregular behavior. You'd be better off updating an element within the form with the error message or replacing the form altogether.