0

I was creating a project for last days. Its a task assigning project to employees in rails 4. In this project the admin can change the order of the task. For that I am using jquery ui sortable.So when admin drag the tasks in specific order, an ajax request is send to the rails controller. Where I could get the position of the sortable element. I can save it to database.But cannot render on to the html page.

My Html file

<% content_for :head do %>
    <script>
        $(function() {
            $( "#sortable" ).sortable({
                revert: true,
                stop: function(event,ui){
                    var datas = { 
                        top: ui.position["top"],
                        left: ui.position["left"] 
                    };
                    $.ajax({
                type: "POST",
                url: '/task/position',
                data: datas,
                success: function(data){
                    console.log(data);
                    },
                    dataType: 'json'
                });
                }
            });
            $( "#draggable" ).draggable({
                connectToSortable: "#sortable",
                opacity: 0.35,
                helper: "clone",
                revert: "invalid"
            });
            $( "ul, li" ).disableSelection();
        });
    </script>
<% end %>

<% @page_title = 'Tasks' %>

<% if !flash[:notice].blank? %>
    <div class="notice">
        <%= flash[:notice] %>
    </div>
<% end %>
<div class="big">
    <%= @top %>
    <strong>Task Assigned</strong>
    <ul id="sortable">
        <% @tasks.each do |task| %>
        <li>
            <%= link_to(task.task_name,{:action => 'show',:id => task.id}) %>
        </li>
        <% end %>
    </ul>
</div>

My Controller looks like this

  def position

    @top = params[:top]
    value = {
      :top => @top,
      :test => "hiuhiuh",
    }
    respond_to do |format|
      format.html
      format.json { render json: value}
    end

  end

My doubt is that can I access the variable @top directly in the html page.I tried but cant.But the json response has the data because I am passing it through that.Can I take the variable @top directly in the html page like <%=@top %>.

Also I need to get a logic for saving the position,So browser refresh did not refreshes the sortable elements position.

1 Answer 1

1

Yes, in your html view you can directly reference the @top variable you set up in your controller.

Sign up to request clarification or add additional context in comments.

2 Comments

But When I called in the html page its not displaying anything.
Check your ajax parms, and make sure the request is not using json as the type (vs html), otherwise your controller will render a json serialized version of that object. If you choose to stick with json, just parse it on your client side (js)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.