Using font awesome with formtastic submit button - ruby-on-rails

I have a submit button on my form:
= semantic_form_for record do |form|
= form.actions do
= form.submit 'Save'
I want to add an icon from FontAwesome, with link_to I can use a block:
= link_to record do
= fa_icon 'save'
Save
But this doesn't work with Formtastic's form.submit.
I also tried:
= form.submit fa_icon('save')
= form.submit fa_icon('save').html_safe
But both renders escaped HTML.
How do I add a font-awesome icon to the <button> tag?

Try using capture to set a label, as in:
- label = capture do
= fa_icon 'save'
Save
= form.button label
This only seems to work for form.button, but not for form.submit. You'll have to use form.button label, type: :submit.

Related

Rails form on a single page

I am trying to get each candidate belonging to a position have its own page. How do I implement this?
= form_for #vote do |f|
- Position.includes(:candidates).order(:name).each do |position|
= position.name
- position.candidates.each do |candidate|
= image_tag(candidate.image, :size => '50x50') if candidate.image.attached?
= candidate.name
= candidate.info
= f.check_box :c_votes
= f.submit "Submit"
When I use this code it creates a submit button for each candidate on the same page.

Change hidden_field_tag name

_form.html.erb
<% #subjectmodulelists.each_with_index do |modules,index| %>
<%= hidden_field_tag 'subjectModuleId'+index.to_s, modules.subject_module_id%><%= f.submit class:"btn btn-primary" %>
<% end %>
The above code I have in partial form actual field name was subject_module_id in my db. Here i changed to 'subjectModuleId'+index.to_s for store array of data.
I have following Error.
Mysql2::Error: Field 'subject_module_id' doesn't have a default value: INSERT INTO term_questions
Params passes Like:
"subjectModuleId0"=>"65", "subjectModuleId1"=>"66", "subjectModuleId2"=>"67",
In my controller
#question = TermQuestion.new
#question.subject_module_id = params[:subjectModuleId]
How I change the name into subject_module_id.
Thanks.
Pass array value to the hidden field
<%= hidden_field_tag 'subject_module_id[]', #subjectmodulelists.map(&:subject_module_id) %>
<%= f.submit class:"btn btn-primary" %>
This way you will get params like
{ "subject_module_id"=>[65, 66, 67] }
Which then you can assign
#question.subject_module_id = params[:subject_module_id]

How to add attribute for HAML block?

I would like to add class attribute for HAML = link_to "#" do block?
= link_to "#" {class: "class-name"} do
image_tag "image_path.ext"
Link Sample Text
The code above does not work.
You're missing a comma between the target link and the options hash. Try this
= link_to '#', { class: 'btn btn-primary' } do
= image_tag 'image_url'

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
}

Dynamically insert ruby generated HTML

I have a form where a user can elect to create more input areas (to provide more information). I have a link the user will click on and then the extra form inputs will be created. I'd like to use the rails form helpers so that I don't have to write the html myself. I've tried inserting the form helpers directly into coffeescript and saving the outputted html to a data tag on the link, but I can't get the coffeescript to execute the ruby code and I'm having escaping issues with the data attribute.
Here's the form:
= simple_form_for([#site, #zone]) do |f|
= f.error_notification
.form-inputs
= f.input :site_id
= label_tag "X"
= text_field_tag 'x_coords[]'
= label_tag "Y"
= text_field_tag 'y_coords[]'
= label_tag "X"
= text_field_tag 'x_coords[]'
= label_tag "Y"
= text_field_tag 'y_coords[]'
= label_tag "X"
= text_field_tag 'x_coords[]'
= label_tag "Y"
= text_field_tag 'y_coords[]'
= link_to "Add Point", "#", id: "add_point", data: { fields: label_tags }
.form-actions
= f.button :submit
When a user clicks the "Add Point" link, I'd like to add another block of:
= label_tag "X"
= text_field_tag 'x_coords[]'
= label_tag "Y"
= text_field_tag 'y_coords[]'
label_tags is in application_helper.rb:
def label_tags
label_tag "Z"
end
The problem is the output for the "Add Point" link is:
Z" id="add_point">Add Point
and the quotation marks are causing the link to come out with the text: "Z" id="add_point">Add Point"
I got the data attribute idea from this screencast
You cannot execute Ruby code from Javascript. When the page is requested all embedded ruby is evaluated and the results are what you get. The issue that I can see from you paste is that your label block is in the right data attribute but it's not escaped.
What you'll need to do is escape the quotes on the generated HTML going into that field and then unescape them via Javascript. You could use html_escape here like: data: { fields: h(label_tags) } (h is an alias for html_escape or you could do this yourself, manually.
def escape(str)
str.gsub(/</, "<").gsub(/>/, ">").gsub(/"/, """)
end
# later in the view for the form
data: { fields: escape(label_tags) }
And then your CoffeeScript would click handler would like:
function unescape(str) {
return str.replace(/((?:<)|(?:>)|(?:"))/g, function($1) {
switch($1) {
case ">":
return ">";
case "<":
return "<";
case """:
return '"';
}
});
}
$("a").on("click", function() {
var html = unescape(this.data("fields"));
$(".the-place-to-put-it").html(html);
});
I do not doubt a better solution exists and as of the posting of this answer I have not tested this to work (in theory it should). Ideally, you should just generate the elements with jQuery in Javascript and not depend on this method for doing this - yes, it's duplicate code duplicated between ruby and Coffee.
Simple solution for me was to replace the double quotes in my generated HTML with single quotes. In code:
= link_to "Add Point", "#", id: "add_point", data: { fields: label_tags.gsub("\"", "'") }
Also, had to use capture and concat in the helper method:
module ApplicationHelper
def label_tags
capture do
concat label_tag "X"
concat text_field_tag 'x_coords[]'
concat label_tag "Y"
concat text_field_tag 'y_coords[]'
end
end
end

Resources