TinyMCE and ActiveAdmin for Rails - tinymce

I am reacquainting myself with Rails and I am really liking Active Admin. I would like to get tinyMCE working with it for use in text areas. However, any instructions I find are incomplete. For some reason, I think that I am missing something really simple here.
So, for example, I have tinymce-rails installed (3.4.9) and followed the instructions (https://github.com/spohlenz/tinymce-rails). However, here's where I think I failed: actually initiating tinyMCE. According to the doc, I have two options:
use the <%= tinymce %> helper or...
initialize it like the following tinyMCE.init({
mode: 'textareas',
theme: 'advanced'
});
I tried adding the latter to my active_admin.js file to no avail.
If someone could guide me on this, I would be most appreciative.

I got it working doing the following things (outside of the install described at the repo)
In admin/my_class.rb:
ActiveAdmin.register MyClass do
form do |f|
f.inputs do
f.input :body, :input_html => { :class => "tinymce" }
end
end
end
In initializers/active_admin.rb:
...
config.register_javascript 'tinymce.js'
This was what actually got the tinymce.js script to show up in the head of the admin layout.
In javascripts/active_admin.js:
//= require active_admin/base
//= require tinymce
$(document).ready(function() {
tinyMCE.init({
mode: 'textareas',
theme: 'advanced'
});
});
After doing those things, that body input (text area) had a fully functioning editor on it.

Do your textarea inputs have a 'class' attribute or something that tinyMCE can hook into? Does it work from the javascript console (firefox/chrome)? Have you checked the presence of tinymce.js in the head(source) of your page.
I got it working with a form partial, and I had to give a class to the input element so tinymce could hook to it.
<%= f.input :literature_nld, :input_html => { :class => 'tinymce', :size => "80x4" } %>
Good luck

Related

Disable css in test environment for Rspec

I'm using rails 4 and rspec with capybara for testing.
I want to disable css in my test environment. How can I do that? Can anyone help me.
The simplest way - skip CSS files loading in your /app/views/layouts/application.html.erb for test env.:
<% unless Rails.env.test? %>
<%= stylesheet_link_tag "application", :media => "all" %>
<% end %>
Capybara allows you to blacklist specific URLs from being loaded. You can utilize this feature to block the loading of your CSS files. For example if your CSS is loaded from /assets/application.css:
RSpec.configure do |config|
config.before(:each, js: true) do
page.driver.browser.url_blacklist = [
"http://127.0.0.1:#{page.server.port}/assets/application.css"
]
end
end
You can confirm what assets are getting loaded by turning on Capybara's debugging. To enable debugging simply set the driver to :webkit_debug instead of :webkit
Capybara.javascript_driver = :webkit_debug

Devise error messages missing after custom redirect

I have a custom redirects in place for my devise sign up forms, I have two forms, one for an individual and another for a company. I have added this in the create action of the registrations controller:
if resource.company_form
redirect_to new_user_registration_path(company: true)
else
redirect_to new_user_registration_path
end
In doing this though I have lost all the devise error messages, as in the don't display any validation errors, so I need to send the error messages along with the redirect, don't I? However, I am not sure how.
So far I have tried printing the error messages to the console:
ap(resource.errors.full_messages)
[
[0] "Email can't be blank",
[1] "Password can't be blank",
[2] "Company name can't be blank"
]
Whereas this:
ap(resource.errors)
#messages={:email=>["can't be blank"], :password=>["can't be blank"], :company_name=>["can't be blank"]}
How would I get the error messages to be displayed above the form again?
The magic of devise error messages is made with respond_with method.
Hence you can change the redirect_to for a respond_with block
respond_with(resource) do |format|
if resource.company_form
format.html { render 'new', locals: { is_organisation: true } }
else
format.html { render 'new' }
end
end
and in your view
<% params[:organisation] ||= is_organisation -%>
Not sure if this will help, but you can customize this even further with your messages. First things first it's a good idea to actually ensure this works first. Then customize it further.
Add your toastr gem to your gemfile.
gem 'toastr-rails', '~> 1.0'
In your application.js you will need to add //= require toastr
In your stylesheet.scss you will need to import toastr #import "toastr";
Then run bundle in your terminal
In your views/devise/registrations/ folder and views/devise/password/ folder, the pages inside are your devise views that show the error messages. There you will find the error messages by devise. <%= devise_error_messages! %>
So what you wanna do, is customize these messages.
Now go to views/shared and create a new file and name it _devisemes.html.erb
<% unless resource.errors.empty? %>
<script type="text/javascript">
<% resource.errors.full_messages.each do |value| %>
toastr.error('<%= value %>')
<% end %>
</script>
<% end %>
After you have saved this file. Just go to the following files and find
<%= devise_error_messages! %>
Replace it with <%= render 'shared/devisemes' %> in the following files below:
views/devise/registrations/edit.html.erb
views/devise/registrations/new.html.erb
views/devise/password/new.html.erb
views/devise/password/edit.html.erb
Now logout and go to create an account and without an email, or password and test it. You will notice all the error messages from devise showing up with toastr.

Why doesn't simple form with bootstrap generate bootstrap-compatible forms?

I follow the API instructions, but the form behavior doesn't output what it says it should in the example and example source code.
I installed simpleform and bootstrap.
I did rails g simple_form:install --bootstrap
I see that the files have been generated.
Simpleform says to do code for an element like so
= simple_form_for #park, html: { multipart: true, role: "form", class: "form-horizontal" } do |f|
- if #park.errors.any?
#error_explanation
%h2= "#{pluralize(#park.errors.count, "error")} prohibited this park from being saved:"
%ul
- #park.errors.full_messages.each do |msg|
%li= msg
= f.input :address, label: 'address', autofocus: true, placeholder: "123 Fake Street Irvine, CA 90123", class: "form-control"
The layers of DSLs for simple output of forms is getting kind of ridiculous.
Many things wrong with the output
1) the wrapping classes don't match bootstrap's form-group, and instead it's still controls
2) the form doesn't stretch out 100% width like it should.
3) If an input isn't detected as "required", the label doesn't get wrapped, and it sticks off to the left of the screen.
Why isn't the API working?
The simple form generator command with bootstrap generates config initializers files for bootstrap 2.x
Thus you must modify the initializer files to spit out appropriate classes.
See the answer to this question.
Bootstrap3 and simple form

