I'm working on a rails app that is using ActionText and trix for rich text content.
I noticed that when I add multiple hyperlinks to an instance of trix, the first one works as expected, the others get prepended to the content that is already there in the box. In this screenshot, for instance I added the hyperlink to "implement" and that worked, and then I added it to "strategies" but the hyperlink got added at the beginning of the content.
I haven't added any custom logic to trix other than allowing for embedding media content. Any idea of why might this be happening? I also tried to remove the media content logic but the issue kept happening.
Any idea as to why this may be happening?
EDIT 1 - Code snippets + additional context
In my section.rb file I added a rich_text field
class Section < ApplicationRecord
has_rich_text :content
In the view
<div class="nested-fields bg-gray-100 p-5">
<div class="flex justify-between items-center mb-10">
<h6 class="text-sm"> Content </h6>
<button class="bg-red-400 text-sm text-white p-1 rounded cursor-pointer" data-action="nested-form#remove_association">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<div>
<%= form.rich_text_area :content %>
</div>
</div>
It's worth mentioning that this is not happening application-wide. On the same page I have another instance of actiontext that is working as expected.
class Lesson < ApplicationRecord
has_rich_text :prep_overview
<%= form_with model: [:madmin, record], url: resource.show_path(record), data: { 'tabs-target': 'form' }, class: 'flex flex-col space-y-6' do |f| %>
<%= f.rich_text_area :prep_overview %>
Another thing I noticed is that the issue seems to be broader than just hyperlinks not being added where they should. I've also noticed that once I add the first hyperlink I'm basically unable to add other text formatting such as bullet points.
#ilrock
can you include some code snippets for us to help you debug and hopefully fix the concern/issue you're into?
in case it's not possible, you may refer to Rails Guides on ActionText
hope it helps!
Using the this button_to helper:
<%= button_to "Add Beer", "/growlers/create", method: "get" %>
yields the following HTML:
<form action="/growlers/create" class="button_to" method="get">
<div>
<input type="submit" value="Add Beer" />
</div>
</form>
Which is exactly what is supposed to happen. However, as you can see, the HTML element is a <form>. I am wondering how to render this as a <button> for styling purposes (using Foundation).
I found this:
<%= link_to "<button>Add Beer</button>".html_safe, "/growlers/create", method: "get" %>
yields this HTML:
<a data-method="get" href="/growlers/create">
<button>Add Beer</button>
</a>
Which, though a <button>, doesn't seem perfect to me.
I am curious if there are any other solutions/workarounds to this issue.
If you wrap your button label in the button_to statement:
<%= button_to "/growlers/create", method: "get" do %>
Add Beer
<% end %>
It will render the wrapped text within a element inside the form.
props to apidock: https://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
Are you looking for button_tag?
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-button_tag
I have an application with a large Text field:
<%= simple_form_for(#language) do |f| %>
<div class="form-inputs">
<%= f.input :text_message, :input_html => { :class => "span8", rows: "12" } %>
</div>
And I'm trying to add this HTML and Javascript which allows me to click a button to add a predetermined value into the textfield. Right now it works but I can't figure out how to link it to the RoR textfield.
<form name="virtual">
<input type="text" name="text"/>
<input type="button" onclick="last_name()" value="Last Name" style="border:none;"/>
<input type="button" onclick="first_name()" value="First Name" style="border:none;"/>
</form>
<script type="text/javascript">
function last_name(){
document.forms["virtual"]["text"].value += "{last_name}";
}
function first_name(){
document.forms["virtual"]["text"].value += "{first_name}";
}
</script>
So my question is, how can I combine these two so that selecting the buttons will add "{last name}" or {first name} to the text_message field in the Language model, and not the plain html form.
Edit: To provide more context - later on these variables like {last_name} will be parsed out and replaced with their associated fields - but for now that's not relevant as all I'm trying to do is create buttons that input the actual string "{last_name}" or "{first_name}" into the textfield. This JS was the only way I could figure out to do this
Thanks!
Using simple_form_for, Bootstrap and Rails 3. In a form:
<%= f.input :upload, label: 'PDF file:' , input_html: {accept: ('application/pdf') } %>
I don't know how I'd style this so that the "choose file" button can have a different class ('btn btn-primary').
Additionally, when using with Bootstrap at least, its severely misaligned by default. See attached image.
Finally, how do I redefine the text from "No file chosen" to "Chose file" when there isn't one added yet, and show the file name when there is one.
This is how I do it:
In the view add your form file field and hide it
Add a styled additional field just to display the file name
Add a button to trigger the file browse dialog
<div class="control-group">
<div class="attach-set">
<%= f.input :real_file, input_html: { hidden: true }, label: 'Upload Attachment' %>
<div class="input-append">
<input id="file-display" class="input-large uneditable-input" type="text">
<a id="upload-btn" class="btn"><i class="icon-upload-alt"></i> Browse</a>
</div>
</div> <!-- /attach-set -->
</div> <!-- /control-group -->
In your JS (Coffee w/ jQuery shown), pass the click from the display button onto the real file input and when they select a file drop the file name in the display text field (I drop the path so that I don't see C:\FakePath....)
$(document).ready ->
# ------------------------------------------------------
# pretty-fy the upload field
# ------------------------------------------------------
$realInputField = $('#real_file')
# drop just the filename in the display field
$realInputField.change ->
$('#file-display').val $(#).val().replace(/^.*[\\\/]/, '')
# trigger the real input field click to bring up the file selection dialog
$('#upload-btn').click ->
$realInputField.click()
This worked great for me and only requires HTML
<label class="btn btn-primary">
Add a file!
<span style="display:none;">
<%= f.file_field :image, required: true, multiple: true, name: 'picture' %>
</span>
</label>
I ran across and am using Jasny's extension to Bootstrap 3. It seems to work well so far.
No JS required, just plain css
scss
.fileinput-button {
position: relative;
overflow: hidden;
float: left;
margin-right: 4px;
width: 110px;
height: 32px;
input{
opacity: 0;
filter: alpha(opacity=0);
transform: translate(-300px, 0) scale(4);
direction: ltr;
cursor: pointer;
}
}
html / slim
span class="btn btn-success fileinput-button"
i.fa.fa-pencil
span
| Select File
= f.file_field :cover_ar
I recommend using compass for cross browser compatibility
As #rafaelfranca said you can't style file input but you can add your own button which will be clicking your hidden original button. See example here http://jsfiddle.net/rUdf2/6/
Every Browser has a different type of file input field button and this makes it a pain. You can play a little with css. This has given me a basic styling with JS without the annoying "No file chosen" text in chrome and Safary:
$(document).ready(function() {
$(".your_button").css("width", "80px");
});
Otherwise the best solution is to hide it and show a fake one that intercepts the click:
http://duckranger.com/2012/06/pretty-file-input-field-in-bootstrap/
With respect to the question of how to show that a file has been uploaded, a basic solution with jquery file upload is to detect the upload complete event and replace some of your text with a success message (The exact file name I believe it is not possible to obtain with modern browsers):
$(".your_button").fileupload({
dataType: "json",
done: function(e, data) {
$(".place_for_your_text").text("File uploaded.");
}
});
In summary, a basic solution is to use javascript in your assets to:
Hide the annoying "No file chosen text" with css.
Place your "Chose file" text next to the button and give it a class you can reference.
Replace the text with "File uploaded"
No fancy shiz required:
HTML:
<form method="post" action="/api/admin/image" enctype="multipart/form-data">
<input type="hidden" name="url" value="<%= boxes[i].url %>" />
<input class="image-file-chosen" type="text" />
<br />
<input class="btn image-file-button" value="Choose Image" />
<input class="image-file hide" type="file" name="image"/> <!-- Hidden -->
<br />
<br />
<input class="btn" type="submit" name="image" value="Upload" />
<br />
</form>
JS:
$('.image-file-button').each(function() {
$(this).off('click').on('click', function() {
$(this).siblings('.image-file').trigger('click');
});
});
$('.image-file').each(function() {
$(this).change(function () {
$(this).siblings('.image-file-chosen').val(this.files[0].name);
});
});
CAUTION: The three form elements in question MUST be siblings of each other (.image-file-chosen, .image-file-button, .image-file)
If you are using Bootstrap, Simple Forms, Jasny, and ReFile, this post may be of interest to you refile simple_form undefined method attachment_field
<label class="btn btn-primary"
<%= f.input :upload, label: 'PDF file:',
class:"hidden", input_html: {accept: ('application/pdf') } %>
</label>
Works for me
Using class="hidden"
PS: I'm using Tailwind CSS
I am working on a search function on RoR that basically just has a cool visual effect when a user clicks the search button.
Here is the code in my view for search.rhtml
<html>
<head>
<title>Tutor</title>
<%= javascript_include_tag :defaults %>
</head>
<body>
<h1></h1>
<%= form_remote_tag :url =>{ :action => :search_results }, :update => "results" %>
<fieldset>
<legend>Tutor Search.</legend>
<label for="searchItField">Keyword Search.</label>
<input class="tfield" type="text" value="FIND YOUR TUTOR HERE" name="searchItField" id="searchItField" />
<span id="Submit_search">
<span id="Submit_search_hover"><input type="submit" id="Submit" value="Search" /></span>
</span>
</fieldset>
</form>
<div id="results">
</div>
</body>
</html>
Here is the search_results.rhtml which is what gets displayed in the results div in search.rhtml
<% for tutors in #tutors %>
<%= tutors.first_name %> <br/>
<% end %>
And finally here is my controller and actions.
class TutorsController < ApplicationController
def search
end
def search_results
#tutors = Tutors.find_by_sql('SELECT * FROM tutors')
end
end
What I want to do is basically create an effect that when the search results are loaded because of the ajax call that it would slide up. I am using jrails. Exactly how this site does it. http://keyonary.com/#/paste
In short, to get you started, you will need to do the following:
use observe_field on the input-field, which retrieves data from a controller-method and will update a certain div with the results
create a corresponding controller-method that will retrieve the items (use your search_results)
create a view for that controller-method that will show the data with the required effects and animation.