【问题标题】:Rails - Calling a POST function from serverRails - 从服务器调用 POST 函数
【发布时间】:2010-07-14 10:55:08
【问题描述】:

我需要从另一个操作访问控制器级别的操作。这是由视图访问的(因此需要真实性令牌)

def post_message
  #creates the message
  #normally accessed via webform with authenticity token
end

def post_via_email
  #my virtual mailserver calls this method when it receives an email
  #i need to call the post_message function
end

我知道如果我用 post_message() 调用前者,它将是一个 GET 调用。不会有真实性令牌。

如果我从网页访问前一个函数以及参数和令牌,我如何调用它?

【问题讨论】:

  • 您能否更具体地了解这两种方法的作用?一般来说,您不应该直接调用控制器操作。控制器旨在处理 HTTP 请求。您的业​​务逻辑应该驻留在不同的类/模型中。然后,您可以在该类的实例上调用您想要的任何方法,无论是从您的控制器还是您想要的任何其他地方。

标签: ruby-on-rails ruby authentication httpwebrequest


【解决方案1】:

我认为您不能直接从 post_via_email 操作中调用 post_message。

你可以试试这个。

def post_message
  private_post_message(params)
  #creates the message
  #normally accessed via webform with authenticity token
end

def post_via_email
  private_post_message(params)
  #my virtual mailserver calls this method when it receives an email
  #i need to call the post_message function
end

private

def private_post_message(param)
  #posting message code here
end

【讨论】:

  • 谢谢!这将意味着对代码进行一些重构,但我想这是不可避免的...... =)
猜你喜欢
  • 2012-07-16
  • 1970-01-01
  • 2018-10-19
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 2014-01-04
  • 2012-11-18
相关资源
最近更新 更多