How increment value in hidden_field? - ruby-on-rails

When I create a new answer of a Poll, I need to increase the value by one of a hidden_field :sequence . What do I need to add in the value in hidden_field?
.poll_row
.poll_item
= f.input :answer, input_html: { class: 'ctrlenter expanding' }, label: false, placeholder: 'Введите вариант ответа'
= f.hidden_field, :sequence, value:??????
= button_tag 'вверх', type: 'button', class: 'up_id', value: 'up'
= button_tag 'вниз', type: 'button', class: 'down_id', value: 'down'
- if #poll.editable?(current_user)
= link_to_remove_association "remove", f, { wrapper_class: 'poll_item' }

Related

Rails form keep sending old values after error

I have a form that for some reason always keep same values after error.
For example, if I enter name that already exists, it will get me an error and at the same time it will keep sending old value even if you change it (so you have to refresh page and lose all changes to actually resolve error).
= provide(:title, t(:create_task))
.center
= form_for :task, url: tasks_path do |f|
= render 'shared/big_flash'
= render 'task_error_messages'
.row.text-center
.col-lg-10.col-lg-offset-1.col-md-12.col-sm-12.col-xs-12
.jumbotron
%h2= t(:create_task)
.row
.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_task_name)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_new_name)} info_outline
= f.text_field 'name', maxlength: 255, class: 'form-control'
= f.label t(:par_category)
= f.collection_select 'category', Category.order(:name), :name, :name, {}, {class: 'form-control'}
= link_to t(:btn_modify_categories), categories_modify_path, class: 'btn btn-primary btn-block', data: { confirm: t(:changes_lost_proceed) }
= f.label t(:par_task_type)
= f.select 'task_type', Task::TASK_TYPES_LIST, {include_blank: true}, {class: 'form-control', id: 'type_of_task'}
= f.label t(:par_score)
= f.number_field 'score', min: '0', step: '1', class: 'form-control'
.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_asset)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_asset)} info_outline
= f.file_field 'category'
.description
= f.label t(:par_description)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_description)} info_outline
= f.text_area 'name', class: 'form-control'
= f.check_box 'mathjax'
= f.label t(:par_mathjax)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_mathjax)} info_outline
.row
.correct_solutions.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_correct_solutions)
#generated_correct_solutions
= button_tag t(:btn_add_correct_solution), type: 'button', class: 'btn btn-primary btn-toolbar', id: 'add_correct_solution'
.wrong_solutions.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_wrong_solutions)
#generated_wrong_solutions
= button_tag t(:btn_add_wrong_solution), type: 'button', class: 'btn btn-primary btn-toolbar', id: 'add_wrong_solution'
#close-ended_task{class: 'hidden'}
= f.check_box 'random', id: 'randomize_task'
= f.label t(:par_random)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_random)} info_outline
#randomize{class: 'hidden'}
.row
.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_number_of_solutions)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_no_random_solutions)} info_outline
= f.number_field 'no_random_solutions', min: '1', step: '1', class: 'form-control'
.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_min_number_of_correct_solutions)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_min_no_random_correct_solutions)} info_outline
= f.number_field 'min_no_random_correct_solutions', min: '0', step: '1', class: 'form-control'
.btn-group.btn-group-justified.hidden-xs
.btn-group
= f.submit t(:btn_create_new_task), class: 'btn btn-lg btn-primary btn-toolbar'
.btn-group
= link_to t(:btn_cancel), '#', class: 'btn btn-lg btn-primary btn-toolbar'
.btn-group-vertical.visible-xs
= f.submit t(:btn_create_new_task), class: 'btn btn-lg btn-primary'
= link_to t(:btn_cancel), '#', class: 'btn btn-lg btn-primary'
And my tasks_controller.rb
def new
#task = Task.new
end
def create
#task = Task.new(task_params)
#task.user_id = helpers.current_user.id
if #task.save
flash[:success] = t(:task_created)
redirect_to new_task_path
else
render 'new'
end
end
Any idea what could be the problem?
I don't want to clean up fields after error, I want to keep them, just on next POST, send new values instead.

Select in rails simple_form gives nil irrespective of option selected

Hi I am using simple_form in building forms. In select menu I get nil value for team_size irrespective of the option choosen by the user. Why does this happen. Could anyone help me out.
This is my form:
.centric
h2 Add new game
hr
= simple_form_for #game do |f|
- if #game.errors.any?
- #game.errors.full_messages.each do |msg|
li = msg
.pull-left
= f.input :name, label: "Game", required: true
= f.input :game_type, collection: game_type_map, label: "Type", required: true, include_blank: false
= f.input :played_by, collection: played_by_map, selected: "Select", label: "Played By", required: true
= f.input :team_size, collection: 1..12, selected: "Select", required: true
hr
= f.button :submit, "Add", class: 'btn btn-primary', data: {confirm: "Add Game?"}
| |
= link_to "Cancel", root_path

How pass local variable into another view?

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

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.

Change options_for_select to radio buttons

Hi i'm trying to change the following:
.form-group
= label_tag :metric, 'Metric', class: 'sr-only'
= select_tag :metric, options_for_select(Report::METRICS, selected: #report.metric), class: 'form-control'
to something like the following:
.form-group.pull-left
= label_tag :metric, 'Metric', class: 'sr-only'
.btn-group{"data-toggle" => "buttons"}
%label.btn.btn-default.active
%input#option1{name: "options", type: "radio", value: "value1"}/
Value1
%label.btn.btn-default
%input#option2{name: "options", type: "radio", value: "value2"}/
Value2
However the problem is that when I submit the form, it doesn't actually submit the radio button value. Also after it submits, it defaults back to no selection when it should remain on the selected radio button.
You can use the radio_button_tag helper and set the checked parameter correctly.
Using your example, it's something like this:
.form-group.pull-left
= label_tag :metric, 'Metric', class: 'sr-only'
.btn-group{"data-toggle" => "buttons"}
%label.btn.btn-default.active
= radio_button_tag('options', 'value1', params[:options] == 'value1')
Value1
%label.btn.btn-default
= radio_button_tag('options', 'value2', params[:options] == 'value2')
Value2

Resources