How to check input right in template - ruby-on-rails

Can I access the input value and check his value?
I want to disable the ability to change the key if it's equal to 'foo'.
.email-template-param-nested-fields
- if :key == 'foo'
= f.input :key, placeholder: 'foo', readonly: true
- else
= f.input :key, placeholder: 'key'
= f.input :value, placeholder: 'example'

You can assign variable as
- args = f.object.key == 'foo' ? { placeholder: 'foo', readonly: true } : { placeholder: 'key' }
And then
= f.input :key, **args

Related

RAILS : Read_only field using rendered form

I have a form view with an email text field and some other type of field in a view named _form.html.haml, as follows :
....
= easy_form_for #form, label_col: 'col-md-2', control_col: 'col-md-10' do |f|
= f.text_field :name, required: true
= f.text_field :email, required: true
= f.text_area :description, rows: 5
....
And an edit.html.haml view that inherits from this form as follows:
...
%h2.page-header= page_title
= render 'form'
...
I would like to make a Patch on this form in the edit.html.haml view just gray the email field and leave the other fields of the form as they are
You could send a parameter to the form partial and handle the readonly part there.
= render partial: 'form', locals: { disable_email: true }
If the form partial does not receive any disable_email it will default to false.
....
- disable_email = local_assigns.has_key?(:disable_email) ? disable_email : false
= easy_form_for #form, label_col: 'col-md-2', control_col: 'col-md-10' do |f|
= f.text_field :name, required: true
= f.text_field :email, required: true, readonly: disable_email
= f.text_area :description, rows: 5
....

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?

Rails 5: How to implement a conditional validation for an ActiveModel?

As the code below shows, I am trying to validate an form value of an ActiveModel object based on the value of a checkbox of the same form.
If the box is checked (I made sure it will return true not 1) the validation on order_number should be deactivated, as the field is being deactivated as well (by JS). The naive approach shown below, using the attribute that is connected to the checkbox not_a_customer as conditional for the validation of order_number didn't work.
What else can I try?
I have an ActiveModel class:
class SupportCase
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor(:email, :os, :inquiry_type, :order_number, :first_name, :last_name, :message, :not_a_customer)
validates :order_number, presence: true,
numericality: { only_integer: true },
length: { in: (4..5), message: 'doh' },
unless: :not_a_customer
end
And a form for creating support cases:
= simple_form_for #support_case, html: { class: 'form inset' } do |f|
.row
.col.sm-6
.row{ id: 'order-row' }
.col.sm-6
= f.input :order_number, input_html: { class: 'icon-field hash-icon' }
.col.sm-6
.label-title{ title: t("simple_form.labels.support_case.hint") }
= f.input :not_a_customer, as: :boolean do
= f.check_box :not_a_customer, {}, "true", "false"
.col.sm-6
= f.input :email, input_html: { type: 'email', required: 'true' }
= f.input :first_name, input_html: { type: 'text' }
= f.input :last_name, input_html: { type: 'text' }
.col.sm-12
~ f.input :message, as: 'text', input_html: { required: 'true' }
%button.btn.btn-action
= t('views.contact.form.submit')
Check the value of not_a_customer. You are just getting a string instead of a boolean value, which is always truthy ("true", "false" are both truthy value).
In Rails, you can do not_a_customer.to_bool to convert it into boolean.
The checkbox does not convert your value into boolean, because params is parsed as if they are all strings (including int, string, boolean values).

Unpermitted parameter error whereas parameter specified in params in controller

I have a product model which has a many to many association with a category model through join model category_product
I have a product/new.html.slim
=simple_form_for #product, html: { multipart: true } do |t|
= t.error_notification
div class="form-group"
= t.input :name, label: 'Nom',equired: true, input_html: { class: 'form-control' }
div class="form-group"
= t.input :description, label: 'Description', required: true, input_html: { class: 'form-control' }
div class="form-group"
= t.input :price, label: 'Prix', required: true, input_html: { class: 'form-control' }
div class="form-group"
= t.input :weight, label: 'Poids', required: true, input_html: { class: 'form-control' }
div class="form-group"
= t.association :categories, as: :check_boxes, label: "Catégories"
= t.button :submit, value: "Valider", class: "btn-success marge-bas"
when I submit my form I get the following error :
found unpermitted parameter: category_ids
though in my ProductController I have permitted category_ids :
def product_params
params.require(:product).permit(
:category_ids,
:name,
:price,
:description,
:weight,
:picture,
:picture1,
:picture2,
:picture3,
)
end
When I check my params category_ids is an array of strings
"category_ids"=>["1", "2", "5", ""]
What am I doing wrong ?
Try rewriting product_params to:
def product_params
params.require(:product).permit(:name, ... :picture3, :category_ids => [])
end
Setting the category_ids to be an array at the end of your list of permitted params should resolve this error.
Hope it helps!
I ran into this "Unpermitted parameter: category_ids" error when working through the book, "Rails, Up And Running" and adding category_ids => [] fixed it. Thank you, Zoran!

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