I cant download the files which i was uploaded using carrierwave - ruby-on-rails

previously i was able to download whwn i uploaded single file, but when i changed it to multiple file upload i was unabel to download please help me out
here is my controller
invoice_details_controller.rb
class InvoiceDetailsController < ApplicationController
before_action :set_invoice_detail, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#invoice_details = InvoiceDetail.all
respond_with(#invoice_details)
end
def show
#invoice_detail = InvoiceDetail.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #invoice_detail }
format.pdf { render :layout => false }
end
end
def new
#invoice_detail = InvoiceDetail.new
respond_with(#invoice_detail)
end
def edit
end
def create
#invoice_detail = InvoiceDetail.new(invoice_detail_params)
respond_to do |format|
if #invoice_detail.save
format.html { redirect_to invoice_details_path, notice: 'Invoice was successfully created.' }
else
format.html { render action: "new" }
end
end
end
def update
#invoice_detail.update(invoice_detail_params)
redirect_to invoice_details_path
end
def destroy
#invoice_detail.destroy
redirect_to invoice_details_path
end
def download
respond_to do |format|
format.html {
if params[:invoice_id].nil?
redirect_to :back
else
begin
invoice = InvoiceDetail.find(params[:invoice_id])
attachment_file = File.join('public', invoice.attachment.url)
if File.exists?(attachment_file)
send_file attachment_file, :disposition => 'attachment'
else
redirect_to :back
end
rescue Exception => error
redirect_to :back
end
end
}
end
end
private
def set_invoice_detail
#invoice_detail = InvoiceDetail.find(params[:id])
end
def invoice_detail_params
params.require(:invoice_detail).permit(:attachment,:invoice_number, :supplier_name, :invoice_date, :invoice_due_date, :description_of_goods, :quatity, :price_per_unit, :total_amount, :mode_of_payment, :status, :shipping_country)
end
end
here are my models
invoice_details.rb
class InvoiceDetail < ActiveRecord::Base
has_many :attachment
accepts_nested_attributes_for :attachment
end
invoice_files.rb
class InvoiceFile < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
belongs_to :InvoiceDetail
validates :name, presence: true # Make sure the owner's name is present.
end
here is my form
_form.html.erb
<%= form_for(#invoice_detail , html: {class: 'form-horizontal', role: 'form' }) do |f| %>
<% if #invoice_detail.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#invoice_detail.errors.count, "error") %> prohibited this invoice_detail from being saved:</h2>
<ul>
<% #invoice_detail.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :invoice_number, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :invoice_number, :class => 'text_field', :required => true, :maxlength => 15, :placeholder =>'15 Alpha Numeric with special Characters', :presence => "check invoice num" %>
</div>
</div>
<div class="field">
<%= f.label :supplier_name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :supplier_name, :class => 'text_field', :required => true,:maxlength => 20, :placeholder => '20 Alpha numeric characters' %>
</div>
</div>
<div class="field">
<%= f.label :invoice_date, :class => 'control-label' %>
<div class="controls1">
<%= f.date_select :invoice_date, :class => 'text_area' %>
</div>
</div>
<div class="field">
<%= f.label :invoice_due_date, :class => 'control-label' %>
<div class="controls1">
<%= f.date_select :invoice_due_date, :class => 'text_area' %>
</div>
</div>
<div class="field">
<%= f.label :description_of_goods, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :description_of_goods, :class => 'text_area', :maxlength => 50, :placeholder => 'Accepts up to 50 Alpha Numeric with special Characters'%>
</div>
</div>
<div class="field">
<%= f.label :quality, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :quatity , :class => 'text_area',:maxlength => 10, :placeholder =>'Accepts up to 10 Digits'%>
</div>
</div>
<div class="field">
<%= f.label :price_per_unit, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :price_per_unit, :class => 'text_area',:maxlength => 15, :placeholder =>'Accepts up to 15 Digits' %>
</div>
</div>
<div class="field">
<%= f.label :total_amount, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :total_amount, :class => 'text_area', :maxlength => 15, :placeholder =>'Accepts up to 15 Digits' %>
</div>
</div>
<div class="field">
<%= f.label :mode_of_payment, :class => 'control-label' %>
<div class="controls">
<%= f.select :mode_of_payment, options_for_select(%w[Cash Cheque Transfer LC]), :class => 'text_area', :placeholder => 'Select mode of payment' %>
</div>
</div>
<div class="field">
<%= f.label :status, :class => 'control-label' %>
<div class="controls">
<%= f.select :status, options_for_select(%w[Paid Pending]), :class => 'text_area' %>
</div>
</div>
<div class="field">
<%= f.label :shipping_country, :class => 'control-label' %>
<div class="controls">
<%= f.country_select :shipping_country, {priority: %w(ID US CA), prompt: 'Please select a country'}, :class => 'text_area' %>
</div>
</div>
<div class="control-group">
<%= f.label :attachment , :class => 'control-label' %>
<div class="controls">
<%= f.file_field :avatar, :multiple => true, name: "attachment[avatar][]", :class => 'file_field', :required => true %>
</div>
</div>
<div class="form-actions1">
<%= f.submit :class => 'btn btn-primary' %>
<%#= f.submit "cancel", :class => 'btn btn-danger', method: :delete, data: { confirm: 'Are you sure you want to cancel' } %>
<%= form_for(#invoice_detail , html: {class: 'form-horizontal', role: 'form' }) do |f| %>
<% if #invoice_detail.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#invoice_detail.errors.count, "error") %> prohibited this invoice_detail from being saved:</h2>
<ul>
<% #invoice_detail.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :invoice_number, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :invoice_number, :class => 'text_field', :required => true, :maxlength => 15, :placeholder =>'15 Alpha Numeric with special Characters', :presence => "check invoice num" %>
</div>
</div>
<div class="field">
<%= f.label :supplier_name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :supplier_name, :class => 'text_field', :required => true,:maxlength => 20, :placeholder => '20 Alpha numeric characters' %>
</div>
</div>
<div class="field">
<%= f.label :invoice_date, :class => 'control-label' %>
<div class="controls1">
<%= f.date_select :invoice_date, :class => 'text_area' %>
</div>
</div>
<div class="field">
<%= f.label :invoice_due_date, :class => 'control-label' %>
<div class="controls1">
<%= f.date_select :invoice_due_date, :class => 'text_area' %>
</div>
</div>
<div class="field">
<%= f.label :description_of_goods, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :description_of_goods, :class => 'text_area', :maxlength => 50, :placeholder => 'Accepts up to 50 Alpha Numeric with special Characters'%>
</div>
</div>
<div class="field">
<%= f.label :quality, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :quatity , :class => 'text_area',:maxlength => 10, :placeholder =>'Accepts up to 10 Digits'%>
</div>
</div>
<div class="field">
<%= f.label :price_per_unit, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :price_per_unit, :class => 'text_area',:maxlength => 15, :placeholder =>'Accepts up to 15 Digits' %>
</div>
</div>
<div class="field">
<%= f.label :total_amount, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :total_amount, :class => 'text_area', :maxlength => 15, :placeholder =>'Accepts up to 15 Digits' %>
</div>
</div>
<div class="field">
<%= f.label :mode_of_payment, :class => 'control-label' %>
<div class="controls">
<%= f.select :mode_of_payment, options_for_select(%w[Cash Cheque Transfer LC]), :class => 'text_area', :placeholder => 'Select mode of payment' %>
</div>
</div>
<div class="field">
<%= f.label :status, :class => 'control-label' %>
<div class="controls">
<%= f.select :status, options_for_select(%w[Paid Pending]), :class => 'text_area' %>
</div>
</div>
<div class="field">
<%= f.label :shipping_country, :class => 'control-label' %>
<div class="controls">
<%= f.country_select :shipping_country, {priority: %w(ID US CA), prompt: 'Please select a country'}, :class => 'text_area' %>
</div>
</div>
<div class="control-group">
<%= f.label :attachment , :class => 'control-label' %>
<div class="controls">
<%= f.file_field :avatar, :multiple => true, name: "attachment[avatar][]", :class => 'file_field', :required => true %>
</div>
</div>
<div class="form-actions1">
<%= f.submit :class => 'btn btn-primary' %>
<%#= f.submit "cancel", :class => 'btn btn-danger', method: :delete, data: { confirm: 'Are you sure you want to cancel' } %>
<%= link_to "Cancel", invoice_details_path(), :class => 'btn btn-danger', data: { confirm: 'Are you sure you want to cancel' } %>
</div>
<% end %>
<%= link_to "Cancel", invoice_details_path(), :class => 'btn btn-danger', data: { confirm: 'Are you sure you want to cancel' } %>
</div>
<% end %>

you just need a valid link to download any asset.For paperclip,this is how i download the asset(image/audio/video)
def download_video
#video = Video.find params[:id]
redirect_to #video..avatar.expiring_url(10)
end

Related

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 4 Nested Attributes has Unpermitted parameter error

Below is my api model:
class Api < ActiveRecord::Base
validates_presence_of :name
belongs_to :service
has_many :statuses
accepts_nested_attributes_for :statuses, reject_if: proc { |attributes| attributes['name'].blank? }
end
Below is my description model:
class Description < ActiveRecord::Base
validates_presence_of :value
belongs_to :status
end
Below is my status model:
class Status < ActiveRecord::Base
belongs_to :api
has_many :descriptions
accepts_nested_attributes_for :descriptions, reject_if: proc { |attributes| attributes['value'].blank? }
end
And below is my new and create action of controller:
def new
#api = Api.new
#status = #api.statuses.new
#status.descriptions.new
end
def create
#api = Api.new(api_params)
if #api.save
flash[:info] = request.original_url + ".do?apiname=" + "#{#api.name}"
redirect_to root_path
else
#api.statuses.new
render :new
end
private
def api_params
params.require(:api).permit(:name, statuses_attributes: [:name, descriptions_attributes:[:value]])
end
Below is my new template:
<div class="form-horizontal">
<%= form_for #api, :url => commons_path do |f| %>
<div class="form-group">
<%= f.label :name, "API Name", class: "col-sm-2 control-label" %>
<div class="col-sm-8">
<%= f.text_field :name, class: "form-control" %>
</div>
</div>
<%= f.fields_for :statuses do |status| %>
<div class="form-group">
<%= status.label :name, "Status", class: "col-sm-2 control-label" %>
<div class="col-sm-8">
<%= status.text_field :name, class: "form-control" %>
</div>
</div>
<%= f.fields_for :description do |description| %>
<div class="form-group">
<%= description.label :value, "Body", class: "col-sm-2 control-label" %>
<div class="col-sm-8">
<%= description.text_area :value, class: "form-control", rows: 12, cols: 65 %>
</div>
</div>
<% end %>
<% end %>
<%= f.submit("Create Data", class: 'btn btn-primary col-sm-offset-2') %>
<%= link_to "Cancel", root_path, class: "btn btn-danger" %>
<% end %>
After I create new data.It seems like only description's value did not save to my data base. and find an error "Unpermitted parameter: description" in my console.
Any one know what happen?
<div class="form-horizontal">
<%= form_for #api, :url => commons_path do |f| %>
<div class="form-group">
<%= f.label :name, "API Name", class: "col-sm-2 control-label" %>
<div class="col-sm-8">
<%= f.text_field :name, class: "form-control" %>
</div>
</div>
<%= f.fields_for :statuses do |status| %>
<div class="form-group">
<%= status.label :name, "Status", class: "col-sm-2 control-label" %>
<div class="col-sm-8">
<%= status.text_field :name, class: "form-control" %>
</div>
</div>
<%= status.fields_for :descriptions do |description| %>
<div class="form-group">
<%= description.label :value, "Body", class: "col-sm-2 control-label" %>
<div class="col-sm-8">
<%= description.text_area :value, class: "form-control", rows: 12, cols: 65 %>
</div>
</div>
<% end %>
<% end %>
<%= f.submit("Create Data", class: 'btn btn-primary col-sm-offset-2') %>
<%= link_to "Cancel", root_path, class: "btn btn-danger" %>
<% end %>

Turbolinks and rails

I can't submit my post without refreshing edit page, I am using turbolinks gem and simple_form here is the source code:
<div class = "row">
<div class = "col-lg-9 col-md-8">
<%= simple_form_for #post do |f| %>
<div class = "form-group">
<%= f.input :title, :label => false %>
<%= f.input :content, :as => :ckeditor, :input_html => { :ckeditor => {:toolbar => 'MyToolbar', :height => 465} } %>
<%= f.input :cat_list, :label => "Type your tags here" %>
</div>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
</div>
<div class = "col-lg-3 col-md-4 ">
<div class = "form-group" data-no-turbolink >
<p>
<%= image_tag(#post.thumbnail.url(:original), :class => "img-thumbnail") %>
</p>
<p>
<%= f.file_field :thumbnail %>
</p>
<p>
<%= f.select :tag_list, Post::Months, { }, { :multiple => true, :size => 10, :class => "form-control" } %>
</p>
<p>
<%= f.button :submit, class: "btn btn-primary" %>
</p>
</div>
</div>
</div>
<% end %>
How can I solve this issue?
Please correct me if I have misunderstood your problem.
By default, Turbolinks works only for GET requests.
So, in your case, you are not using turbolinks to submit the page.
Notice the data-no-turbolink attribute added to <div class = "form-group">
<div class = "form-group" data-no-turbolink >
<p>
<%= image_tag(#post.thumbnail.url(:original), :class => "img-thumbnail") %>
</p>
<p>
<%= f.file_field :thumbnail %>
</p>
<p>
<%= f.select :tag_list, Post::Months, { }, { :multiple => true, :size => 10, :class => "form-control" } %>
</p>
<p>
<%= f.button :submit, class: "btn btn-primary" %>
</p>
</div>
Whenever turbolinks finds a data-no-turbolink attribute, it ensures that all anchor tags within it fallback to the default behavior i.e. ignore turbolinks
The 'edit' page however may have been rendered using turbolinks.

Submit multiple entries in database table through single object in rails

I am new to rails, And stuck in this problem from last two days
I have a student and student_parent model they have a one-to-many relationship i want to build database attribute through one object from form to datase.
controller's method
def new
#student = Student.new
1.times{ #student.student_parents.build }
.....
.....
end
Create Method:-
def create
#student = Student.new(params[:student])
respond_to do |format|
if #student.save
format.html { redirect_to Student, notice: 'Student was successfully created.' }
format.json { render json: #student, status: :created, location: #student }
else
format.html { render action: "new" }
format.json { render json: #student.errors, status: :unprocessable_entity }
end
end
end
Model:-
class Student < ActiveRecord::Base
include ErrorMessages
belongs_to :user
has_many :student_parents
attr_accessible :birth_date, :blood_group, :first_name, :gender, :last_name, :middle_name,
:school_name, :student_rollno, :email, :user_id, :student_parents_attributes
accepts_nested_attributes_for :student_parents
end
form:-
<%= simple_form_for #student, :html => { :class => 'form-horizontal' } do |f| %>
<div class="row-fluid">
<div class="span3">
<%= f.label :first_name, :class => 'control-label',:required => true %>
<%= f.text_field :first_name %>
</div>
<div class="span3">
<%= f.label :middle_name, :class => 'control-label'%>
<%= f.text_field :middle_name %>
</div>
<div class="span3">
<%= f.label :last_name, :class => 'control-label',:required => true %>
<%= f.text_field :last_name %>
</div>
</div>
<div class="control-group">
<label class = "control-label"> Email </label>
<div class="controls">
<%= f.text_field :email, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<label class = "control-label"> Birth Date <abbr title="required">*</abbr></label>
<div class="controls">
<%= f.text_field :birth_date, :class => 'text_field' ,'data-behaviour' => 'datepicker' %>
</div>
</div>
<% model_class = StudentParent %>
<div class="page-header">
<h4> Parent Information</h4>
</div>
<%= f.fields_for :student_parents do |student_parent| %>
<div class="row-fluid">
<!--<div class="span9">-->
<h5> Father Name </h5>
<div class="span3">
<%= student_parent.label :first_name, :class => 'control-label',:required => true %>
<%= student_parent.text_field :first_name %>
</div>
<div class="span3">
<%= student_parent.label :middle_name, :class => 'control-label'%>
<%= student_parent.text_field :middle_name %>
</div>
<div class="span3">
<%= student_parent.label :last_name, :class => 'control-label',:required => true %>
<%= student_parent.text_field :last_name %>
</div>
</div>
<div class="row-fluid">
<h5> Mother Name </h5>
<div class="span3">
<%= student_parent.label :first_name, :class => 'control-label',:required => true %>
<%= student_parent.text_field :first_name %>
</div>
<div class="span3">
<%= student_parent.label :middle_name, :class => 'control-label'%>
<%= student_parent.text_field :middle_name %>
</div>
<div class="span3">
<%= student_parent.label :last_name, :class => 'control-label',:required => true %>
<%= student_parent.text_field :last_name %>
</div>
</div>
<% end %>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
students_path, :class => 'btn' %>
</div>
when submit it only build student information in student table and mother information in student_parent table. But it miss the parent information.

How do I fix this form the Rails way?

I want to have all the errors appear on top of their respective areas.
I ran an if statement with any? on the first one but I know I am repeating myself and have to do it the Rails way.
Any help?
<%= form_for #movie, :html => {:class => "form-horizontal"} do |f| %>
<div class="control-group">
<% if #movie.errors[:title].any? %>
<div class="alert alert-error"><%= #movie.errors[:title].to_sentence %></div>
<% end %>
<%= f.label :title, :class => "control-label" %>
<div class="controls">
<%= f.text_field :title %>
</div>
</div>
<div class="control-group">
<div class="alert alert-error"><%= #movie.errors[:description].to_sentence %></div>
<%= f.label :description,:class => "control-label" %>
<div class="controls">
<%= f.text_area :description, :class => "span8", :rows => "10" %>
</div>
</div>
<div class="control-group">
<div class="alert alert-error"><%= #movie.errors[:rating].to_sentence %></div>
<%= f.label :rating, :class => "control-label" %>
<div class="controls">
<%= f.select :rating, Movie::RATINGS, prompt: "Pick one" %>
</div>
</div>
<div class="control-group">
<div class="alert alert-error"><%= #movie.errors[:total_gross].to_sentence %></div>
<%= f.label :total_gross, :class => "control-label" %>
<div class="controls">
<%= f.number_field :total_gross %>
</div>
</div>
<div class="control-group">
<div class="alert alert-error"><%= #movie.errors[:released_on].to_sentence %></div>
<%= f.label :released_on, :class => "control-label" %>
<div class="controls">
<%= f.date_select :released_on, :order => [:month, :day, :year], :prompt => { :month => 'Select month',:day => 'Select day', :year => 'Select year' }, :start_year => 1950 %>
</div>
</div>
<div class="control-group" >
<%= f.label :image_file_name, :class => "control-label" %>
<div class="controls">
<%= f.text_field :image_file_name %>
</div>
</div>
<div class="form-actions">
<%= f.submit :class => "btn btn-success btn-large" %> <%= link_to "Cancel", root_path, class: "btn btn-danger btn-large" %>
</div>
<% end %>
Here's a solution that will be more DRY and won't necessitate gems like simple_form.
Create a partial for your control group and replace the field name and form field helper with variables:
# File: _control_group.html.erb
<% show_errors = true if show_errors.nil? %>
<div class="control-group">
<% if #movie.errors[field_name].present? && show_errors %>
<div class="alert alert-error">
<%= #movie.errors[field_name].to_sentence %>
</div>
<% end %>
<%= label_tag field_name, :class => "control-label" %>
<div class="controls">
field_helper
</div>
</div>
Then replace these items in your form with a partial render and pass the appropriate code in through the parameters:
<%= form_for #movie, :html => {:class => "form-horizontal"} do |f| %>
<%= render "control_group", field_name: :title, field_helper: f.text_field(:title) %>
<%= render "control_group", field_name: :description, field_helper: f.text_area(:description, :class => "span8", :rows => "10") %>
<%= render "control_group", field_name: :rating, field_helper: f.select(:rating, Movie::RATINGS, prompt: "Pick one") %>
<%= render "control_group", field_name: :total_gross, field_helper: f.number_field(:total_gross) %>
<%= render "control_group", field_name: :released_on, field_helper: f.date_select(:released_on, :order => [:month, :day, :year], :prompt => { :month => 'Select month',:day => 'Select day', :year => 'Select year' }, :start_year => 1950) %>
<%= render "control_group", field_name: :image_file_name, field_helper: f.text_field(:image_file_name), show_errors: false %>
<div class="form-actions">
<%= f.submit :class => "btn btn-success btn-large" %> <%= link_to "Cancel", root_path, class: "btn btn-danger btn-large" %>
</div>
<% end %>
Ran into a similar problem.
Using simple_form worked for me. See an example here

Resources