Skyscanner Flight search API with Ruby on rails6 - ruby-on-rails

Hi i'm working on flight ticket website(search cheap flight ticket),using Skyscanner Flight API. Basically i copied this API tutorial to implement flight search function. However it only shows blank result in search.html.erb. In tutorial's form page you only fill out country name in form using different API. I'm stuck now and wonder how to implement search action & find_ticket action to take several parameter from index page's form and shows result properly.
Environment Rails:6.02 Ruby:2.65 RapidApi:skyscannerFlightSearch(i'm using RakutenRapidAPI)
Routes.rb
Rails.application.routes.draw do
root to: 'travel#index'
get '/search' => 'travel#search'
resources :messages
end
travel_controller.rb
class TravelController < ApplicationController
def index
end
def search
tickets = find_ticket(params[:country])
unless tickets
flash.now[:alert] = 'ticket not found'
return render action: :index
end
#ticket = tickets.first
end
private
def find_ticket(name)
request_api(
"https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/#{URI.encode(name)}"
)
end
def request_api(url)
response = Excon.get(
url,
headers: {
'X-RapidAPI-Host' => URI.parse(url).host,
'X-RapidAPI-Key' => Rails.application.credentials.rapid[:XXXX_key]
}
)
return nil if response.status != 200
JSON.parse(response.body)
end
end
Views/travel/index.html.erb
<div class="container">
<div class="row">
<div class="col-lg-12 mt-5">
<% if flash[:alert] %>
<div class="alert alert-warning"><%= flash[:alert] %></div>
<% end %>
<div class="mx-auto mt-5" style="width: 400px">
<%= form_with(url: search_path, method: 'get', local: true) do %>
<div class="form-group">
<%= label_tag :country, 'country '%>
<%= text_field_tag :country, nil, placeholder: 'US', class: 'form-control' %>
<%= label_tag :currency, 'currency'%>
<%= text_field_tag :currency, nil, placeholder: 'USD', class: 'form-control' %>
<%= label_tag :locale, 'locale'%>
<%= text_field_tag :locale, nil, placeholder: 'en-US', class: 'form-control' %>
<%= label_tag :originplace, 'originplace'%>
<%= text_field_tag :originplace, nil, placeholder: 'SFO-sky', class: 'form-control' %>
<%= label_tag :destinationplace, 'destinationplace'%>
<%= text_field_tag :destinationplace, nil, placeholder: 'JFK-sky', class: 'form-control' %>
<%= label_tag :outbounddate, 'outbounddate'%>
<%= text_field_tag :outbounddate, nil, placeholder: '2020-02-01', class: 'form-control' %>
</div>
<%= button_tag 'Search', class: 'btn btn-success btn-block' %>
<% end %>
</div>
</div>
</div>
</div>
views/travel/search.html.erb
<div class="container">
<div class="row mt-5">
<div class="col-lg-6">
<h4>Ticket info</h4>
<dl>
<dt>PlaceId</dt>
<dd><%= #ticket['PlaceId'] %></dd>
<dt>lataCode</dt>
<dd><%= #ticket['IataCode'] %></dd>
<dt>Name</dt>
<dd><%= #ticket['Name'] %></dd>
<dt>SkyscannerCode</dt>
<dd><%= #ticket['SkyscannerCode'] %></dd>
<dt>CityName</dt>
<dd><%= #ticket['CityName'] %></dd>
</dl>
</div>
</div>
</div>
</div>

Related

Date not saving within parameters of model

within my view form, I currently have this code:
<div class="panel panel-default">
<div class= "panel-body">
<%= form_for #meal_plan, url: new_meal_plan_path, method: "GET", html: { class: "form-inline" } do |form| %>
<div class="form_group">
<%= form.label :start_date %>
<%= form.text_field :start_date, type: "date", class: "form-control" %>
</div>
<div class="form_group">
<%= form.label :end_date %>
<%= form.text_field :end_date, type: "date", class: "form-control" %>
</div>
<div class="form-group action">
<%= form.submit "Generate Plan", class: "btn btn-default"%>
</div>
<% end %>
</div>
</div>
That takes a start date and an end date input for a meal plan. In the new method within the meal_plan_controller, I have:
def new
puts params[:start_date]
puts params[:end_date]
#meal_plan = current_user.meal_plans.build(
start_date: params[:start_date] || Date.today,
end_date: params[:end_date] || 6.days.from_now.to_date,
)
#meal_plan.build_meals
end
In the puts statements that I wrote to debug, :start_date and :end_date contains only the default dates, so my guess is that the dates are not saving when the user hits submit, but I can't figure out why.

