路由错误 - 通过Rails中的按钮触发控制器操作4

时间:2014-12-28 04:00:28

标签: ruby-on-rails ruby button controller routes

我有客户端,除其他外,我希望能够点击两个按钮=一个发送预先写好的文本消息,一个删除客户端。

目前,

发送短信(通过控制器中的欢迎)=不起作用

删除客户端(通过控制器中的销毁)=完美运行

点击"欢迎文字"时的错误在客户#5的节目视图中是:

No route matches [POST] "/clients/5"

我可以确认在应用的其他部分发送短信

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

客户端显示视图( 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.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

最后, routes.rb 文件:

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

我怀疑破坏/删除已经存在于我的应用程序中,并且我需要一个自定义路由来从客户端控制器发送文本消息(欢迎)。我尝试添加一些欢迎&#39;客户的路线,但没有解决问题。我过去常常添加路由来引导用户访问路径但是发送短信时,我不想重定向用户,只需调用操作即可。我很感激任何建议。

1 个答案:

答案 0 :(得分:2)

试试这个

在视图中

 <%= 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?' } %>

路线

 resources :clients do
  collection do
    post :welcome
  end
 end

希望它能运作