How pass local variable into another view? - ruby-on-rails

I have next code:
posts/_form.html.haml
- cat = Post::CATEGORIES.map { |c| [t(c, scope: :post_categories), c] }
= f.input :category, collection: cat
= f.input :subject, input_html: { class: 'input-block-level' }
= f.input :body, input_html: { rows: 5, class: 'ctrlenter input-block-level expanding' }
= link_to "#", class: 'smiley', role: 'add_smiley', tabidex: 4 do
%i.icon.icon-smile
смайлы
= smiles_helper '#post_body'
.form-horizontal
= f.input :tag_list, input_html: { class: 'input-block-level' }
- if (current_user.paid? && current_user.moderated_group_ids.include?(#group.id)) || moderator?
= f.input :comments_disabled, inline_label: true, label: false
.attachments
= f.simple_fields_for :attachments do |af|
= render "attachments/#{af.object.asset_type.underscore}", :f => af
.poll{style: "display: none;"}
%h1 "Новый опрос"
= f.simple_fields_for :poll do |post|
= render "polls/poll_fields", f: post
polls/poll_fields.html.haml
= f.error_messages header_message: nil
= f.input :question, disabled: !#poll.editable?(current_user), input_html: { class: 'input-block-level' }
= f.input :results_hidden, as: :boolean, inline_label: 'Скрыть результаты до окончания опроса', label: false
= f.input :from_date, as: :datetime, input_html: { class: 'poll_date' }
= f.input :to_date, as: :datetime, input_html: { class: 'poll_date' }
%h3#poll-items Варианты ответа (не больше пяти)
.item_index
= f.simple_fields_for :poll_items do |poll|
= render template: "polls/poll_item_fields", f: poll
= link_to_add_association 'Добавить еще вариант', f, :poll_items,
{ 'data-association-insertion-method' => 'before',
'data-association-insertion-traversal' => 'next' }
polls/poll_item_fields.html.haml
.poll_row
.poll_item
= f.input :answer, input_html: { class: 'ctrlenter expanding' }, label: false, placeholder: 'Введите вариант ответа'
= link_to_remove_association "удалить", f, { wrapper_class: 'poll_item' }
Displayed error: Showing .../views/polls/poll_item_fields.html.haml where line #3 raised:
undefined local variable or method `f' for #<#:0xbe2d1a2c>
How me correct pass variable f to polls/poll_item_fields.html.haml?

instead of
render template: "polls/poll_item_fields", f: poll
try
render "polls/poll_item_fields", f: poll

Related

Ruby on Rails - Can't access the updated model from Slim View

I have a view for editing my model. I am using Slim views.
My model_name is 'cupcake'
= simple_form_for [:admin, :order, record] do |f|
.row
.col-md-6
label for="_kind" Kind
= f.select :kind, [['Clipart', :clipart], ['Image', :image], ['Message', :message]], { selected: record[:kind] }, value: record[:kind], class: 'form-control', :input_html => { :name => "cupcake[:kind]" }
- if #record[:kind] == 'image'
.col-xs-12
= f.input :image, as: :shrine_file
- elsif #record[:kind] == 'clipart'
.col-md-6
label for="_clipart" Clipart
= f.select :clipart_id, options_for_select(#cliparts.map { |c| [c['name'], c['id'], {'value' => c['id']}] }, selected_key = selected_clipart(record).id), { selected: selected_clipart(record).name }, class: 'form-control', :input_html => { :name => "cupcake[:clipart_id]" }
- else
.col-md-6
= f.input :text_line_style, collection: ["Three Line Text","Single Line Text"], checked: record['first_line'] ? 'Three Line Text' : "Single Line Text", as: :radio_buttons, item_wrapper_class: "inline"
.row
.col-xs-12
hr
button.btn.btn-success.btn-lg
= fa_icon('check')
| Save
- redirect_location = [:admin, :cliparts]
= link_to redirect_location, class: 'btn btn-primary btn-lg'
= fa_icon('chevron-left')
| Back
.col-xs-12
- if f.object.created_at.present?
p.help-block Created at: #{f.object.created_at}
- if f.object.updated_at.present?
p.help-block Updated at: #{f.object.updated_at}
I want to update the view based on the kind selected. Currently, it is not updating the view if I select a different kind. When I try to access cupcake[:kind], it is showing undefined error. Any way to access the cupcake object? I looked for it in the f object. But, I couldn't find it.
Anyone, please help?

Search Bar with multiple input and multiple filter rails

My question is how can i in multi-input search bar filter my announcements with multiple filter ?
I would like to index just the announcements who respects my conditions.
Now i just search with geolocalisation and references.
Informations :
I used a PostgreSQL database with ActiveRecord
Coding Language : Ruby On Rails
Type of project : Web Site For Estate
Search Gem : Pg Search
Geolocalisation Gem : Geocoder
Annoucements_Controller.rb
def index
if params[:search]
#annoucements = Annoucement.all if params[:search][:search_by_city] == "" && params[:search][:search_by_reference] == ""
#annoucements = Annoucement.near(params[:search][:search_by_city], params[:search][:distance], units: :km) if params[:search][:search_by_city] != ""
#annoucements = Annoucement.search_by_city_and_reference(params[:search][:search_by_reference]) if params[:search][:search_by_reference] != ""
else
#annoucements = Annoucement.all
end
end
home.html.erb
<%= simple_form_for :search, url: annoucements_path, method: :get do |form|%>
<%= form.input :search_by_status,label: false , collection: ['A vendre', 'A louer'], selected: "A vendre"%>
<%= form.input :search_by_city, as: :string, label: false , placeholder: 'Ville'%>
<%= form.input :search_by_reference, as: :string, label: false , placeholder: 'Reference'%>
<%= form.input :distance, as: :hidden, input_html: {value: 10}%>
<%= form.submit "Rechercher", class: 'btn btn-primary' %>
<% end %>
annoucement.rb
geocoded_by :address
after_validation :geocode, if: :address_changed?
include PgSearch
pg_search_scope :search_by_city_and_reference, against: {:city => 'A', :reference => 'B'}
My objectif it's to filter all the data present in this form :
index.html.erb
<%= simple_form_for :search, url: annoucements_path, method: :get do |form|%>
<%= form.input :search_by_status,label: false , collection: ['A vendre', 'A louer'], selected: "Achat"%>
<%= form.input :search_by_city, as: :string, label: false , placeholder: 'Ville'%>
<%= form.input :search_by_reference, as: :string, label: false , placeholder: 'Reference'%>
<%= form.input :distance, label: "Périmètre de recherche (en Km)", input_html: {value: 10}, collection: 1..50%>
<%= form.input :price, as: :integer, label: false , placeholder: 'Prix'%>
<%= form.input :number_of_bedroom, as: :integer, label: "Nombre de chambre", placeholder: 'Nombre de chambre'%>
<%= form.input :number_of_bathroom, as: :integer, label: 'Nombre de salle de bain',placeholder: 'Nombre de salle de bain'%>
<%= form.input :size, label: 'Taille du bien (m2)', as: :integer, placeholder: 'Taille du bien (m2)'%>
<%= form.input :field_size, as: :integer, label: 'Taille de terrain (m2)', placeholder: 'Taille de terrain (m2)'%>
<%= form.input :balcony, as: :boolean, placeholder: 'Balcon'%>
<%= form.input :garage, as: :boolean, placeholder: 'Garage'%>
<%= form.input :furnished, as: :boolean, placeholder: 'Meublé'%>
<%= form.submit "Rechercher", class: 'btn btn-primary' %>
<%end%>
Thank you,
Bye

Ruby on Rails - Adding a human security field into the simple_form gem?

I currently have a form created using the simple_form gem in Ruby on Rails but I would like to add a sort of 'What is 1+1?' question and input field as the last question to remove the risk of bots etc. How would I add this function into my form?
My form consists of the following:
<%= simple_form_for #job, html: { multipart: true } do |form| %>
<h2>Job Position:</h2>
<%= form.input :position, input_html: { maxlength: 60 }, placeholder: "Job Position", label: false %>
<%= form.input :company, input_html: { maxlength: 60 }, placeholder: "Company name", label: false %>
<%= form.input :salary, input_html: { maxlength: 60 }, placeholder: "Salary", label: false %>
<%= form.input :contract, input_html: { maxlength: 60 }, placeholder: "Contract Type", label: false, collection: ['full time', 'part time', 'internship'], prompt: "Contract Type" %>
<%= form.input :city, input_html: { maxlength: 60 }, placeholder: "City", label: false %>
<%= form.input :expirydate, input_html: { maxlength: 60 }, placeholder: "Expiry date", label: false %>
<%= form.input :description, input_html: { maxlength: 60 }, placeholder: "Full job description", label: false %>
<%= form.input :apply, input_html: { maxlength: 60 }, placeholder: "How to apply", label: false %>
<h2>Your Contact Details:</h2>
<%= form.input :contactname, input_html: { maxlength: 60 }, placeholder: "Contact Name", label: false %>
<%= form.input :contactemail, input_html: { maxlength: 60 }, placeholder: "Contact Email", label: false %>
<%= form.input :contactphone, input_html: { maxlength: 60 }, placeholder: "Contact Telephone", label: false %>
<%= form.button :submit %>
<% end %>
You can add a validation to your model like this:
class Job < ActiveRecord::Base
attr_accessor :human_sum
validate :not_a_bot
private
def not_a_bot
if human_sum.to_i != 2
errors.add(:human_sum, 'Get out, you bot!')
end
end
end
And then in your form:
<%= simple_form_for #job, html: { multipart: true } do |form| %>
...
<%= form.input :human_sum, label: 'What is 1+1?'
<% end %>
Don't forget to add :human_sum to your permitted params in your controller as well.

Rails simple_form error: false

in my app I have form that looks like this
= simple_form_for #user do |f|
= f.input :name, error: false
= f.input :surname, error: false
Is there any way to avoid this repetitions (error: false)?
If they're all of the same type, something like this should work:
= simple_form_for #user do |f|
- [ :name , :surname ].each do |field|
= f.input field, error: false
If not, you could use a hash or something, instead of an array, and specify the type, as well.
It appears that simple form has the following option:
If you want to pass the same options to all inputs in the form (for
example, a default class), you can use the :defaults option in
simple_form_for. Specific options in input call will overwrite the
defaults:
<%= simple_form_for #user, defaults: { input_html: { class: 'default_class' } } do |f| %>
<%= f.input :username, input_html: { class: 'special' } %>
<%= f.input :password, input_html: { maxlength: 20 } %>
<%= f.input :remember_me, input_html: { value: '1' } %>
<%= f.button :submit %>
<% end %>
From https://github.com/plataformatec/simple_form
So, in your case:
= simple_form_for #user , defaults: { error: false } do |f|
= f.input :name
= f.input :surname
You could loop through an array of symbols
simple_form_for #user do |f|
[:name, :surname].each do |element|
f.input element, error: false
end
end

Rails - simple form

I use simple_form gem in my Rails app. When I use code like this
= f.input_field :name, label: false, placeholder: 'Name'
= f.input_field :price, label: false, placeholder: 'Price'
= f.input_field :description, label: false, placeholder: 'Description'
= f.input_field :image, label: false, placeholder: 'Image'
I get HTML for input:
<input class="string required textform" id="item_name" label="false" maxlength="255" name="item[name]" placeholder="Имя" size="255" type="text">
As you can see size of input is 255 now. Actually it's much more than enough. How can specify the size of inputs?
Following is from simple_form documentation here
It is also possible to pass any html attribute straight to the input,
by using the :input_html option, for instance:
<%= simple_form_for #user do |f| %>
<%= f.input :username, input_html: { class: 'special' } %>
<%= f.input :password, input_html: { maxlength: 20 } %>
<%= f.input :remember_me, input_html: { value: '1' } %>
<%= f.button :submit %>
<% end %>
To set size of 100 for name input:
= f.input :name, label: false, placeholder: 'Name', input_html: { size: 100 }

Resources