I am working on Ruby on rails but it is giving undefined method `errors' for nil:NilClass error

Don't know why my app is throwing error on the registration file.Plz let me know the error.Thank you very much
Error
Showing /home/punisher/Desktop/billboard/app/views/register/signup.html.erb where line #36 raised:
undefined method `errors' for nil:NilClass
Extracted source (around line #36):
<h1 class="Login-here">Register Here</h1>
<%= form_with model: #owner,url:register_path, local: true do |form| %>
<% if #owner.errors.any? %>
<div class="alert alert-danger">
<% #owner.errors.full_messages.each do |message| %>
<div> <%= message %> </div>
Register_controller
class RegisterController < ApplicationController
def new
#owner=Owner.new
end
def create
# render plain: "Thanks for registering!"
# render plain: params[:user]
#owner=Owner.new(owner_params)
if #owner.save
session[:owner_id] = #owner.id
redirect_to login_path
else
flash[:error] ="User Not Registered"
render :signup
end
end
private
def owner_params()
params.permit(:email, :company_name, :address, :contact_no, :password, :password_confirmation)
end
end
signup.erb.html
<body style = "background-color:rgb(92, 179, 196)">
<div class="tnavbar">
<div class="tlogo">
<%= image_tag('1.png', :class => "logoimg") %>
<h3 class="title"> Out of Home</h3>
</div>
<div>
<%= link_to "login", login_path, class:"home-btn"%>
</div>
</div>
<div class="container-fluid" style="margin-top:50px;">
<div class="row loginpagerow">
<div class="col-1">
</div>
<div class="logincol col-5 imagebox">
<h1 class="headinglogin">Bringing you the best of the best.</h1>
<%= image_tag('loginpageimage.png', :class => "img") %>
</div>
<div class="col-5 logincol loginbox">
<h1 class="Login-here">Register Here</h1>
<%= form_with model: #owner,url:register_path, local: true do |form| %>
<% if #owner.errors.any? %>
<div class="alert alert-danger">
<% #owner.errors.full_messages.each do |message| %>
<div> <%= message %> </div>
<% end %>
</div>
<% end %>
<div class="row" style="margin-top:30px">
<div style="width:45%; height:35vh; margin-left:25px;">
<%= form.email_field :email ,class: "form-control login-email-field signup-fields", placeholder:"Email" %>
<%= form.text_field :company_name ,class: "form-control login-email-field signup-fields", placeholder:"Company Name" %>
<%= form.text_field :address ,class: "form-control login-email-field signup-fields", placeholder:"Address" %>
</div>
<div style="width:45%; height:35vh; margin-left:25px;">
<%= form.telephone_field :contact_no ,class: "form-control login-email-field signup-fields", placeholder:"Contact No" %>
<%= form.password_field :password ,class: "form-control login-email-field signup-fields", placeholder:"password" %>
<%= form.password_field :password_confirmation,class: "form-control login-email-field signup-fields", placeholder:"Confirm password" %>
</div>
<div class="mb-3 login-input">
<%= form.submit "Signup" ,class: "sign-btn btn btn-primary"%>
</div>
</div>
<% end %>
</div>
<div class="col-1">
</div>
</div>
</div>
</body>
You have a app/views/register/signup.html.erb view and Rails expects signup method in your registrations_controller and is looking for #owner
instance method but you don't have signup method that's why it's raising an error because #owner is nil.
def signup
#owner = Owner.new(owner_params)
...
end

Can't seem to submit collection_select and receiving Unpermitted parameter: :hero_id

Sorry for the messy post, my first time posting. I have been trying to get this collection submit to work, but every time I press create report button I have it goes back to the screen and puts out Unpermitted parameter: :hero_id in the rails server terminal.
Model
class Report < ApplicationRecord
validates :subject, presence: true, length: { minimum: 6, maximum: 100 }
validates :description, presence: true, length: { minimum: 10, maximum: 300 }
belongs_to :requester
has_and_belongs_to_many :heros
end
View/Form
<div class="container">
<div class="row justify-content-center">
<div class="col-10">
<% if #report.errors.any? %>
<h2>The following errors prevented the article from being saved</h2>
<ul>
<% #report.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= form_with(model: #report, class: "shadow p-3 mb-3 bg-dark rounded", local: true) do |f| %>
<div class="form-group row">
<%= f.label :subject, class: "col-2 col-form-label text-light" %>
<div class="col-10">
<%= f.text_field :subject, class: "form-control shadow rounded", placeholder: "Subject of Report" %>
</div>
</div>
<div class="form-group row">
<%= f.label :description, class: "col-2 col-form-label text-light" %>
<div class="col-10">
<%= f.text_area :description, rows: 10, class: "form-control shadow rounded", placeholder: "Description of Issue" %>
</div>
</div>
<div class="form-group row">
<%= f.label :hero, class: "col-2 col-form-label text-light" %>
<div class="col-10">
<%= f.collection_select(:hero_ids, Hero.all, :id, :hero_name, {prompt: "Select a Hero"}, {:required => true}) %>
</div>
</div>
<div class="btn-toolbar p-2 mb-2 row justify-content-center">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
</div>
<div class="mb-3">
<%= link_to '[ Cancel and return to reports listing ]', reports_path, class: "text-info" %>
</div>
</div>
</div>
Controller
def report_params
#byebug
params.require(:report).permit(:subject, :description, hero_ids: [])
end
Console
(byebug) params.require(:report)
<ActionController::Parameters {"subject"=>"Test report", "description"=>"Test report", "hero_ids"=>"1"} permitted: false>

Twilio Rails Integration for Devise and Milia

Firstly sorry for a newbie question. Just started developing in rails around 15 days back. Now I have installed Milia, Devise and they handle all the work for signing up, logging in, and entering the customer data into the DB.
What am I trying to achieve?
I want to send an SMS to a customer, whose mobile no, we will be taking using a data entry form, whose code I have shown below.
Now once all the information is entered, and the create customer button is pressed, all the information is entered into the DB. Once the button is pressed, I want an SMS to be sent via Twilio, to the customer with a defined body.
For this, I have created a controller, an SMS.rb file and a form which has the code which should help in firing the SMS.
FORM for Entering the Information:
<%= form_for [#sms, #tenant, #customer], :html => { :class => "form-horizontal customer" }, :id => 'form1' do |f| %>
<% if #customer.errors.any? %>
<div id="error_expl" class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><%= pluralize(#customer.errors.count, "error") %> prohibited this customer from being saved:</h3>
</div>
<div class="panel-body">
<ul>
<% #customer.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<h3 align=centre>Personal Infomation</h3>
<div class="form-group">
<%= f.label :name, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :name, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :date_of_birth, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :dob, :class => 'form-control datepicker' %>
</div>
</div>
<div class="form-group">
<%= f.label :mobileno, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :mobileno, :class => 'form-control', :id =>"mobileno", onblur:"Calculate()" %>
</div>
</div>
<div class="form-group">
<%= f.label :email_Id, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :emailid, :class => 'form-control' %>
</div>
</div>
<H3 align=centre>Address</H3>
<div class="form-group">
<%= f.label :building, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :building, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :sub_area, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :subarea, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :locality, :class => 'control-label col-md-2'%>
<div class="col-lg-10">
<%= f.text_field :locality, :class => 'form-control', :id => "locality", onblur: "Calculate()" %>
</div>
</div>
<div class="form-group">
<%= f.label :pincode, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :pin, :class => 'form-control' %>
</div>
</div>
<h3 align=left>Medical Information</h3>
<div class="form-group">
<%= f.label :blood_group, :class => 'control-label col-lg-2' %>
<div class="col-lg-10">
<%= f.text_field :bg, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :diseases, :class => 'control-label col-lg-2' %>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'Diabetes', checked('Diabetes'), id: 'diseases_diabetes'%>
<%= label_tag 'diseases_diabetes', 'Diabetes' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'Cancer', checked('Cancer'), id: 'diseases_cancer'%>
<%= label_tag 'diseases_cancer', 'Cancer' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'TB', checked('TB'), id: 'diseases_tb'%>
<%= label_tag 'diseases_tb', 'TB' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'BP', checked('BP'), id: 'diseases_tb'%>
<%= label_tag 'diseases_bp', 'BP' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'HIV', checked('TB'), id: 'diseases_tb'%>
<%= label_tag 'diseases_hiv', 'HIV' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'Brain Tumor', checked('Brain Tumor'), id: 'diseases_tb'%>
<%= label_tag 'diseases_brain_tumor', 'Brain Tumor' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'STD', checked('STD'), id: 'diseases_tb'%>
<%= label_tag 'diseases_std', 'STD' %>
</div>
</div>
<div class="form-group">
<%= f.label :medicine, :class => 'control-label col-md-2 col-sm-3 col-xs-4' %>
<div class="col-lg-10">
<%= f.text_field :medicin, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :allergy, :class => 'control-label col-md-2 col-sm-3 col-xs-4' %>
<div class="col-lg-10">
<%= f.text_field :allergy, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :uid, :class => 'control-label col-md-2 col-sm-3 col-xs-4'%>
<div class="col-lg-10">
<%= f.text_field :uid , :class => 'form-control', :id => 'uid' %>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<br>
<%= f.hidden_field :tenant_id, value: params[:tenant_id], :class => 'form-control' %>
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
root_path, :class => 'btn btn-default' %>
</div>
</div>
<% end %>
SMS.rb FILE:
class SMS < ActiveRecord::Base
def client
Twilio::REST::Client.new Rails.application.secrets.twilio_account_sid, Rails.application.secrets.twilio_token
end
acct_sid = "ACCOUNT_SID"
auth_token = "AUTH_TOKEN"
twilio_no = "+16xxxxxxxxx"
acct_sid = ENV['twilio_account_sid']
auth_token = ENV['twilio_token']
twilio_no = ENV['twilio_no']
def send
numberto = params[:mobileno]
client.account.messages.create(
:messaging_service_sid => Rails.application.secrets.twilio_messaging_service_sid,
:from => "+1xxxxxxxxxx",
:to => "#{numberto}",
:body => "Hi. Thanks a lot for signing up with us. Your UID is: #{uid}"
)
end
The problem I am facing:
I cannot add #sms to the form as it has [#tenant, #cusotmer] already. Adding #sms, gives me an error.
I am frantically stuck and don't know how to proceed. Can you guys help me out?
Things to Know
1. Using Rails 4.2.6
2. Using Ruby 2.3.3
UPDATES
I have updated the Form.html.erb file now.
Here it is:
<%= form_for [#tenant, #customer], :html => { :class => "form-horizontal customer" }, :id => 'form1' do |f| %>
<% if #customer.errors.any? %>
<div id="error_expl" class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><%= pluralize(#customer.errors.count, "error") %> prohibited this customer from being saved:</h3>
</div>
<div class="panel-body">
<ul>
<% #customer.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<h3 align=centre>Personal Infomation</h3>
<div class="form-group">
<%= f.label :name, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :name, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :date_of_birth, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :dob, :class => 'form-control datepicker' %>
</div>
</div>
<div class="form-group">
<%= f.label :mobileno, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :mobileno, :class => 'form-control', value: params[:mobile], :id =>"mobileno", onblur:"Calculate()" %>
</div>
</div>
<div class="form-group">
<%= f.label :email_Id, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :emailid, :class => 'form-control' %>
</div>
</div>
<H3 align=centre>Address</H3>
<div class="form-group">
<%= f.label :building, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :building, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :sub_area, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :subarea, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :locality, :class => 'control-label col-md-2'%>
<div class="col-lg-10">
<%= f.text_field :locality, :class => 'form-control', :id => "locality", onblur: "Calculate()" %>
</div>
</div>
<div class="form-group">
<%= f.label :pincode, :class => 'control-label col-md-2' %>
<div class="col-lg-10">
<%= f.text_field :pin, :class => 'form-control' %>
</div>
</div>
<h3 align=left>Medical Information</h3>
<div class="form-group">
<%= f.label :blood_group, :class => 'control-label col-lg-2' %>
<div class="col-lg-10">
<%= f.text_field :bg, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :diseases, :class => 'control-label col-lg-2' %>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'Diabetes', checked('Diabetes'), id: 'diseases_diabetes'%>
<%= label_tag 'diseases_diabetes', 'Diabetes' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'Cancer', checked('Cancer'), id: 'diseases_cancer'%>
<%= label_tag 'diseases_cancer', 'Cancer' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'TB', checked('TB'), id: 'diseases_tb'%>
<%= label_tag 'diseases_tb', 'TB' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'BP', checked('BP'), id: 'diseases_tb'%>
<%= label_tag 'diseases_bp', 'BP' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'HIV', checked('TB'), id: 'diseases_tb'%>
<%= label_tag 'diseases_hiv', 'HIV' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'Brain Tumor', checked('Brain Tumor'), id: 'diseases_tb'%>
<%= label_tag 'diseases_brain_tumor', 'Brain Tumor' %>
</div>
<div class="col-md-2 col-sm-3 col-xs-4">
<%= check_box_tag 'customer[diseases][]', 'STD', checked('STD'), id: 'diseases_tb'%>
<%= label_tag 'diseases_std', 'STD' %>
</div>
</div>
<div class="form-group">
<%= f.label :medicine, :class => 'control-label col-md-2 col-sm-3 col-xs-4' %>
<div class="col-lg-10">
<%= f.text_field :medicin, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :allergy, :class => 'control-label col-md-2 col-sm-3 col-xs-4' %>
<div class="col-lg-10">
<%= f.text_field :allergy, :class => 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :uid, :class => 'control-label col-md-2 col-sm-3 col-xs-4'%>
<div class="col-lg-10">
<%= f.text_field :uid , :class => 'form-control', :id => 'uid' %>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<br>
<%= f.hidden_field :tenant_id, value: params[:tenant_id], :class => 'form-control' %>
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
root_path, :class => 'btn btn-default' %>
</div>
</div>
<% end %>
I have also updated the Customer.rb file:
Here it is:
def format_phone_number
self.to = "+91" + (self.to.gsub('-',''))
end
def client
Twilio::REST::Client.new Rails.application.secrets.twilio_account_sid, Rails.application.secrets.twilio_token
end
acct_sid = "Axxxxxxxxxxxx"
auth_token = "6xxxxxxxxxxx"
twilio_no = "+1xxxxxxxxxxx"
acct_sid = ENV['twilio_account_sid']
auth_token = ENV['twilio_token']
twilio_no = ENV['twilio_no']
def send (to,body)
client.account.messages.create(
:messaging_service_sid => Rails.application.secrets.twilio_messaging_service_sid,
:to => '#{mobileno}',
:body => "Hi. Thanks a lot for signing up with us. Your UID is: #{uid}"
)
end
Instead of writing in the sms.rb file, I have written the code in customer.rb and erased the sms.rb file and now it is connecting well to the Twilio API. But it takes a random number as the to number +66245366 and so the message sending fails. I don't know why this is happening.
Latest Updated
Customer.rb
class Customer < ActiveRecord::Base
before_save do
self.diseases.gsub!(/[\[\]\"]/,"") if attribute_present?("diseases")
end
belongs_to :tenant
validates_uniqueness_of :mobileno
def self.by_plan_and_tenant(tenant_id)
tenant = Tenant.find(tenant_id)
if tenant.plan == 'free'
tenant.customers
else
tenant.customers
end
end
def client
Twilio::REST::Client.new Rails.application.secrets.twilio_account_sid, Rails.application.secrets.twilio_token
end
acct_sid = "Axxxxxxxxxxxxxx"
auth_token = "6cxxxxxxxxxxxxxxxxxxxxx"
twilio_no = "+16xxxxxxxxx"
acct_sid = ENV['twilio_account_sid']
auth_token = ENV['twilio_token']
twilio_no = ENV['twilio_no']
def send (to, body)
client.account.messages.create(
:messaging_service_sid => Rails.application.secrets.twilio_messaging_service_sid,
:from => "+1xxxxxxxxx1",
:to => '#{mobileno}',
:body => "Hi. Thanks a lot for signing up with us. Your UID is: #{uid}"
)
end
end
Twilio developer evangelist here.
After a long chat we changed the method name to send SMS messages from send (which is an important method for objects in Ruby) to send_sms.
Then we ensured the number had the right international code.
def send_sms()
client.account.messages.create(
:messaging_service_sid => Rails.application.secrets.twilio_messaging_service_sid,
:from => "+16312010201",
:to => "#+91{mobileno}",
:body => "Hi. Thanks a lot for signing up with us. Your UID is: #{uid}"
)
end
We then updated the Customers controller create action to send the SMS when the customer was successfully created:
def create
#customer = Customer.new(customer_params)
respond_to do |format|
if #customer.save
format.html {
#customer.send_sms
redirect_to root_url, notice: 'Customer data was successfully created.'
}
else
format.html { render :new }
end
end
end
So things work now.

Rails - getting the value out from the view form into a controller

In my form the user has to select a category for his post.
<div class="field form-group">
<%= f.text_field :category, class: "form-control" %>
</div>
(Currently I am using text-input instead of a drop-down list)
Posts belong to categories
#post = #category.posts.build(post_params)
However I can't understand how to get the category value out of that field.
I have tried passing a number, to find_by id, and string to find_by name.
#category = Category.find(params[:category]) #returns no Categoy with nil id
#category = Category.find_by(name: params[:category]) #returns no method error
any help would be appreciated
Edit:
Form code
<%= form_for [#company, #post], :html => { :class => "form-posts"} do |f| %>
<div class="field form-group">
<div class="input-group input-group-lg">
<span class="input-group-addon">$</span>
<%= f.text_field :text, class: "form-control", placeholder: "text", required: true %>
</div>
</div>
<div class="field form-group">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-calendar"></i>
</span>
<%= f.text_field :date,
class: "form-control",
value: #today,
data: {behaviour: "datepicker"},
required: true %>
</div>
</div>
<div class="field form-group">
<%= f.text_field :comment, class: "form-control", placeholder: "Comment (optional)" %>
</div>
<div class="field form-group">
<%= f.text_field :category, class: "form-control" %>
</div>
<div class="actions"><%= f.submit 'Add', class: "btn btn-lg btn-primary" %></div>
<% end %>
Edit 2
controller:
form:
<%= f.text_field :category, class: "form-control" %>
def create
#company = current_user.companies.find(params[:company_id])
#category = #company.categories(params[:category])
#post = #category.posts.build(post_params)
debugger:
{"utf8"=>"✓", "authenticity_token"=>"...", "transaction"=>{"text"=>"lalala", "date"=>"11.11.2013", "comment"=>"", "category"=>"5"}, "commit"=>"Add post", "action"=>"create", "controller"=>"posts", "company_id"=>"2"}
undefined method `transactions'
Take a look at the development log.
Probably it will be something like params[:post][:category]
Get your category like this:
params[:post][:category]

Resources