Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to order stimulus reflex morph asc?

spacerobotTR asked in Javascript

I have a morph for stimulus reflex that updates a portion of the page when todos are created. What the customer would like to have happen is that when they create new todos they are added to the bottom of the list. Currently they are added at the top. I have tried a bunch of different ways to get it to sort but have been unsuccessful so far. If I manually refresh the page it sorts correctly by adding the order by to the controller, but when I add a new todo it always puts it at the top. Any ideas on how to do this?

def toggle
todo = Todo.find(element.dataset[:id])
todo.update(completed_at: (todo.completed_at? ? nil : Time.current), lastUpdatedBy: current_user.id)
morph "#todo-#{todo.id}", ApplicationController.render(
partial: 'todos/todo',
locals: { todo: todo }
)
end

Reply

My understanding is that it re-runs the controller when it compiles, so if you want the sort order to be different you need to change that in the rails controller itself. I'm guessing the index action is where you're rendering from, so that's where that would happen.

Reply

I thought that it ran the controller methods as well. In my show method I have it sorted ASC. When I refresh the page it does sort them correctly, but when I create new ones it always puts the new one at the top of the list.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.