rails 3.1 auto-completion and realtime update in rails

Can anyone tell me how to easily implement auto-complete in rails 3.1?thanks. p.s. What's the best solution for simple_form to do this??
Besides, I also want to know how to implement meetup's RSVP instant push in rails?
I have the following form, but it didn't work, please advise.
%table
= simple_form_for(#book) do |f|
%th book name
%th
= f.input :name, label: false, placeholder: "add a name", :wrapper_html => { :id => 'autocomplete' }
= f.input :object, autofocus: true, label: false
%th
= f.button :submit, "Yes!"
I built a simple Rails 3.1 app using the jQueryUI autocompletion feature here. You simply add the jQueryUI library to your assets, add a css class or id to the actual text field that shall be autocompleted and put some javascript logic in your application, like:
$(function() {
$('#autocomplete').autocomplete({
source: '/movies/autocomplete'
});
});
with #autocomplete the css id of the text field. '/movies/autocomplete' is the actual route that will HTTp requested with the data that's put into the text field. In the movies#autocomplete action you then use that param to search for data for the autocompletion. Depending in what kind of jQueryUI autocompletion you use, you simply return an array like [{:label => 'FOO', :value => 'foo'}, ...] as JSON. label is the text you get autocompleted, value is the text that is put into the text field when clicking the label. I think and hope the code from the repo is quite understandable. Also, have a look at the JQueryUI library.
Though there shouldn't be a problem with simple_form, since it's just the text field that gets touched.
Best regards
Tobias

Asp MVC 2: Typed Editor Template

(I reference this tutorial in this text)
I want to use the Html.EditorFor (or Html.Editor) helpers.
If a UserControl needs additional data it is passed via
...EditorFor(model => model.Album, new { Artists = Model.Artists, ... })
In the UserControl it's accessed via ViewData[stringKey], ie
... new SelectList(ViewData["Artists"] as IEnumerable, ...
To me this smells a little fishy as I would prefer a strongly typed ViewModel which ensures that specific data is available.
I'm now a little bit stuck as I don't know wheater there's a "typed way" to find or I should accept this way as-is.
How did you solve this issue? Any help appreciated!
Lg
warappa
I would probably change my view model so that I don't need to pass this additional information. You could make for example an album has a collection of artists. Now all tha you will have to do is:
<%: Html.EditorFor(model => model.Album) %>
And in your editor template:
<%: Html.DropDownListFor(x => x.SelectedArtist, new SelectList(Model.Artists)) %>