简体   繁体   English

从视图中调用助手方法

[英]call helper method from view

I would like to be able to click a button and send a text message to the person using twilio. 我希望能够单击一个按钮,然后使用twilio向该人发送短信。

In my controller I have a helper method: controllers/people_controller.rb: 在我的控制器中,我有一个辅助方法:controllers / 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

Then in the show view I would like to click the link and send the message: 然后在显示视图中,我想单击链接并发送消息:

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

When I run this I get an error: 当我运行它时,我得到一个错误:

ActionController::UrlGenerationError in People#show

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

How do I call the send_text_message method from the Person#show view? 如何从Person#show视图调用send_text_message方法?

Add the path in the routes.rb file. route.rb文件中添加路径

Something like: 就像是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM