jejacks0n / mercury

Mercury Editor: The Rails WYSIWYG editor that allows embedding full page editing capabilities directly inline.
http://jejacks0n.github.com/mercury
Other
2.63k stars 530 forks source link

Mercury fails to save to db #144

Closed bjhaid closed 12 years ago

bjhaid commented 12 years ago

Mercury was working fine, but really I cant tell when and where in broke in my app, I keep getting "mercury was unable to save to the url" I realised that it was updating a record that had the following:

when I append this to some other records, then saving works, I am getting confused here, I would have included the dirty hack, but the app is in production already, and Mercury breaks both in development and production.

Awaiting your response, thanks

jejacks0n commented 12 years ago

I don't know what you mean by records.. you mean database records?

bjhaid commented 12 years ago

jejacks0n, thanks for your response, I mean database records, I have tried updating to the latest version though, that doesnt fix, also for appending

I change the id's to match the database record id too

jejacks0n commented 12 years ago

"when I append this to some other records, they become editable,"

I don't understand what you mean here.. how can you append this to some database records?

"I am getting confused here"

No more so than I am. I'd love to help you, but I can't figure out what 1. your issue is, 2. what you want it to do.

I'd say open the debug console, and look at what's coming back from the server.. chances are pretty solid that's broken. If that's not what it is, it had to have been something that happened in an update recently, so consider locking your gem in your Gemfile to a specific commit (from a few weeks back or something). Here's an example:

gem 'mercury-rails', git: 'https://github.com/jejacks0n/mercury.git', ref: 'a2b16bcdc9'
bjhaid commented 12 years ago

I have locked the Gemfile to that particular commit, the one Ryan specified in his tutorial

I edit the database records from rails console and append the value to it, basically when I set debug to true, I check the firebug console mercury sends the following as post, which I think is adequate:

