无法通知使用AJAX CRUD操作以及如何在不重新加载的情况下显示通知?

时间:2012-07-31 08:33:13

标签: jquery ruby-on-rails ajax

EDITED

尝试了很多解决方案,但没有任何效果。

以下是create.js.erb的代码:

$('<%= j render @website %>').appendTo('#websites').hide().fadeIn();
$("#new_website")[0].reset();
$(".alert").html("<%= escape_javascript(flash[:notice]) %>"); $(".alert").show(300); 

这是我的控制器:

   def create
@website = current_user.websites.new params[:website]

respond_to do |format|
  if @website.save
   format.html { redirect_to  @website, notice: 'Website was successfully created.' }
  format.js {
    flash[:notice] = 'Website was successfully created.'
    render action: 'create'
  }

   else
    format.html { render action: "new" }
    format.js { render action: "new" }
   end
  end
end

我的通知HTML - 提醒:

       <div class="alert alert-notice">

你能说明为什么它不起作用吗?

2 个答案:

答案 0 :(得分:1)

看来你只是在回复html请求时设置通知,而不是js请求,我认为你需要这样的东西:

def create
  @website1 = Website1.new(params[:website1])

  respond_to do |format|
    if @website1.save
      format.html { redirect_to @website1, notice: 'website1 was successfully created.' }
      format.js {
        flash[:notice] = 'website1 was successfully created.'
        render action: 'create'
      }
    else
      format.html { render action: "new" }
      format.js { render action: "new" }
    end
  end
end

答案 1 :(得分:0)

尝试删除该行

  

渲染动作:'创建'