如何将控制器中定义的方法移动/转换为其自己的 restful 路由



我只想知道如何将我的方法从 get 请求移动到特定的控制器,在那里我可以定义它并在资源中使用它作为 RESTFUL 路由, 例如: 我有以下资源:

resources: send_sms
get :new_sms to, send_sms#new_sms

我也希望在我的资源中完成new_sms方法。 不想另外使用获取。

我的路线.rb

resources :send_sms, only: %i[index create]
get :new_sms, to: 'send_sms#new_sms'

在这里,我想将我的new_sms移动到资源不想使用这个.. 获取:new_sms,到:send_sms#new_sms

我的send_sms_controller.rb

class Admin::SendSmsController < AdminController
before_action :authenticate_admin!
before_action :user, only: %i[show edit new_sms kits_status]
before_action :users, only: :index
def show; end
def index
@users = users.search_by(params[:query]&.downcase)
end
#def new_sms; end

def create
client = Twilio::Client.new
message = { to: user.phone_number,
body: params[:message] }
client.send_message(message)
redirect_to admin_users_path
end
##################################################
my 
new_sms.slim
h2
="Send sms to #{@user.email}"
= simple_form_for(@user, url: admin_user_send_sms_path(@user), method: :post) do |f|
section
.form-inputs.column
= text_area_tag 'message',nil, placeholder: 'write message here ...', size: "35x10"
div#buttons
=f.submit 'Send', class: 'btn btn-success'
=link_to 'Cancel', admin_users_path, class: 'btn btn-danger'

您可以在资源中定义成员路由

resources :send_sms, only: %i[index create] do
member do
get :new_sms 
end
end

这将生成成员路由get '/send_sms/:id/new_sms', to: 'send_sms#new_sms'

还可以在资源中定义收集路由

resources :send_sms, only: %i[index create] do
collection do
get :new_sms 
end
end

这将生成收集路由get '/send_sms/new_sms', to: 'send_sms#new_sms'

相关内容

最新更新