Parametersapplication/x-www-form-urlencoded content[page_content][typ... editable content[page_content][val... We are searching for partners Source content%5Bpage_content%5D%5Btype%5D=editable&content%5Bpage_content%5D%5Bvalue%5D=We+are+searching+for+partners

But rails keep returning routing error, and basically I am surprised why this works for records with:

My issue is that I noticed that I started getting: Mercury was unable to save to url (why this occurred I really cant tell, I followed Ryan's tutorial in out and it worked originally, I wanted to update a record yesterday, and noticed mercury had broken how and when I cannot tell) while fiddling around I noticed only a particular record was updating, on inspecting its elements I found it had that hidden field, and upon appending that hidden field to other records from rails console, they began to update via mercury.

I would like to paste my codes if that can clarify this, you can specify what parts you want me to paste and I would go ahead. Thanks

martinjlowm commented 12 years ago

What does your web log say? Knowing the exact error message, would help a lot.

bjhaid commented 12 years ago

martinjlowm, this is from rails server development log:

Started POST "/pages/3" for 127.0.0.1 at 2012-01-23 08:06:46 +0100

ActionController::RoutingError (No route matches [POST] "/pages/3"):

Rendered /home/bjhaid/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.9ms)

martinjlowm commented 12 years ago

And that is when you try to update a record, yes?

bjhaid commented 12 years ago

martinjlown: yes

martinjlowm commented 12 years ago

Ofcourse it is... the problem is rather simple. When you attempt to update a record, you use the POST method. I assume you use resources :pages inside your routes file. Resources, by default, uses the PUT method for updating records.

I don't recall Ryan creating a route specifically to mercury_update, but that is one solution. Mercury also lets you change what method to use for updating records. POST is for creating, PUT is for updating.

If you take a look at https://github.com/jejacks0n/mercury/blob/master/app/views/layouts/mercury.html.erb you can see some options are set, before instantiating the Mercury editor. You can change the save method here. For updating it should be PUT.

But changing this, would also affect creating new records. What I did was, changing the save method by doing what's described here: https://github.com/jejacks0n/mercury/wiki/Extending-Mercury Scroll down to changing functionality.

I changed it to a little to match all urls and use a variable to use either put or post. The code is on my laptop, so I can post the snippet later if you want.

If you want it working right away, inside your routes file do post "/pages/:id" = "pages#mercury_update"

edit: This method is far more complicated than the above line, but this follows the RESTful design and the above line does not.

custom_page_editor.js.coffee: https://gist.github.com/1662449

mercury.js (bottom): https://gist.github.com/1662477

Just a note here, mercury.js is set to load itself before JQuery, so if you have trouble with that. Make it require JQuery before itself.

With these snippets, add a data-save-method to an element with the id of content. And create a variable inside your show method in your PagesController e.g @SaveMethod = 'PUT'. Inside your view where content resides check for @SaveMethod and print out its content so the attribute will end up looking like data-save-method="PUT". Mercury will then read data-save-method, add the value to Mercury.saveMethod. The custom page editor will then use this variable to determine whether it should use POST or the variable's value (PUT in this case)

edit 2: Just watched the railscast again and noticed Ryan does add the route and makes it a member to the pages resource. Are you sure you did that step too? It should be able to find the mercury_update method then. Remember this has to be done for each resource!

bjhaid commented 12 years ago

Hi martinjlowm:

Thanks alot, sorry it took me so long, but my day has been very busy, i followed Ryan's tutorial, and it worked later broke, what broke it I cannot explain, but thanks for all the details, I set put for the member in the route, it worked for only one resource, every other resource is failing, I guess I would have to look at the JS files later when I have that much of time, thanks for the assistance once again, I am grateful

martinjlowm commented 12 years ago

Just an extra tip here, have you looked at your app's routes? run 'rake routes' in the terminal. Make sure it returns mercury_update at POST, if it does not. It's a problem in your app and not Mercury.

seocahill commented 12 years ago

Hi guys I'm having a similar issue and I'm a little confused.

The mercury save event is a put request right?

Then why in Ryan Bates RC is the member route a post?

Sorry for the dumb question, I'm getting a RoutingError for the above reason, thanks.

jejacks0n commented 12 years ago

Has he posted an updated Railscast? I think the one up there is a bit dated -- and I provided him with a list of things that he can cover off on now.. Anyway, if you ran the install generator you should get all the routes as they're needed to be to save -- at least I believe so.. Can you explain some background so I can understand where you are?

Thanks.

seocahill commented 12 years ago

This is still based on the original rc

Hey thanks for the quick response, in the railscast there's a custom controller action that's used for saving the page, he creates a member route for the appropriate resource, in his example :pages, in my case :events like so

resources :events, only: [:index, :show] do member { post :mercury_update } end

when I try to save my event page I get this in the console

Started PUT "/events/2/mercury_update" for 127.0.0.1 at 2012-10-09 23:38:48 -0700 23:38:48 web.1 | ActionController::RoutingError (No route matches [PUT] "/events/2/mercury_update")

seocahill commented 12 years ago

I see the mercury resource route in rake routes

/mercury/:type/:resource(.:format) mercury#resource

How would you suggest implementing an update action in a standard rails app?

And mercury is awesome btw, excuse my manners!

alexonozor commented 11 years ago

please i am still getting the dialog box that said "Mercury was unable to save to the url: http://localhost:3000/articles/mario-balotelli-lands-into-trouble-for-smoking-in-a-toilet" after i attempt to save the page why

my model----- class Article < ActiveRecord::Base attr_accessible :category_id, :content, :excerpt, :title, :image, :remote_image_url mount_uploader :image, ImageUploader belongs_to :category validates :title, :content, :excerpt, :category_id, presence: true validates :title, uniqueness: true

extend FriendlyId friendly_id :title, use: [:slugged, :history]

def long_title

" #{title} - #{created_at} "

end

end

my_article controler

def mercury_update article = Article.find(params[:id]) article.title = params[:content][:article_title][:value] article.content = params[:content][:article_content][:value] article.save! render text: "" end

my_show.html.erb

<%= raw @article.title %>

<%=raw @article.content %>

$(window).bind('mercury:ready', function() { var link = $('#mercury_iframe').contents().find('#edit_link'); Mercury.saveURL = link.data('save-url'); link.hide(); });

$(window).bind('mercury:saved', function() { window.location = window.location.href.replace(/\/editor\//i, '/'); });

gvarela commented 11 years ago

Mercury is using jQuery Ajax and assumes a json response. You are not responding with valid json.

egeersoz commented 10 years ago

If Mercury assumes a json response, how do I redirect the browser to another page? For example, I want to make it so that when the user clicks save, they are taken back to the page itself (without the editor). The server side works fine at the moment (changes are saved in the db) but Mercury throws that generic "Mercury was unable to save to the url" error and prevents the redirect.

egeersoz commented 10 years ago

Never mind, found the answer here: http://stackoverflow.com/questions/11525665/how-do-i-get-mercury-editor-to-redirect-to-a-new-resource

It would be great if Mercury supported server-side redirects.

jejacks0n commented 10 years ago

You'd know how to respond with a server side redirect but not adjust the client side save method... Right.

egeersoz commented 10 years ago

My apologies. I'm still pretty new to web development.

rkrevolution commented 9 years ago

This is still an issue for me... can you please post how you solved it?