Topic: render partial, drag'n drop and session

Hi all,

I'm trying to render a partial that shows session data. It's triggered by a drag and drop using scriptaculous.

The problem is that the informations are stocked into the session (when I refresh the browser the data and the notice show up as it should) but don't get refreshed with the partial rendering. There's no error in the log.

I've check on the api and google for more info on render and ajax interaction but it did not help.


###view new.rhtml###

<div id= "content">
    <%= render :partial => "form"%>

    <div id="drag1_<%= @one %>" class="drag"><%= @one%></div>
    <%= draggable_element "drag1_#{@one}" %>
    <%= drop_receiving_element "str",:url => {:action => "add_attribute", :att => "str"}%>
</div>

###partial _form.rhtml###

<div id="notice">
  <%= @flash[:notice]%>
</div>
<%= form_tag :action => "create" %>
        <table id="attributes">
        <tr>
          <td>Strenght:</td>
          <td><%= @session[:character].str %></td>
          <td><div id="str"></div></td>
        </tr>
        </table>
<%= end_form_tag %>

###controller ###

def new
  # Return a character object. If there's already one cached
  # in the session,it use it, otherwise create a new one
  # and add it to the session
    @session[:character] ||= Character.new
end
def add_attribute
  #Get the id and the attribute in this format id_attribute
  #and split it after the "_" to take the attribute in index 1
    @attribute = params[:id].split("_")[1]
    @att_name = params[:att]
   
    case @att_name
      when "str"
        @session[:character].str = @attribute
    ###I took the other case out its simpler to read...###
    end

    @flash[:notice] = "Updated " + @att_name
    render :partial => "form"
  end


ahhh...enough! time to sleep! tongue

Re: render partial, drag'n drop and session

It sounds like the partial isn't being rendered onto the page - the returned data from the action is just being lost in Javascript land. I'm not that familiar with draggable elements, but it looks like RJS works with sortable elements. So you may want to try that to update the notice, etc.

Railscasts - Free Ruby on Rails Screencasts

Re: render partial, drag'n drop and session

Thanks Ryan, it puts me on the rails...

I've installed firebug to see the XMLHttpRequest result and the response result. The data is return but is not replacing the partial date, as you said...lost in the javascript land...I could not say! tongue

I fixed it that way, with the help of the topic you gave me. Not sure it's perfect but seem to work well on first sight.

### controller ###

render :update do |page|
  page.replace_html "character", :partial => "form"
end

### view ###

<div id="character">
   <%= render :partial => "form"%>
</div>


I think I understand why it wasn't working before, tell me if I'm wrong.

The only difference with that exemple and the scriptaculous cart exemple is that they iterate through the data in the partial (in the controller too for the sortable element). I was not doing that, I was just printing session data.

Now, is it possible that iterator force the rendering but not a simple print of data?

Sorry if I can't put it simpler, my french keep me from having a perfect english wink

Last edited by infernalsirius (2007-01-05 00:12:29)

Re: render partial, drag'n drop and session

Before you were simply returning the HTML you wanted to replace without specifying what you wanted to replace. With RJS however, the action is actually returning Javascript which is run that handles the replacing, specifying the HTML to update and which element to update.

Railscasts - Free Ruby on Rails Screencasts