0

我有一个运行良好的简单 rails 应用程序,但我想将其更改为使用 ajax。我已经使用 Ajax 升级了其他部分,但我在控制器代码方面遇到了问题。所以:

1) 我已将 remote: true 添加到 form_for。

2) 我的控制器代码有问题。目前它看起来像这样:

def create #saving new materials
    @material = Material.new(params[:material])
    @material.user_id = current_user.id if current_user
    if @material.save
      flash[:success] = "Content Successfully Created"
      redirect_to @material
    else
      render 'new'
    end
end 

问题是我知道我需要一个 respond_to 块,但我不知道把它放在哪里。我是新手,如果这是一个愚蠢的问题,我深表歉意。

仅供参考 - js 代码很好。

4

1 回答 1

1

像这样的东西应该工作:

def create #saving new materials
  @material = Material.new(params[:material])
  @material.user_id = current_user.id if current_user
  respond_to do |format|
    if @material.save
      flash[:success] = "Content Successfully Created"
      format.html { redirect_to @material }
      format.js
    else
      format.html { render 'new' }
      format.js
    end
  end
end
于 2013-01-26T10:25:22.020 回答