0

At http://localhost:3000/books, I have an index page where I have a list of books.

enter image description here

When you click on one of the links, the action to which it is bound, book, gets fired:

enter image description here

However, when you click on one of the links from one of the book pages, the book action doesn't get fired:

enter image description here

Note that the URL does change when the links are clicked, from both the index page and the book pages, but the problem that I'm having is that book doesn't get activated when you click on a link to a book page from another book page. How can I fix a situation like this?

FYI, here is a repo where this problem can be reproduced.

GDP2
  • 1,948
  • 2
  • 22
  • 38

3 Answers3

0

The method book doesn't get called twice because your view is already setup. The change in the url only triggers an reactive update in your view.

What is it that you are trying to achieve?

  • Re: What is it that you are trying to achieve?, As you can see, `book` isn't run when you click on a book link from one of the book pages; I'm trying to have `book` be run even when you click on one of the book links from another book page. I'm guessing that I explicitly have to call `book` somehow. – GDP2 Aug 25 '15 at 14:32
0

As it turns out, Volt computations are a way to solve this problem. By using the params collection, you can attach computations to changes in the URL. I solved this issue and got book to run on changes in the URL like so: https://github.com/ylluminarious/volt_action_problems/commit/5fcefc16d7b14a980f68ce572a80f25540d21636.

GDP2
  • 1,948
  • 2
  • 22
  • 38
0

Sorry for the late reply. This is a known issue thats on my to fix list. As GDP2 mentioned, you can use .watch! to handle it, or the probably better way to do it is to write your controllers in a more functional way so that the data being pulled from params is used in methods instead of setting instance variables.

So for example, if your using the params in a query, instead of doing something like:

attr_reader :query
def index
  @item = store.items.where(name: params._name).first
end

You could do something like:

def query
  store.items.where(name: params._name).first
end

This might seem less efficient, but there's a lot of caching and this is pretty much just as efficient.

Once I get time though, I'll make it retrigger the action method when accessed data changes. (Sorry, just haven't gotten to it.)

Ryan
  • 956
  • 7
  • 8
  • Thanks, this is a nice alternative solution. Thanks also for working on re-triggering the action when accessed data changes, it's definitely appreciated. – GDP2 Aug 28 '15 at 12:20