【问题标题】:Routing Error -- Triggering Controller Action via Button in Rails 4路由错误——通过 Rails 4 中的按钮触发控制器动作
【发布时间】:2015-02-24 17:42:33
【问题描述】:

我有客户,除此之外,我希望能够单击两个按钮 = 一个用于发送预先编写的文本消息,一个用于删除客户。

目前,

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

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

当我在客户端 #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

我怀疑destroy/delete 已经融入了我的应用程序,并且我需要一个自定义路由来从客户端控制器发送文本消息(欢迎)。我尝试为客户添加几种“欢迎”路线的变体,但这并没有解决问题。我习惯于添加路由以将用户定向到路径,但是通过发送文本消息,我不想重定向用户,只需调用一个操作。我很感激任何建议。

【问题讨论】:

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


    【解决方案1】:

    试试这个

    在视图中

     <%= 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
    

    希望能成功

    【讨论】:

    • Rajarshi,非常感谢您的帮助。我合并了您的更改,并且几乎就在那里。我最终需要使用以下行: @client.id), method: :post, class: 'btn btn-info', data: { confirm: '你确定要发送欢迎短信吗? } %> 在视图中让它接受客户端 ID。再次感谢!
    猜你喜欢
    • 2013-04-20
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    相关资源
    最近更新 更多