我正在使用Rails 3,我有一个ajax形式(即:remote => true),它在我的控制器上点击了“插入”动作。
我想要的是让用户留在索引页面上,但能够显示控制器中“插入”操作设置的闪存消息。
目前我的“插入”操作的行为类似于:
def insert
begin
# perform insert of stuff
flash[:success] = "Successfully..."
rescue
flash[:error] = "failed ..."
end
render :nothing => true
end
这允许提交表单(在index.html.erb上)并使用户保持该页面,但不显示任何flash消息。如何显示Flash消息? (猜测它与渲染有关:没什么东西,但没有它我在服务器日志中得到“缺少模板”错误。)
答案 0 :(得分:4)
你需要一个javascript响应者:
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 %>);
使用flash显示表单错误消息绝对是不规则的行为。您最好使用错误消息更新表单中的元素或完全替换表单。