radio_button selection - ruby-on-rails

Using Haml 3.1.4 (Separated Sally)
I am curious as to what I am doing wrong.
Why does this not show the first radio button selected?
btw, at execution, #organization.pdf_size does equal 'letter_size'
I would actually like the radio button selected based on the
#organization.pdf_size, however I am just trying to get a hard
coded selection to work atm. tyfyt
= form_for [#organization] do |f|
Select a PDF page size
= label_tag 'Letter (8.5x11)'
= f.radio_button :pdf_size, id: 'letter_size', :checked => true
= label_tag 'Half Legal (8.5x7)'
= f.radio_button :pdf_size, id: 'half_legal_size'
= f.submit 'Save', class: 'button'
I have also tried other examples I have seen on stackoverflow, in this fashion:
= f.radio_button :pdf_size, id: 'letter_size', :checked => #organization.pdf_size == 'letter_size' ? true : nil

Try this:
= f.radio_button :pdf_size, "value", id: 'letter_size', :checked => true
As documented here, the radio button needs a value before the options.
Remember to change "value" to something that makes sense for your application.

Try appending this to your link:
input_html: {checked: true}

Related

Select box doesn't show selected value

I have a select box like this, it works and is saved in the database, but after I refresh the page the selected value doesn't appear as selected. Any suggestions?
.col-md-6
%span.label.label-purple{:style => "pointer-events: none; font-size: 14px"} Country Default
= select_tag :country_default, options_for_select(Location.where(loc_type: "country").map { |country| [country.name, country.loc_code] }),
style: "margin-top: 11px;",
include_blank: "",
class: "country-default select2", data: { select: "select2"}
options_for_select has an optional second argument, which will display your selected option, if one has been selected.
...options_for_select(Location.where(loc_type: "country").map { |country|
[country.name,
country.loc_code]}, #YOURMODEL.country_default)...
Use collection_select instead of select_tag like this:
=collection_select :country, :default, Location.where(loc_type: "country"), :loc_code, :name,
{include_blank: '', selected: 'select2' },
{class: 'country-default select2', style: 'margin-top: 11px;'}
another option is using select to generate select boxes.
See the Action View Helpers Guide

Multiple selected data doesn't appear

I use select2 for multiple select. My problem is, I can select multiple value from json data and I can save this data in related model columns. But when I want to update this model, the selected data doesn't appear.
some part of my json data:
{
- categories:[
- {
id:7,
name:"Eğitim Bilimleri Enstitüsü"
},
- {
id: 8,
name: "Eğitim Yönetim Teftişi ve Planlaması 1. Yarıyıl"
},
...
]
}
I have a Moodle class(it is not inheritance from ActiveRecord, just a class and it has some functions which return json data). I am wondering whether it is right approach.
in the content of my form:
<%= simple_form_for(#period) do |f| %>
...
<%= f.input :moodle_connect_ids, collection: Moodle.categories.map {|p| [ p['name'], p['id'] ] }, input_html: {multiple: true} %>
...
<% end %>
I explicitly set moodle_connect_ids to be an array in my controller:
params.require(:period).permit(..., :moodle_connect_ids => [])
in the content of my .js file:
$("#period_moodle_connect_ids").select2({
placeholder: "Moodle Dönem Bağlantılarını Seçiniz",
multiple: true,
allowClear: true
});
When I select to multiple values and save my model, the column's value looks like this:
> Period.last
=> #<Period:0x007fcf53a4d830
id: 25,
...
moodle_connect_ids: "[\"\", \"85\", \"120\"]"
...
Am I on the wrong way? Have you any suggestion?
I've figured out how to do this issue.
I've added selected option in the f.input:
selected: #period.moodle_connect_ids? ? JSON.parse(#period.moodle_connect_ids).map(&:to_i) : []
the input's last state:
<%= f.input :moodle_connect_ids, collection: Moodle.categories.map {|p| [ p['name'], p['id'] ] }, input_html: {multiple: true}, selected: #period.moodle_connect_ids? ? #period.moodle_connect_ids.map(&:to_i) : [] %>

Title case for simple_form labels

The following code gives a label of "Project address":
%h2 New Project
= simple_form_for(#project, html: {class: "form-horizontal"}, wrapper: :horizontal_form) do |f|
= f.error_notification
= f.input :project_address, required: true
= f.button :submit, "Create"
Is there any way of making the label title case ("Project Address") apart from using f.label?
If you wanted to title case all labels, you can configure simple form in the initializer:
# config/initializers/simple_form.rb
# How the label text should be generated altogether with the required text.
config.label_text = lambda { |label, required, explicit_label| "#{required} #{explicit_label ? label : label.to_s.titleize}" }
This will make all your labels title case unless you specify the label explicitly with the :label option.

Can't specify a class with simple_form gem with a block

I have the following form but for some reason, I can't get the HTML class to come appear in the resulting code. The documentation suggests this should work, but they don't have an example that uses a block. I've tried a few different things but I haven't found the right answer yet. I have to use a block to get the custom data to appear with the select.
= simple_form_for([:admin, #theme]) do |f|
= f.input :title_bar
= f.input :apple_touch_icon_image, input_html: { class: 'imagepicker' } do
= f.select :apple_touch_icon_image_id, Theme::AppleTouchIconImage.where(company: #company).map{ |i| [i.id, i.id, { 'data-img-src' => i.image.url(:thumbnail) }] }
= f.button :submit, class: "btn-success"
I would try:
= f.input :apple_touch_icon_image do
= f.select :apple_touch_icon_image_id, Theme::AppleTouchIconImage.where(company: #company).map{ |i| [i.id, i.id, { 'data-img-src' => i.image.url(:thumbnail) }] }, {}, { class: 'imagepicker' }

Two submit buttons one remote and one for submit in rails [duplicate]

This question already has answers here:
HTML form with two submit buttons and two "target" attributes
(16 answers)
Closed 8 years ago.
I looking for the solution to use two submit buttons with one form like the following:
The first should submit the given form, to the create function in my controller
The second should execute the given form remotely, to show the entered text under the form as preview
View:
<%= form_for #topic do |f| %>
.................
... some code ...
.................
<p>
<%= f.fields_for :topic_content do |tf| %>
<%= tf.text_area :text , :id => 'topic_text', :cols => 100 , :rows => 15, :class => 'topic_text' %></p>
<% end %>
.................
... some code ...
.................
<%= f.submit "Save", :name => 'save' %>
<%= f.submit "Preview", :name => 'preview' %>
<%end%>
<div id='preview_topic_text'>
</div>
Please change your submit to link_to tag and give id for that link_tag,then write ajax function
<%=link_to('Save', '#', :class => "save","data-button" => "save") %>
for preview
<%=link_to('preview', '#',:class => "save","data-button" => "preview") %>
jQuery(".save").click(function(){
var form_value = jQuery('#your_form_id').serialize();
var button = $(this).data("button");
if(button == "save"){
//your ajax code
// in data you append button the value
}
else{
}
// document.multi.action+="?"+form_value+"&flag="+ button;
// document.multi.submit();
});
so,in controller you can get params[:flag],then you could check the condition.
u can use PHP to define conditions of what button is pressed, try this code:
if (isset($_GET['submitbtn1']))
{
//execute the 1st code here
}
else if (isset($_GET['submitbtn2']))
{
//execute the 2nd code here
}

Resources