I want to do this in login form,
I just want to keep the data when user submit the data, but which is not valid.
The codes below:
in controller:
def login
if request.request_method == "POST"
#session = params[:session]
end
render "login",
:layout => "admin_login"
end
and the admin_login tempaltes:
= form_for :session, :url => { :action => "login", :controller => "administrator" }, :html => { :class => "form-horizontal" } do |f|
%div.control-group
#{f.label :token, "User / Email", :class => "control-label" }
%div.controls
#{f.text_field :token}
%div.control-group
#{f.label :password, "Password", :class => "control-label" }
%div.controls
#{f.password_field :password}
But it will throw a exception when I submit the form, said undefined method 'token'
Thanks
if you are not dealing with a resource, you should use form_tag
= form_tag { action: 'login', controller: 'administrator' }, class: 'form-horizontal' do
%div.control-group
%label.control-label User / Email
%div.controls
= text_field_tag :token
%div.control-group
%label.control-label Password
%div.controls
= password_field_tag :password
Related
Hi I need some more help.
I'm following this tutorial on using client_side_validation: Railcasts: Client Side Validations. It seems straight forwardly easy to use however I can't find how to make it my inline errors show.
Reports Model
Class Report < ActiveRecords::Base
attr_accessor :producer_selected, producer_id
belongs_to :producer
validates :producer_selected, presence => true
....
Reports Controller
Class ReportsController < ApplicationController
def generate_clients
#report = Report.new(report_params)
#report.queue_status = "Completed"
#report.created_by = current_user.id
respond_to do |format|
if #report.save
#producer = Producer.find(params[:report][:producer_id])
....
if #report.report_type == "Clients per Producer"
requests = Request.from_date_range(params[:report][:from_due_date], params[:report][:to_due_date])
#records = requests.records(params[:report][:producer_id])
end
format.html {
outstrio = StringIO.new
#report.update_attribute(:date_start, DateTime.now)
p = render_to_string handlers: [:axlsx], formats: [:xlsx], template: "reports/create_clients"
outstrio.write(p)
#report.update_attribute(:date_end, DateTime.now)
send_data outstrio.string, filename: "#{DateTime.now.to_date} #{#report.report_name}.xlsx"
}
format.json { render :show, status: :created, location: #report }
else
puts #report.errors.full_messages
format.html { redirect_to :back, alert: 'Both dates must be filled up.' }
format.json { render json: #report.errors, status: :unprocessable_entity }
end
end
end
_clients_form.html.haml
= simple_form_for #report, :validate => true, url:generate_clients_reports_path do |f|
= f.error_notification
.form-inputs
.form-group.col-md-12
=f.label :from_due_date, :label => "From:", class: "col-md-2 text-right", :style => "margin-top:6px;"
=f.date_field :from_due_date, :as => :date, class: "range-control col-md-2"
=f.label :to_due_date, :label => "To:", class: "col-md-2 text-right", :style => "margin-top:6px;"
=f.date_field :to_due_date, :as => :date, class: "range-control col-md-2"
.form-group.col-md-12
=f.input :report_type, :as => :select, :collection => #report_types, :wrapper => :field_multi8, :label_html => {:class => "col-md-2 text-right"}, :input_html => {:class => "form-control"}, :label => "Report Type:", :include_hidden => false, :include_blank => false
.form-group.col-md-12
= f.input :producer_selected, :url => autocomplete_producer_search_string_requests_path, :required => :true, :as => :autocomplete, :wrapper => :field_multi8, :label_html => { :class => "col-md-2 text-right" }, :input_html => { :class => "form-control" }, :id_element => "#report_producer_id", :label => "Producer/Client :", :placeholder => "Find Producer", :update_elements => {}, :autofocus => true
= f.input_field :producer_id, :as => :hidden
client_side_validations.rb
require 'client_side_validations/simple_form' if defined?(::SimpleForm)
require 'client_side_validations/formtastic' if defined?(::Formtastic)
<label for="#{instance.send(:tag_id)}" class="message"></label>
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
unless html_tag =~ /^<label/
%{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
else
%{<div class="field_with_errors">#{html_tag}</div>}.html_safe
end
end
Gem is installed already in gemfile
Rails 4.2.3
I'm doing all the basic things, but all the basic validations are also not working. After going out-of-focus from the field, no inline validation occurs.
What could be the problem? Help!
EDIT: Also tried this solution but it says that repository is not found.
How would one do something like:
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:age].blank? or "20"
Above doesen't work. I want to be able to set a default value if there is no param available for that attribute.
Any smart way to do this thx!
EDIT 1:
Full form:
= simple_form_for :people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:people][:age_from] || "20"
= f.submit "Go »"
You're using helpers that are taking their values from the object you're building the form on.
So in your controller, you should preset the values on the object.
def some_action
#people = People.new
#people.age = params[:age] || 20
end
Then in the form, remove the :selected option and it should be fine. Make sure you build the form for #people :
= simple_form_for #people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age'
= f.submit "Go »"
This is my controller:
class ReportNeedsController < ApplicationController
def index
#report_needs = nil
end
def new
#report_need = ReportNeed.new
end
def create
#report_need = ReportNeed.new(params[:report_need])
#report_need.save
redirect_to #report_need
end
end
This is my new.haml file:
Why do you want to report?
%form
%input{:name => "option", :type => "radio", :value => "spam"}
spam
%br/
enter code here
%input{:name => "option", :type => "radio", :value => "product_corrupted"}
product_corrupted
%br/
%input{:name => "option", :type => "radio", :value => "product_spoiled"}
product_spoiled
Make changes in view like :
%input{:type => "radio", :name => "report_need[product_type]", :id => "report_need[product_type]-1", :value => "salesforce", :checked => true}
%br/
%input{:type => "radio", :name => "report_need[product_type]", :id => "report_need[product_type]-2", :value => "salesforce", }
In controller, If product_type is field of table report_needs then no need to change in your action . else its change and store it like :
def create
report_need = ReportNeed.new(params[:report_need])
report_need.radio_field_name = params[:report_need][product_type]
report_need.save
redirect_to report_need
end
Excuse me, I am a newbie programmer in Rails.
I want use AJAX in a DATE SELECT. when I change the date, change the punitive ammount depends the result of method get_punitive (Model).
I use the next code in Controller, Model and View.However the event On change is not recognized. please you could help me.
Operations Model (partially)
def get_punitive(payment_date, expiration_date)
if payment_date > expiration_date
expiration_days= (payment_date - expiration_date).to_i
else
expiration_days = 0
end
punitive_ammount = (self.capital * (self.interest_rate_for_taker / 100.0) * expiration_days) / 30
# raise punitive_ammount.inspect
end
Operations Controller (partially)
def cancel
#operation = Operation.find(params[:id])
last_pagare = Pagare.find(:first, :conditions => "operation_id = #{#operation.id} AND state <> 'cancelled'")
#punitive_ammount = #operation.get_punitive(DateTime.now.to_date, last_pagare.expiration_date.to_date)
end
def update_payment_date
raise params.inspect
end
def submit_cancellation
#operation = Operation.find(params[:id])
ammount = params[:ammount]
punitive_ammount = (params[:punitive_ammount].blank?) ? 0 : params[:punitive_ammount]
payment_date = Date.new(params[:"payment_date(1i)"], params(:"payment_date(2i)"), params(:"payment_date(3i)"))
admin = User.find_by_login('admin')
if #operation.cancel(ammount, punitive_ammount, admin, payment_date)
respond_to do |format|
format.html { redirect_to(admin_operations_url) }
format.xml { head :ok }
end
else
#operation.errors.add_to_base("Los montos ingresados son invalidos")
render :action => 'cancel'
end
View of cancel
Cancelacion de Operaciones
<% form_for(#operation, :url => submit_cancellation_admin_operation_path) do |f| %>
<%= f.error_messages %>
<%= text_field_tag :a, :onClick => "javascript:alert('hola');" %>
<p>Fecha de Pago:<br/><br/>
<%= date_select("", :payment_date, {:start_date => Time.now, :onchange => remote_function(:url => {:controller => 'operations', :action => "update_payment_date"}, :with => "'payment_date='+value")}) %>
</p>
<p>Prueba
<%= collection_select "", :object, Operation.all, :id, :taker_id, { :onChange => "javascript::alert('hola');", :onClick => remote_function(:url => {:controller => 'operations', :action => "update_payment_date"}, :with => "'dgdfg='+value")} %>
</p>
<p>Monto a Cancelar (Mayor a cero):<br/><br/>
<%= text_field_tag :ammount , #operation.get_balance(#operation.interest_rate_for_taker) %>
</p>
<p>Monto punitorio:<br/><br/>
<%= text_field_tag :punitive_ammount, #punitive_ammount %>
</p>
<!--<p>Fecha de Pago:<br/><br/>
<%#= date_select("", :date, :start_year => Time.now.year-1, :default => Time.now) %>
</p>-->
<% %>
<p>
<%= f.submit 'Cancelar' %>
</p>
<% end %>
Hi format of date_select helper is
date_select(object_name, method, options = {}, html_options = {})
you have
<%= date_select("", :payment_date, {:start_date => Time.now, :onchange => remote_function(:url => {:controller => 'operations', :action => "update_payment_date"}, :with => "'payment_date='+value")}) %>
try
<%= date_select("", :payment_date, {:start_date => Time.now}, {:onchange =>"javascript::alert('hola');"}) %>
the same about collection_select
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
you have
<%= collection_select "", :object, Operation.all, :id, :taker_id, { :onChange => "javascript::alert('hola');", :onClick => remote_function(:url => {:controller => 'operations', :action => "update_payment_date"}, :with => "'dgdfg='+value")} %>
try
<%= collection_select "", :object, Operation.all, :id, :taker_id, {}, { :onChange => "javascript::alert('hola');"}%>
And as a result its not passing validation.
This is my embedded form :
- form_for [#organization, #referral] do |f|
= f.error_messages
= render :partial => 'referral_fields', :locals => { :f => f }
= f.submit "Submit", :class => "button"
#_referral_fields.html.haml
.grid_7
.grid_1{:style => "width: 64px;"}
= f.label :org_name, "Business", :class => "right"
.grid_4
= f.text_field_tag :org_name
.grid_7
.grid_1{:style => "width: 64px;"}
= f.label :name, '', :class => "right"
.grid_4
= f.text_field_tag :name
.grid_7
.grid_1{:style => "width: 64px;"}
= f.label :email, '', :class => "right"
.grid_2.omega{:style => "width: 114px;"}
= f.text_field_tag :email, '', :style => "width: 125px;"
.grid_1.alpha
= f.label :town, '', :class => "right"
.grid_2
= f.text_field_tag :town, '', :style => "width: 100px;"
And when I click submit, SQL definately reads the data I inputted :
Processing ReferralsController#create (for ::1 at 2010-10-18 09:39:07) [POST]
Parameters: {"name"=>"asdfasd", "commit"=>"Submit", "action"=>"create", "authenticity_token"=>"/1bwOqHjojde3p0Py08mCrko8xULE4R+eXUvT6qf1cE=", "controller"=>"referrals", "org_name"=>"asdfasdf", "organization_id"=>"1", "town"=>"asdfasd", "email"=>"asdfasd"}
Not sure what I'm missing. Here is controllers and models :
#referral.rb
belongs_to :organization, :touch => true
validates_presence_of :org_name, :name, :email, :town
#referrals_controller.rb
def new
#referral = Referral.new
respond_to do |format|
format.html { render :layout => 'manage' }
end
end
def create
#referral = Referral.new(params[:referral])
if #referral.valid? && #organization.referrals << #referral
flash[:notice] = "Referrals saved."
redirect_to new_organization_referrals_path(#organization)
else
render :action => :new, :layout => 'manage'
end
end
From looking at the parameters, it doesn't look like you have your form fields setup properly?
You are using the params[:referral] hash to build the referral, but I don't see a :referral hash in your parameter list....
Your form fields should look like this:
<input name="referral[name]"/>
<input name="referral[town]"/>
<input name="referral[org_name]"/>
etc...
And then in your parameter list you should be something like { :referral => {:name => "foo", "org_name" => "bar", town => "Boise" } }