Turbolinks and rails - ruby-on-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.

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 save not working on gem cocoon with different edit form

Friends , I have a model order that has many details. I am using the cocoon gem to generate forms . They render ok but are not saving . I click the button to save and nothing happens , and rails console shows nothing. The default edit form, called detail_fields saves with no problem. But i had to create this one called detail_fields_dev, and this one is not working. Can someone help?
routes.rb
resources :details
resources :orders
get '/orders/:id/devolucao' => 'orders#devolucao' , as: 'devolucao_order'
put '/orders/:id' => 'orders#update'
patch '/orders/:id' => 'orders#update'
orders_controller, is ok, using the default rails code. I created this action to call the detail_fields_dev form:
def devolucao
# #order = Order.find(params[:id])
end
The details params are ok too:
def order_params
params.require(:order).permit(:customer_id, :valor_total, :valor_total_dev, :item_total, :item_total_dev,:tipo,:descontado,:order_num, details_attributes: [:id,:order_id, :cod_produto, :desc_produto, :cod_cor, :desc_cor, :desc_tamanho,:preco,:quantidade,:quantidade_dev,:total, :total_dev,:barcode, :_destroy])
end
Order view:
_form_devolucao.html.erb
<%= simple_form_for(#order) do |f| %>
<%= f.error_notification %>
<div class="form-inputs form_fixed">
<%= f.input :customer, :as => :hidden %>
<%= f.input :tipo, :as => :hidden %>
<%= f.input :descontado, :as => :hidden %>
<%= f.input :valor_total, :as => :hidden %>
<%= f.input :item_total, :as => :hidden %>
<%= f.input :order_num, :as => :hidden %>
<div class="row">
<form action="#" method="post">
<div class="small-6 medium-3 columns leitor_dev">
Código de barras
(Leitor):
<input class = "cod_barras_dev" type="text" name="cod_barras_dev" value="" />
</div>
</form>
<div class="small-5 columns end not_found">
<span class="not_found">Produto não existe neste pedido!</span>
</div>
</div>
</div>
<div class="row">
<div class="small-12 columns">
<hr/>
<div id="details">
<%= f.simple_fields_for :details do |detail| %>
<%= render partial: "orders/detail_fields_dev", locals: {f: detail} %>
<% end %>
</div>
<hr/>
</div>
</div>
<div class="row">
<div class="small-12 columns">
<div class="form-actions container">
<%= link_to_add_association '+ ítens', f, :details, data: {"association-insertion-method" => :before, "association-insertion-node" => ".container " },:class => "button tiny radius add" %>
<br />
<br />
<br />
</div>
</div>
</div>
<div class="row">
<div class="small-6 medium-2 columns">
<%= f.button :submit, "Salvar" %>
</div>
<div class="small-6 medium-2 columns end">
<%= link_to 'Cancelar', orders_path, :class => "button alert"%>
</div>
</div>
<% end %>
And the partial orders/detail_fields_dev.html.erb
<div class="nested-fields">
<div class="row listCod" data-cod="<%= f.object.barcode %>">
<div class="small-6 columns show-for-small-only">
<%= f.input :barcode, label: "Cod Barras", input_html: { class: 'barcode_ror_dev' } %>
</div>
<%= f.input :order_id, :as => :hidden, input_html: { class: 'order_id_ror_dev' } %>
<div class="show-for-medium-up small-2 columns">
<%= f.input :cod_produto, label: "Produto", input_html: { class: 'cod_produto_ror_dev' } %>
</div>
<%= f.input :desc_produto,:as => :hidden, input_html: { class: 'desc_produto_ror_dev' } %>
<%= f.input :cod_cor,:as => :hidden, input_html: { class: 'cod_cor_ror_dev' } %>
<div class="small-2 show-for-medium-up columns">
<%= f.input :desc_cor,label: "Cor", input_html: { class: 'desc_cor_ror_dev' } %>
</div>
<div class="small-2 show-for-medium-up columns">
<%= f.input :desc_tamanho,label: "Tam", input_html: { class: 'desc_tamanho_ror_dev' } %>
</div>
<div class="small-2 show-for-medium-up columns">
<%= f.input :preco,label: "Preço",input_html: { class: 'preco_ror_dev' } %>
</div>
<div class="small-6 medium-2 columns">
<%= f.input :quantidade_dev,label: "Qtd Dev", input_html: { class: 'quantidade_ror_dev' } %>
</div>
<div class="show-for-medium-up medium-2 columns end">
<%= f.input :total_dev,label: "Total Dev",input_html: { class: 'total_ror_dev' } %>
</div>
<div class="small-1 columns end">
<%= link_to_remove_association "-", f, :class => "button tiny alert remove"%>
</div>
</div>
</div>
Output:
!(http://imgur.com/kVPpwY6)

I cant download the files which i was uploaded using carrierwave

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

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

show rest of a form if a checkbox is ckecked in ruby on rails

I need to ask to my user if will pay a service with credit card...if it checked the option pay_with_card? it must show the rest of the form, that ask for other data like card number, mail, etc. if the user don't checked it, it must show a message, the question is...how can I do this? thanks in advance
<%= form_for(#product) do |f| %>
<%= f.label :pay_with_card? %>
<%= f.check_box :pay_with_card,{}, "Yes", "No"%>
<div>
<%= f.label :card_number %> <%= f.text_field :card_number %>
</div>
<div>
<%= f.label :mail %> <%= f.text_field :mail %>
</div>
<% end %>
Make the card number/mail details div style="display:none;", then add some javascript to the checkbox to change it to display:block;
Something like this:
<%= form_for(#product) do |f| %>
<%= f.label :pay_with_card? %>
<%= f.check_box :pay_with_card,{}, "Yes", "No"%>
<div id="card_details" style="display:none;">
<%= f.label :card_number %> <%= f.text_field :card_number %>
<%= f.label :mail %> <%= f.text_field :mail %>
</div>
<% end %>
<script type="text/javascript">
var checkbox = document.getElementById('product_pay_with_card');
var details_div = document.getElementById('card_details');
checkbox.onchange = function() {
if(this.checked) {
details_div.style['display'] = 'block';
} else {
details_div.style['display'] = 'none';
}
};
</script>
How about using jQuery?
First, wrap your credit card fields in a div with class credit_card_fields and than add this JS code to your page:
$("input[type='checkbox']#pay_with_card").on('change', function(){
$('.credit_card_fields').toggle();
});
You can use JS for it or move pay_with_card out of form like:
<%= link_to 'pay with card', your_current_path(:pay_with_card => 1) %>
<%= form_for(...) do |f| %>
<% if params[:pay_with_card] %>
<%= # fields for card %>
<% end %>
<% end %>
You can do it through jQuery, for example:
$ ->
$('select#pay_with_card').change ->
if $(this).val() == 'yes'
$('.card_block').slideDown('fast')
else
$('.card_block').slideUp('fast')
assumed that part of the form with payment card is included in the div with .card_block class
Ok my solution is this: all the code in the view, if a user check pay_with_card...(mi code is in spanish) it shows the complete form...if is not checked don´t show nothing, just the same checkbox asking for payment... thanks guys.
function mostrar (){
var checkbox = document.getElementById('chk_tarjeta');
if (checkbox.checked)
document.getElementById("card_details").style.display = "block";
else
document.getElementById("card_details").style.display = "none";
</script>
<h1>Forma de Pago</h1>
<%= form_for(#product) do |f| %>
<div id="product_pay_with_card">
<div >
<%= f.label :paga_con_tarjeta? %></br>
<%= f.check_box :paga_con_tarjeta, :id => "chk_tarjeta", :onclick => "mostrar();" %>
<div>
</div>
</div>
<div id="card_details" >
<div>
<%= f.label :numero_de_tarjeta %></br>
<%= f.text_field :numerotarjeta %>
</div>
<div>
<%= f.label :codigo_de_seguridad %></br>
<%= f.text_field :codigoseguridad %>
</div>
This worked for me with a form_with model and bootstrap
Change my_hidden_form with an id that makes sense for your form.
Original code is haml
= form_with scope: :model, url: models_path, local: true do |form|
.row
.col-6
.form-group
%h5.mb0 THE CASE TO TICK
= form.check_box :form_value, {:data => {:aria => {controls: :my_hidden_form, expanded: false}, :toggle => "collapse", :type => "checkbox", :target => "#my_hidden_form" }}
.row
.col-6
.form-group.collapse#my_hidden_form
%h5.mb0 THE FORM TO SHOW WHEN CASE IS TICKED
= form.text_field :name, placeholder: "A name"
.row
.col-md-12.text-right
= form.submit 'Submit', class: "btn btn-primary"
Converted to erb/html with https://haml2erb.org/
<%= form_with scope: :model, url: models_path, local: true do |form| %>
<div class="row">
<div class="col-6">
<div class="form-group">
<h5 class="mb0">THE CASE TO TICK
<%= form.check_box :form_value, {:data => {:aria => {controls: :my_hidden_form, expanded: false}, :toggle => "collapse", :type => "checkbox", :target => "#my_hidden_form" }} %>
</h5>
</div>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="form-group collapse" id="my_hidden_form">
<h5 class="mb0">THE FORM TO SHOW WHEN CASE IS TICKED
<%= form.text_field :name, placeholder: "A name" %>
</h5>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 text-right">
<%= form.submit 'Submit', class: "btn btn-primary" %>
</div>
</div>
<% end %>
Since the approach suggested by #Unixmonkey didn't work for me, here's a slight variation I put together using an event listener.
<script type="text/javascript">
const checkbox = document.getElementById('product_pay_with_card');
const details_div = document.getElementById('card_details');
checkbox.addEventListener("change", (event) => {
if (event.currentTarget.checked) {
details_div.style['display'] = 'block';
}
else {
details_div.style['display'] = 'none';
}
});
</script>

Resources