Ruby on rails 路由错误--通过Rails 4中的按钮触发控制器操作
我有客户机,除其他外,我希望能够单击两个按钮=一个按钮发送预先编写的文本消息,另一个按钮删除客户机 目前, 发送文本消息(通过控制器中的“欢迎”)无效 删除客户端(通过控制器中的销毁)=工作正常 当我在客户端5的show视图上单击“欢迎文本”时,错误是: 我可以确认在应用程序的其他部分发送文本消息是否有效 客户机的相关部分\u controller.rb:Ruby on rails 路由错误--通过Rails 4中的按钮触发控制器操作,ruby-on-rails,ruby,button,controller,routes,Ruby On Rails,Ruby,Button,Controller,Routes,我有客户机,除其他外,我希望能够单击两个按钮=一个按钮发送预先编写的文本消息,另一个按钮删除客户机 目前, 发送文本消息(通过控制器中的“欢迎”)无效 删除客户端(通过控制器中的销毁)=工作正常 当我在客户端5的show视图上单击“欢迎文本”时,错误是: 我可以确认在应用程序的其他部分发送文本消息是否有效 客户机的相关部分\u controller.rb: class ClientsController < ApplicationController def destroy
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
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
我怀疑destroy/delete已经进入我的应用程序,并且我需要一个自定义路由来从客户端控制器发送文本消息(欢迎)。我尝试为客户添加几种不同的“欢迎”路线,但这并没有解决问题。我习惯于添加路由来将用户定向到路径,但通过发送文本消息,我不想重定向用户,只需调用一个操作。谢谢你的建议 试试这个
鉴于
<%= 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?' } %>
希望它能起作用Rajarshi,非常感谢您的帮助。我加入了你的变化,就快到了。最后,我需要在视图中使用以下行:@client.id),方法::post,类:'btn btn info',数据:{confirm:'您确定要发送欢迎文本吗?'}%>以使其接受客户端id。再次感谢!
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
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
<%= 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