检查记录是否保存在 JS 中



使用标准update操作与remote表单相结合,用户通过AJAX提交一小块数据,这些数据作为javascript发送到操作,响应使用format.js在javascript中呈现。

def update
  @message = Message.where("recipient_id = ? AND id = ?", current_user.id, params[:id]).first
  respond_to do |format|
    if @message.update_attributes(params[:message])
      format.html { redirect_to(@message, :notice => 'Message updated.') }
      format.xml  { head :ok }
      format.js
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @message.errors, :status => :unprocessable_entity }
      format.js
    end
  end
end

这会生成一个名为 update.js.erb 的 JS 文件,我用它来渲染文本和效果,向用户显示操作是否成功。

如何签入 update.js.erb 该记录是否保存?

目前,我正在控制器中执行极其不优雅的@success = true,我在js.erb文件中对其进行了测试,以决定要渲染的文本/效果。一定有更好的方法。

嗨,

使用它怎么样:

<% if @message.errors.empty? %>
  SUCCESS
<% else %>
  FAIL_CODE
<% end %>

我希望这有所帮助。

最新更新