So Ive been working on a rails project that defines two different create actions in the same controller. Here's my controller:
class SmsSendsController < ApplicationController
def new
@at = SmsSend.new
@contact = Contact.find_by(id: params[:id])
end
def create
@at = SmsSend.create(sms_params)
if @at.save!
@con = current_user.contacts.find_by(id: @at.contact_id)
AfricasTalkingGateway.new("trial-error").sendMessage(@con.phonenumber, @at.message)
end
end
def new_all
@at = SmsSend.new
@contact = Contact.find_by(id: params[:id])
end
def create_all
@at = SmsSend.create(sms_params)
if @at.save!
current_user.contacts.each do |c|
AfricasTalkingGateway.new("trial-error").sendMessage(c.phonenumber, @at.message)
end
end
end
private
def sms_params
params.require(:sms_send).permit(:mobile, :message, :contact_id)
end
end
In my
routes.rb
file, Ive used both custom and resourceful routes to define routes for the first and the second new/create actions:
Rails.application.routes.draw do
devise_for :users
get 'sms_sends/new_all', to: 'sms_sends#new_all'
post 'sms_sends', to: 'sms_sends#create_all'
resources :contacts
resources :sms_sends
root 'contacts#index'
end
So both post actions will work if and only if its routes are placed before the other. Is there a way I can get rid of the precedence? Or where am I going wrong?
Thankie.