I have several of this, where the only thing that differs is if its a text_field or a password_field etc etc etc.
I'd like to pass this as a parameter to the render, like :as => :password_field for example.
And I don't wan't to do a case compare, the value passed in the :as is the value of the field. Is this possible ?
.text{:class => form.object.errors[field].any? ? "error" : nil}
= form.label field
-if defined? value
= form.text_field field, :value => value
-else
= form.password_field field
-if defined? hint
%p#hint= hint
= render 'shared/error_messages', :object => form.object, :field
.text{:class => form.object.errors[field].any? ? "error" : nil}
= form.label field
-if defined? value
= form.text_area field, :value => value
-else
= form.text_area field
-if defined? hint
%p#hint= hint
= render 'shared/error_messages', :object => form.object, :field => field
Answer is the one below, with some fixes:
-# expects form, field_name, field_type, value and hint variables to be passed
.text{:class => form.object.errors[field_name].any? ? "error" : nil}
= form.label field_name
- if defined?(value)
= form.send(field_type, field_name, :value => value)
- else
/= form.send(:field_type, field_name)
= form.send(field_type, field_name)
-if defined? hint
%p#hint= hint
= render 'shared/error_messages', :object => form.object, :field => field_name
Usage:
= render 'shared/form_field', :form => f, :field_name => :email, :field_type => :text_field
Create a partial called shared/form_field.html.haml
- # expects form, field_name, field_type, value and hint variables to be passed
.text{:class => form.object.errors[field_name].any? ? "error" : nil}
= form.label field_name
- if defined?(value)
= form.send(:field_type, field_name, :value => value)
- else
= form.send(:field_type, field_name)
-if defined? hint
%p#hint= hint
= render 'shared/error_messages', :object => form.object, :field => field_name
You can invoke the partial as
- form_for :user do |form|
= render 'shared/form_field', :locals => {:form => form,
:field_name => :login, :field_type => :text_field}
If I understand your question right, you're after this:
render :partial => 'user/login_errors', :locals => { :field => :first_name, :value => #user.first_name, :form => form }
You use the locals hash to pass in any number of variables. In your case, the variables in your code snippet were form, field, and value. The keys in the hash determine what the variable will be referenced as in the partial, and the values in the hash determine the variable values.
Related
I have ajax-based select in my form, my form:
= form_for #car, :html => {:id => "new-car-form"} do |f|
- if #car.errors.any?
- if #car.errors.any?
.flash-notice
%b= "Ошибки в полях: #{#car.errors.count} ошибка(и)"
%ul
- #car.errors.each do |attr,msg|
%li= msg
.field
= f.label :vehicle_manufacturer_id, "Марка"
= f.select :vehicle_manufacturer_id, options_from_collection_for_select(VehicleManufacturer.all, :id, :name, #car.vehicle_manufacturer_id), {:prompt => "Выберите марку"}, required: true, id: "manufacturer-select"
.field
= f.label :vehicle_model_id, "Модель"
#models-area
= render :partial => 'models', :object => #models
and partial:
- if #models.present?
= select_tag "by_model", options_from_collection_for_select(#models, "id", "name", selected: #selected), :prompt => "Выберите модель", required: true, id: "model-select"
- else
= select_tag "by_model", nil, :prompt => "Выберите модель", required: true, id: "model-select", disabled: true
I test, and could have such case: when I didn't enter something and submit form - I see validation errors and my select values are nil, but how and in which action to append this something like this:
#models = VehicleModel.where(manufacturer_id: #car.vehicle_manufacturer_id)
#selected = #car.vehicle_model_id
I use rails4.
What I do wrong and how to solve my problem?
Try passing your form variable through to the partial; as the select_tag on its own won't be able to associate it's value with the form_for #car.
= render :partial => 'models', :object => #models, :form => f
Then, in the partial:
= form.select "by_model ..."
To be more specific, that form_for #car clause at the top of your form says 'wrap eveything in this block in a 'car' object when it is submitted'. So right now the form you're submitting has values like this:
car[vehicle_manufacturer_id]
car[vehicle_model_id]
by_model
But you need to wrap that last by_model attribute in the car array like the others:
car[vehicle_manufacturer_id]
car[vehicle_model_id]
car[by_model]
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 »"
Here is my view:
<%= form_tag({ :action => "display"}, :method => "get") do %>
<%= select(:music, :type, MusType::TYPES, {:include_blank => true}) %>
Here is my array constant in the model:
class MusType < ActiveRecord::Base
TYPES = ['Jazz','Rock','Blues']
end
My select menu draws values out of an array. How do I pass the selected value into the controller as a parameter after the submit button is pressed?
you can do something like below.
view code
<%= form_tag(:url => {:controller => "mycontroller" :action => "display"}, :method => "get") do %>
<%= select_tag(:music, :type, MusType::TYPES, {:include_blank => true}) %>
<%= submit_tag "Search", :id => 'search' %>
<% end %>
read the selected value in mycontroller.rb
def display
value = params[:music][:type]
// do something with value
end
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" } }
Manager :has_many :interns, :through => :assigns
Intern :has_many :managers, :through => :assigns
I am trying to create a new assign record. This is the assigns/new view where a given authenticated intern is creating a new association with a manager:
# assigns/new.html.erb
<% form_for #assign, :url => {:action => "create"} do |p| %>
<%= p.label "Select Manager", nil, :class => "label" %>
<%= collection_select :assign, :manager_id, #managers, :id, :manager_name, options ={:prompt => "Select a manager"}, html_options ={:class =>"listBox", :style => "width:25em;"} %>
<%= p.hidden_field :intern_id %>
<% end %>
# assigns controller
def new
#intern = Intern.find(params[:id])
#assign = Assign.new(:intern_id => #intern.id)
render :layout => 'centered'
end
def create
#assign = Assign.new(params[:assign])
#assign.manager_id = params[:manager_id]
if #assign.save
redirect_to :controller => "interns", :action => "home"
else
render :action => :new
end
end
The problem is: manager_id = nil. Why? Thanks very much.
Comment out following or
#assign.manager_id = params[:manager_id]
OR replace with
#assign.manager_id = params[:assign][:manager_id]
You don't need this line
#assign.manager_id = params[:manager_id]
You're already assigning the manager_id in the line before
#assign = Assign.new(params[:assign])