从视图中调用helper方法

时间:2013-11-29 10:28:51

标签: ruby-on-rails templates ruby-on-rails-4 erb twilio

我希望能够点击按钮并使用twilio向该人发送短信。

在我的控制器中,我有一个辅助方法: 控制器/ people_controller.rb:

  def send_text_message
        @person = Person.find(params[:id])
    @account_sid = 'ACadb7a...bb88a3a69b6832'
    @auth_token = 'd751f77....645e31b72'

    # set up a client to talk to the Twilio REST API
    @client = Twilio::REST::Client.new(@account_sid, @auth_token)


    @account = @client.account
    @message = @account.sms.messages.create({
        :from => '+19526496571',
        :to => @person.phone,
        :body => "sent using twilio"
        })
  end

  helper_method :send_text_message

然后在节目视图中,我想点击链接并发送消息:

<%= link_to "message this user", {:action => controller.send_text_message}, {:method => :put } %>

当我运行时,我收到一个错误:

ActionController::UrlGenerationError in People#show

No route matches {:action=>"#<Twilio::REST::SMS::Message:0xb55a5658>", :id=>"2", :controller=>"people"}

如何从Person#show view调用send_text_message方法?

1 个答案:

答案 0 :(得分:1)

routes.rb 文件中添加路径

类似的东西:

put '/person/send_sms_message' => 'person#send_sms_message'