lundi 10 août 2015

how to combine / shortcut two (ajax) requests, rendering the secon serverside if first is ok - in rails??

first: sorry for the question wording, I realy tried to find the best formulation. Others where "how to cross controller rendering" or "How to serverside redirect"

second: my code examples are syntactical wrong but I try to work out what my prob is, without flooding you with code

The situation is in principle classic:

I have a book with pages that have sentences wich have words. So 3 controllers (3 models) - I have more, but thats not the point

all fine working RESTfull & MVC "conform"

Now the editor page comes in, that lets you edit your book, via AJAX

Also a standard is in this case (I think) a request / code like this

sentence_edited:
    $ajax("update sentence")
    if ok
        $ajax("get book content HTML")
        if ok
            change_book_html   
    else
        error_handling  

and (and so on)

word_edited:
    $ajax("update word")
    if ok
        $ajax("get book content HTML")
        if ok
            change_book_html   
    else
        error_handling  

Since all this works fine, I want to get rid of the second ajax reuest doubling RTT and serverside security checks, and other stuff

what I would like to have is:

sentence_edited:
    $ajax("update sentence if ok get book content HTML")
    if ok
        change_book_html   
    else
        error_handling  

So what I did was to introduce an Edit(sub)Controller, so I have

BookController < EditController < ApplicationController
SentenceController < EditController < ApplicationController
WordController < EditController < ApplicationController

with

class EditController < ApplicationController
    before_filter do
        @inmplicite_redir=params.delete(:gimme_book) 
    end

#and - and thats the point where I got stuck:

    def render(*args, &block)
         if @inmplicite_redir
             # yes what?
         end
         super
    end
end

because rendering just an other controller/action render controller:book action: bookhtml renders the view, as the name says, but not calling the action

so in the section "#yes what" above I need something like

    def render(*args, &block)
         if @inmplicite_redir
             params.clear.merge  @inmplicite_redir
             BooksController.new.get_html
         end
         super
    end

but this does not work, and even if it worked, it dos not only smell, it stinks - I think

I do not care if the solution is not perfectly cosy and clean, because it is just a goody to not have 2 requests. Because the rest - the sourrounding is clean

Aucun commentaire:

Enregistrer un commentaire