我必须遗漏一些基本的东西。我不知道为什么Rails试图为给定的路由执行错误的方法。我有一个控制器,而不是通过Twilio处理短信。
sms_controller.rb
class SmsController < ApplicationController
skip_before_filter :verify_authenticity_token
def send(message, number)
twilio_sid = ENV["TS_TWILIO_SID"]
twilio_token = ENV["TS_TWILIO_TOKEN"]
disclaimer = "\n\nReply HELP for help. Reply STOP to unsubscribe. Reply YES to resubscribe."
@twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token
@twilio_client.account.sms.messages.create(
:from => ENV["TS_TWILIO_PHONE_NUMBER"],
:to => number,
:body => message + disclaimer
)
end
def receive
message_body = sms_params[:body]
from_number = sms_params[:from]
SMSLogger.log_text_message from_number, message_body
end
private
def sms_params
params.require(:sms).permit(:body, :from)
end
end
在我的routes.rb文件中,我定义了一个用于处理从用户收到的短信的邮寄路线。
的routes.rb
require 'sidekiq/web'
<redacted>::Application.routes.draw do
authenticate :user, lambda { |u| u.admin? } do
mount Sidekiq::Web => '/sidekiq'
end
devise_for :users, controllers: {
registrations: 'registrations',
sessions: 'sessions',
passwords: 'passwords',
:omniauth_callbacks => "omniauth_callbacks"
}
get 'users/sign_up', to: redirect('/sign-up')
# Static Routes
get 'feed' => 'client#feed'
get 'share/:tag' => 'client#share'
get 'share/:tag/:id' => 'client#share'
get 'history' => 'client#history'
get 'favorites' => 'client#favorites'
get 'submissions' => 'client#submissions'
get 'bumps' => 'client#bumps'
get 'tag' => 'client#tag'
get 'bump/:ref_id' => 'client#bump'
#Enable Push
get 'push-enable/:user_id' => 'client#push_enable'
get 'push' => 'client#push'
# Beta access flow, ordered by good path
get 'code' => 'client#access_code'
get 'disclaimer' => 'client#disclaimer'
get 'terms' => 'client#terms'
get 'sign-up' => 'client#signup'
get 'resend' => 'client#resend_link'
get 'welcome' => 'client#welcome'
get 'selectusername' => 'client#username_select'
# Multi-step beta access flow
post 'confirm-beta' => 'client#confirm_beta_token'
post 'confirm-disclaimer' => 'client#disclaimer_agreement'
post 'confirm-terms' => 'client#terms_agreement'
post 'sms' => 'sms#receive'
# User routes
put 'user' => 'users#update'
post 'authentication/from-native' => 'users#from_native'
# JSON API
namespace :api do
get 'card/:id' => 'media#show'
# Media API
get 'media/:tag' => 'media#next'
get 'share/:tag/:id/:limit/:offset' => 'media#share_feed'
post 'media/:media_id/tags/:name/' => 'tags#create'
post 'media/:media_id/report' => 'media#report'
get 'media/:media_id/unreport' => 'media#remove_report'
# Tags API
get 'tags' => 'tags#tag_feed'
get 'tags/search' => 'tags#search'
get 'tags/:name' => 'tags#show'
# Vote API
get 'votes' => 'votes#show'
post 'votes/:vote/:id/tag/:tag' => 'media#create_vote'
# Users API
get 'users/:id/stats' => 'votes#stats'
get 'users' => 'users#stats'
get 'users/buddies' => 'users#buddies'
patch 'users/:id' => 'users#update'
get 'users/unsubscribe/:id/:type' => 'users#unsubscribe'
get 'users/check-name/:username' => 'users#check_username'
# History API
get 'history/paginated/:limit/:offset' => 'users#paginated_history'
get 'history/bracketed/:id' => 'users#bracketed_history'
get 'history/next/:id' => 'users#next_history'
get 'history/previous/:id' => 'users#previous_history'
# Favorites API
get 'favorites/paginated/:limit/:offset' => 'favorites#paginated_history'
get 'favorites/bracketed/:id' => 'favorites#bracketed_history'
get 'favorites/next/:id' => 'favorites#next_history'
get 'favorites/previous/:id' => 'favorites#previous_history'
post 'favorites/:card_id' => 'favorites#create'
delete 'favorites/:card_id' => 'favorites#delete'
# Referrals API
get 'referral/made/paginated/:limit/:offset' => 'referrals#paginated_collection_made'
get 'referral/received/paginated/:limit/:offset' => 'referrals#paginated_collection_received'
post 'referral/:card_id/:user_ids' => 'referrals#create'
post 'referral/:card_id/:referral_id' => 'referrals#bump'
get 'referral/seen/:referral_id' => 'referrals#seen'
#Bumps API
post 'bump/:media_id/:sharer_ids' => 'bumps#create'
get 'bump/seen/:bump_id' => 'bumps#seen'
end
get '/desktop' => 'client#desktop'
root to: "client#index"
end
出于某种原因,当我POST到这个路由时,它尝试执行send方法,并且我从rails获得了错误的参数数量(1对2)错误。为什么是这样?我的路由文件不能定义要接收的方法吗?
错误消息
Started POST "/sms" for 127.0.0.1 at 2015-04-25 18:09:22 -0700
ActiveRecord::SchemaMigration Load (0.6ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by SmsController#receive as */*
Parameters: {"Body"=>"\"hello\"", "From"=>"\"+16613328096\""}
Completed 500 Internal Server Error in 0ms
ArgumentError (wrong number of arguments (1 for 2)):
app/controllers/sms_controller.rb:4:in `send'
Rendered /Users/placey/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.6ms)
Rendered /Users/placey/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.7ms)
Rendered /Users/placey/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /Users/placey/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (24.2ms)
答案 0 :(得分:0)
所有Ruby对象都有一个方法send
,它继承自类Object
(http://ruby-doc.org/core-2.2.2/Object.html#method-i-send)。当您创建名为send
的操作时,您将覆盖它。
Rails中的一些代码使用1个参数调用send
(因为Object#send
可以接收任何参数)。但是当你覆盖它时,你的方法已被调用,并且它期望完全接收2个参数。
您需要将方法send
重命名为其他名称。