简体   繁体   中英

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.

In my controller I have a helper method: 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?

Add the path in the routes.rb file.

Something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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