#296 Mercury Editor
Mercury allows you to edit a section of HTML directly in the web browser through a WYSIWYG editor. Here I show how to integrate Mercury into a Rails app and save the changes back to the database.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Awesome episode as always, thanks!
One question, though: Do you need to add an id to the edit link?
I usually filter by the
data
attribute. I mean, instead of this:I usually do something like this:
Am I missing some obvious drawback by using this approach?
It's actually slower (Sizzle uses document.getElementById when you do $('#..'), actually). Your solution is a good idea when you have multiple links, but just having one makes the id a better approach.
Very nice episode !
Thanks for the cast! Looks great. Is it possible to edit the source html directly? That would be a must have for me to use Mercury Editor.
Yes, try last button <>
A, thx, cool. a bit hidden though :)
Nice editor, but what about rendering a flash if the editing doens't pass the validation?
I'm actively maintaining the project, and I'm interested in what you mean here.
It may be easier and not get lost if you submit ideas on the github project https://github.com/jejacks0n/mercury -- with some more details.
This library appears to be very high quality and very well thought out. It doesn't pigeonhole you into any set back-end implementation, and focuses on a very flexible and intuitive front-end.
-Doug Puchalski
PENSCO
fullware
Mercury-rails has a dependencies on Formtastic and Paperclip, which I find personally annoying. I tend to use Simpleform and Carrierwave myself, so I'm working on a fork that will remove these dependencies and employ a concept of providers for image-handling. Not sure what to do about Formtastic. It's almost syntactically compatible with Simpleform, but it's use is quite limited. I think a way to delegate options form building to your application would be better, then you'd be free to handle it however you like. All in all, Mercury is awesome, and I'm using it myself. Kudos to Jeremy for sharing it.
Actually you are right, mercury-rails does have a few conventions (using Formtastic and Paperclip), but Mercury (the editor) doesn't use any of these dependencies, it's only the Rails backend that uses it. But you are free to use any backend you want. The forms for the modals can be customized so you can do without both Formtastic and Simpleform if you want, and I myself use Carrierwave with Mercury without ever lookign at Paperclip.
So I don't see how it is annoying that Mercury includes a few simple examples/conventions for you to get started without almost any coding on the server side.
So, yeah.. it does have these dependencies, but I will likely drop formtastic -- as it was only used as a convenience for the example snippet options form. I've been meaning to for a while now.
Thanks for the putting this screencast together! So awesome! And as always, useful -- even if it is my own project.
I've taken notes on some things that could be made nicer or simplified, so it's been invaluable for me. And I'll be pushing updates that address some of the things that could be improved on in the next few weeks.. I've also update the documentation to include a link to this railscast.
It would be awesome to integrate the history button with something like paper_trail. Is that the intention? I couldn't figure out how to implement this if it's already there. Amazing editor by the way!
Thanks for the kind words.. And yeah, that's the basic idea, but it becomes pretty heavy on the server side and I've intentionally kept that to a minimum (see complaint about formtastic and paperclip above).
You can just adjust the configuration to point to a different route/path when loading the history panel, so it's as simple as writing a view that you want to see. =)
How do you add authentication checks on the
/editor/.*
routes?I mean, I know you can:
But I'd prefer the user be kicked out of the editor in case he/she has a bookmark to the editor and isn't logged in.
Simple, add a conditional statement to the line where you include mercury.js. Without this file Mercury never loads, so something like this would solve your issue:
<%= javascript_include_tag('mercury') if current_user %>
The best way i've found to do this is to just add a route in your routes file like:
Then create a new controller that inherits from MercuryController, like:
Then, you can get all the functionality of Mercury, but limit access on requests like that. That might be a bit closer to what you were looking for.
Oh, and be sure to add that route before the mercury routes in your routes.rb file
I'm having troubles with mercury params (Rails 3.1.1rc1). I can read from console
{"content"=>"{\"article_title\":{\"type\":\"editable\",\"value\":\"hello-ise\",\"snippets\":{}},\"article_body\":{\"type\":\"editable\",\"value\":\"\n \tworld\n \n \t \",\"snippets\":{}}}", "locale"=>"fr", "id"=>"1"}
Somehow, under rails 3,1, mercury seems to escape parameters. And params in controller is wrongly interpreted.
Am I the only one to feel stupid?
Thanks to enlight me
I too had an issue with this, you have to do some magic converting the param to a correct JSON format, but the easier solution (at least for Rails) is to use the
form
method to send the data, instead of the defaultjson
method.Thanks for advice...
Change should be in app/layouts/mercury.html.erb
saveStyle: null, // 'form', or 'json' (default json)
Changing null to 'form' did solve my problem.
Just to tell because I have been looking in mercury.js for quite a long time :-).
When i try to install mercury , and run rake mercury_engine:install:migrations
i get the following error
rake aborted!
Don't know how to build task 'railties:install:migrations'
any idea why this happens ?
Won't the browser just cache mercury.js? If so is it still necessary to stop it from loading on every request?
Nice episode as usual, thanks a lot.
I'm wondering how to activate Mercury editor on the 'new' view.
I tried redirecting /pages/new to /editor/pages/new like this :
but without success.
I'd like to keep the usual new_path, which is conventional.
Hi, does anyone know how to use the snippets function?
I'm exploring this plugin for our company website so that the sales team can easily update content. So far I'm loving it, but the image uploading is a little bit mysterious to me. Maybe someone can explain the steps I'm missing to use the images feature. I tried dragging and dropping in an image and I'm receiving this error: "Unable to process response: TypeError: Cannot read property 'url' of undefined." I noticed in the server logs that the image was processed successfully with paperclip but it saves the image to public/system/images. Is there a way to configure where the images get saved? How would I go about customizing the image model?
Same issue here. Did you find a solution to this problem?
The "mercury:saved" trigger doesn't seem to exist anymore.
I've having a problem with this too - did you ever solve this?
Same here... this is driving me crazy! I've tried everything I can think of to get the access to saved event... nothing is working.
My ugly, and hopefully temporary, fix:
Mercury.PageEditor.prototype.save = function() {
var url = Mercury.saveURL ? Mercury.saveURL : this.iframeSrc();
var data = this.serialize();
Mercury.log('saving', data);
if(this.options.saveStyle != 'form') data = jQuery.toJSON(data);
var method = (this.options.saveMethod == 'PUT') ? 'PUT' : 'POST';
jQuery.ajax(url, {
type: method,
dataType: this.options.saveDataType || 'json',
data: {_method: method, content: data},
success: function() {
Mercury.changes = false;
Mercury.trigger('saved');
},
error: function() {
alert("Mercury was unable to save to url:\n"+url);
}
});
};
The missing line was:
Mercury.trigger('saved');
Just a quick update: the two problems with the save event not getting fired and image drag&drop not working will be resolved if you update to the latest version from the git repository. (http://jejacks0n.github.com/mercury/) Note that you will have to rename your table to mercury_images instead of images if it already exists.
I can't get any events to fire:
Notta :(
Event handler declaration has changed in Mercury... try:
The answer lies here
https://gist.github.com/1581146
Correct, it has to go into 'onload' -- you can't just slap it at the bottom of mercury.js anymore
Current Gem:
gem 'mercury-rails', git: 'https://github.com/jejacks0n/mercury.git', ref: '6d9c99fe20958ed87e4f1a05e9de19825af702ef'
My mod to mercury.js
With Rails 3.1.3 this does not seem to work. Mercury is escaping parameters. Here is a part of params hash:
Processing by PagesController#mercury_update as JSON
Parameters: {"content"=>"{\"page_name\":{\"type\":\"editable\",\"value\":\"about\",\"snippets\":{}},\"page_content\":{\"type\":\"editable\",\"value\":\"{\"page_name\":{\"type\":\"editable\",\"value\":\"about\",\"snippets\":{}},\"page_content
I googled and found a similar issue: https://github.com/jejacks0n/mercury/issues/85
Just saw that someone above has posted the same problem. Trying that solution.
As suggested by Jean, changing null with 'form' makes things work!
mercury.html.erb
saveStyle: 'form', // 'form', or 'json' (default json)
What is the difference between https://github.com/balupton/mercury-rails and https://github.com/jejacks0n/mercury ?
I think the later is what we are talking about. the former is abandoned.
Any way to use this in a simple form/textarea?
Would like the wysiwyg and GUI without the additional backend requirements.
First of all - thanks for the awesome Railscast.
I had same issues as described above. Solution:
_ read the README in the git repository https://github.com/jejacks0n/mercury/ exactly
_ use gem 'mercury-rails' in your Gemfile without pointing to the git repository
_ fire bundle update
_ make a backup of your changes in mercury.js
_ make a backup of your changes in mercury.html.erb
_ fire rails generate mercury:install
_ use the changed eventhandler declaration described from Chris Sessions above
_ use saveStyle: 'form' in mercury.htm.erb
_ throw away your backups - you don't need them ;-)
You should not have problems anymore. Relax!
Cheers
Andy
Can someone kindly advise how I can resolve this error? I got this after installing the gemfile. I am new to rails and ruby. Appreciate it!
ExecJS::RuntimeError in Articles#index
Showing C:/Sites/dummy/app/views/layouts/application.html.erb where line #6 raised:
SyntaxError: octal escape sequences "\00" are not allowed on line 512
(in C:/Ruby193/lib/ruby/gems/1.9.1/gems/mercury-rails-0.3.1/vendor/assets/javascripts/mercury/regions/editable.js.coffee)
This was fixed very recently. It works if you use the newest commit (as of right now):
Thank you Jacob. That works until I tried to save, I will still get the 'unable to save to the url' error.
I followed the instructions accordingly but no luck.
Would you know the likely reason why this happen?
+1
thanks ;-)
Thanks for this great railcast!
Is there a tutorial for how to let user drag and drop images into the mercury enabled editing space?
Thanks!
Pier.
It works well in rails 3.2.3 with small change.
Be carefull , 'var saveURL' changed to 'var saveUrl' at the latest version of mercury.
Thanks.
+1
+1
+1
+1
+1
+1
If anyone still having a problem with saving the changes then add the following lines in your mercury.js file. Thanks to @seventhsense
thanks!
I'm pretty new to rails and mercury and I have some troubles to save.
I've implemented the editor on one controller and I want to send the save request to the same controller. I want to update so i use the "PUT" method right.
But the lo tells me that there is no route to url.
Maybe you can help me. Thanks!
Now it works, it was a routing problem.
Hi guys, still have save url error with the latest Mercury version. Are there any solutions?
Sorry, I paid not enough attention to controller file. Everything works!
With mercury 0.9 the posted js didnt work for me anymore so I tried araound a bit... This works for me, maybe somebody needs this,...
I'm trying to use the newer Mercury editor and it is not saving. If anyone can help I would appreciate it:
http://stackoverflow.com/questions/14366307/mercury-editor-0-9-0-cannot-save-changes-in-rails-3-2-11
starflyer59 -
I did get it to work with this javascript added to the end of mercury.js:
Could be my setup but Mercury is running 'onload' twice. i had to do one of these:
Saving works, but there is no redirect when saving. Anyone else having this issue?
Yes I was having this issue. I got around it with the following:
please after every thing i was unable to edit my app content
Just want to point out the massive acronym typo in the description.
It's WYSIWYG as in "woh-zee-wig". What You See Is What You Get.
I can't for the life of me figure out what "WISYIWYG" could stand for...
Wild Industrial Skeletons. You and I Will Yell Greatly?
I don't know how to get the link of an image I upload in a page, with a data-mercury="image".
For example, i have this:
<div> id="page_content" data_mercury="full"></div>
I get it in the controller like this:
params[:content][:page_title][:value]
How to do with ?:
<img id="article_image" data-mercury="image">
Thank you
Following the screencast, but using 'head' instead of the commit used in the screencast, I get this:
Any suggestions on how to install the migrations?
Use
*= stub mercury.css
and//= stub mercury.js
to exclude Mercury by default then include it explicitly on editable pages.This has an advantage over removing the
require_tree
line in that all your javascript and css files will still be included without having to explicitly add them.This is new to Sprockets 2.2.0 and up, I believe.
Great screencast, thanks Ryan.
Currently requirements for a markup are different from what is described in the screencast. Check the source code of the official demo page http://jejacks0n.github.io/mercury/
Hey guys, is there anyone who can help me installing the mercury editor on _form.html.erb? I want to use it when writing articles for my blog.
Do mercury.js have to be in assets/javascripts?
remove //= require_tree . in application.js is the only way to prevent load it on all pages?
When i am running this command on rails 4:
rake mercury_engine:install:migrations
Then I am getting this exception:
rake aborted!
Don't know how to build task 'mercury_engine:install:migrations' (see --tasks)
Does anyone know how to solve it ?