Q:

Routing Error -- Triggering Controller Action via Button in Rails 4

I have Clients and, among other things, I want to be able to click two buttons = one to send a prewritten text message and one to delete the client. 
Currently, 
Sending a Text Message (via welcome in the controller) = does not work 
Delete Client (via destroy in the controller) = works perfectly 
The error when I click on "Welcome Text" on the show view on Client #5 is: 
No route matches [POST] "/clients/5"

I can confirm that sending a text message in other parts of the app works 
Relevant parts of the clients_controller.rb:
class ClientsController < ApplicationController

  def destroy
    @client = current_user.clients.find(params[:id])
    first_name = @client.first_name
    if @client.destroy
      flash[:notice] = "\"#{first_name}\" was deleted successfully."
      redirect_to clients_path
    else
      flash[:error] = "There was an error deleting the client."
      render :show
    end

   def welcome 
     @client = current_user.clients.find(params[:id])

     content = "Welcome to MEDAPulse, #{@client.first_name}. Please save this number in your phone as #{@client.user.first_name}. I'll be texting you with reminders for your goals. Text back if you need help!"

     phone = @client.phone
     @text_message = @client.text_messages.build(text_message_params)
     @text_message.incoming_message = false
     @text_message.sentstatus = false

     if @text_message.scheduled_date == nil 
      @text_message.send_text_message(@text_message.content, @text_message.phone)
     end

     if (@text_message.save && (@text_message.sentstatus == true))
      flash[:notice] = "Success! Your welcome text is being sent now."
     else
      flash[:error] = "There was an error sending your welcome text. Please try again."
     end
  end
end

Client Show view (show.html.erb): 
<div class="row">

  <div class="col-xs-4">
        <%= render partial: "client_sidebar", object: @client %>
  </div>

    <div class="col-xs-8">
        <%= link_to "Create New Plan", new_client_action_plan_path(@client),
                                        class: 'btn btn-success' %>

        <%= link_to "Welcome Text", @client, method: :welcome, class: 'btn btn-info', 
data: { confirm: 'Are you sure you want to send a welcome text?' } %>

        <%= link_to "Delete Client", @client, method: :delete, class: 'btn btn-danger', 

data: { confirm: 'Are you sure you want to delete this client?' } %>

        <%= render @client.action_plans %>
  </div>

</div>

Text Message Model (text_message.rb):
require 'twilio-ruby'
require 'date'

class TextMessage < ActiveRecord::Base

  belongs_to :client, dependent: :destroy
  belongs_to :step, dependent: :destroy
  has_many :coach_emails

before_save :grab_phone

  def grab_phone
    self.phone = phone
  end

  def send_text_message(message, phone)

     twilio_sid = ENV["TWILIO_ACCT_SID"]
     twilio_token = ENV["TWILIO_AUTH_TOKEN"]
     twilio_phone_number = ENV["TWILIO_PHONE_NUMBER"]

    begin
      @twilio_client = Twilio::REST::Client.new(twilio_sid, twilio_token)

      @twilio_client.account.sms.messages.create(
        :from => "+1#{twilio_phone_number}",
        :to => phone,
        :body => message)

      rescue Twilio::REST::RequestError => e
        puts e.message
      end

    if e != "400" || e != "500"
      self.sentstatus = true
    end

    self.save!
  end  
end

Lastly, routes.rb file: 
Rails.application.routes.draw do

require 'sidekiq/web'

   devise_for :users, :path => '',
    :path_names => {:sign_in => 'login', :sign_out => 'logout'}, 
    :controllers => { registrations: 'registrations' }

   authenticate :user do
     mount Sidekiq::Web => '/sidekiq'
   end

   resources :clients do
     resources :action_plans, shallow: true, except: [:index] 
   end

   resources :action_plans, shallow: true, only: [] do
     resources :goals, shallow: true, except: [:index]
   end

   resources :goals, shallow: true, only: [] do
     resources :steps, shallow: true, except: [:index, :destroy]
   end

   resources :steps, shallow: true, only: [] do
     resources :text_messages, shallow: true, except: [:index, :destroy]
   end

   get "text_messages/receive"
   match '/receivetext' => 'text_messages#receive', :via => :post

   get 'about' => 'welcome#about'

   root to: 'welcome#index'

end

I suspect that destroy/delete is baked into my app already and that I need a custom route for sending a text message (welcome) from the Client controller. I tried adding several variations of a 'welcome' route for clients but that did not fix the issue. I'm used to adding routes to direct the users to a path but with sending a text message, I don't want to redirect the user, just invoke an action. I appreciate any advice. 

A:

Try this 
In View
 <%= link_to "Welcome Text", welcome_clients_path(@client), method: :post, class: 'btn btn-info', 
 data: { confirm: 'Are you sure you want to send a welcome text?' } %>

In routes
 resources :clients do
  collection do
    post :welcome
  end
 end

Hope it will work

