Select in rails simple_form gives nil irrespective of option selected - ruby-on-rails

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

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?

Using a select with option to input text if no options are found in collection

I am using Rails, HAML and simple_form and I am attempting to create a select with an option to enter text if the item is not found in the collection. Is this possible? It currently looks like this:
= simple_form_for [:admin, office] do |f|
= f.input :person,
label: "Select Person",
collection: People.all,
label_method: -> (person) { person.name },
input_html: { data: { plugin: "select2" } }
= f.submit "Set Person", class: "btn btn-primary btn-block"

Specify visible input fields from simple_form layout

I'm loading two views that render a simple_form form layout, new and edit.
I need to show all input fields when the user profile is initially being created (new), and specify fields that will not show when it is being used for edit. I've seen a lot of info around persisted but can't figure it out.
My _form.html.slim file.
= simple_form_for([:admin, User.new]) do |f|
= f.error_notification
.form-inputs
= f.input :name, required: true, label: 'Name'
= f.input :email, required: true
= f.input :password, required: true, placeholder: ("#{#minimum_password_length} characters minimum" if #minimum_password_length)
= f.input :password_confirmation, required: true
.form-actions
= f.button :submit, "Create User", class: 'btn btn-primary btn-block btn-lg'
I'm using Rails and Slim - Any help appreciated.
You can indeed use persisted? to conditionally render the fields in form.
= simple_form_for([:admin, User.new]) do |f|
= f.error_notification
.form-inputs
= f.input :name, required: true, label: 'Name'
= f.input :email, required: true
-# Say you don't want user to edit his password after creation
- unless f.object.persisted?
= f.input :password, required: true, placeholder: ("#{#minimum_password_length} characters minimum" if #minimum_password_length)
= f.input :password_confirmation, required: true
.form-actions
= f.button :submit, "Create User", class: 'btn btn-primary btn-block btn-lg'
Here f.object will be the User instance for which the form will be rendered.

How increment value in hidden_field?

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' }

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.

Resources