Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 通过设计(Rails)允许AJAX_Ruby On Rails_Ajax_Ruby On Rails 3_Devise_Ruby On Rails Plugins - Fatal编程技术网

Ruby on rails 通过设计(Rails)允许AJAX

Ruby on rails 通过设计(Rails)允许AJAX,ruby-on-rails,ajax,ruby-on-rails-3,devise,ruby-on-rails-plugins,Ruby On Rails,Ajax,Ruby On Rails 3,Devise,Ruby On Rails Plugins,我刚刚添加并配置了Desive gem。除了阻止表单的autosaveajax调用之外,它工作得很好 在控制器的顶部,我有: before_filter :authenticate_user! 我的AJAX调用来自同一个控制器: def autosave #TODO: update relative entry #TODO: verify user logged in #TODO: verify entry belongs to relative user

我刚刚添加并配置了Desive gem。除了阻止表单的autosaveajax调用之外,它工作得很好

在控制器的顶部,我有:

before_filter :authenticate_user!
我的AJAX调用来自同一个控制器:

  def autosave
    #TODO: update relative entry
    #TODO: verify user logged in
    #TODO: verify entry belongs to relative user
    render content_type: 'text/xml', inline: "<result status='ok' />"
  end
…但这并不能阻止任何人在任何时候调用此控制器函数


允许调用此函数的最佳方式是什么?我仍然希望确保只有登录的用户才能拨打电话,并且他们正在编辑的给定记录属于他们。

不完全确定您要做什么,但听起来您只需要在autosave中添加一些逻辑。在这种设置下,函数本身仍将被调用,但它是否执行任何操作完全是另一回事,除非条件正确

def autosave
  #Verify that the user is signed in and he has ownership of entry
  if !current_user.nil? && entry.user == current_user
    render content_type: 'text/xml', inline: "<result status='ok' />"
  else
    #do nothing nothing or redirect with error here
  end  
end
def自动保存
#验证用户是否已登录并拥有条目的所有权
如果!当前_user.nil?&entry.user==当前用户
呈现内容类型:'text/xml',内联:'
其他的
#在此处不执行任何操作或重定向错误
结束
结束

由于要发布AJAX帖子,您必须为AJAX调用提供一些附加信息作为安全对策,即CSRF安全令牌。有关使用安全令牌的AJAX请求的示例,请参见。另外,确保您的头标签中也包含

由于调用来自AJAX(确切地说是一种CKEDITOR表单),而不是Rails链接,因此它看起来像current_user.nil?将始终返回真值。我必须传递一个特定的参数吗?当前用户返回什么?
def autosave
  #Verify that the user is signed in and he has ownership of entry
  if !current_user.nil? && entry.user == current_user
    render content_type: 'text/xml', inline: "<result status='ok' />"
  else
    #do nothing nothing or redirect with error here
  end  
end