0

I am trying now to use my javascript to render a new view on click of an element, with this code;

$(document).ready(function() {
    $('.link-panel').click( function() {
        window.location.replace('/quotes/'+gon.gon_quote_id);
    });
});

and i get the following error:

Couldn't find Quote with 'id'=undefined [WHERE "quotes"."user_id" = $1]

quote_controller.rb:

  def show
    @quote = current_user.quotes.find(params[:id])
    gon.gon_quote_id = @quote.id
  end

def index
@quotes = current_user.quotes.all
# how to pass the individual quote object's id to gon here

end

I think that it must be the way that I've given the url argument to the replace method, can you help me with what it is that I'm doing wrong?

(gon setup is working fine as alert tests demonstrate.)

Thanks

jbk
  • 1,911
  • 19
  • 36
  • What is the routes file for this action? and in the javascript console what does `'/quotes/'+gon.gon_quote_id` yeild? – Michael Gorman Jun 07 '17 at 19:52
  • If the js file is a static file you put inside assets folder, then you simply can not pass controller value to this file! Because it is a static file... – wesley6j Jun 07 '17 at 20:04
  • I'm actually writing this direct into `application.js` just for now to get it working :/ – jbk Jun 07 '17 at 20:07
  • application.js is also static file. It does not take dynamic value from controller. – wesley6j Jun 07 '17 at 20:08
  • If variables are showing not defined, you also want to make sure that you see `window.gon` object defined in your rendered page's source. You could use view source in your browser to check it! Do you have `<%= include_gon -%>` in your HTML page, perhaps within `...` block? – vee Jun 07 '17 at 20:11
  • even whilst using `gem 'gon'`? And I saw it working in application.js a little while ago, now I've changed something to stop it working but I cannot see what. – jbk Jun 07 '17 at 20:11
  • `` revealed in inspector and `<%= Gon::Base.render_data %>` written in head of application.htl.erb – jbk Jun 07 '17 at 20:14
  • 1
    `gon_quote_id` seems to be missing within `window.gon={}`, are you checking this in correct page? – vee Jun 07 '17 at 20:17
  • 1
    As with your current code, I understand that the redirection will only occur if your are currently executing the `QuoteController#show` action as `gon.gon_quote_id` is defined only there. Hope this helps! – vee Jun 07 '17 at 20:21
  • 1
    AHA!!!, `gon_quote_id` is there in #show but not in #index, of course because I've not defined it in #index. What I'm trying to do is use this js to enable clicking of one of the quote objects displayed in index, to pass the `quote.id` into the `/quotes/..` url so that it redirects to quotes/show for the relevant quote. – jbk Jun 07 '17 at 20:23
  • Do you need to redirect? Can you not use plain links? – vee Jun 07 '17 at 20:26
  • I can and have used `link_to` in the html file, but I'm just trying to learn a bit of js and see how I can achieve the same thing via js. i.e. how from index view to allow js to pick up the quote.id of each individual object, so to inject that id into a url to then `window.location.replace` to take user to the show action for the object they clicked on. (perhas this makes no sense and demonstrates a number of fundamental misunderstandings of the funtions I'm playing with however?) – jbk Jun 07 '17 at 20:31
  • Perhaps capture each quote's id from the each block in the index action and assign to a variable to be picked up by js? – jbk Jun 07 '17 at 20:35

2 Answers2

0

You should use the id parameter in the URL:

window.location.replace('/quotes/?id=' + gon.gon_quote_id);
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
  • No longer results in `ActiveRecord::RecordNotFound in QuotesController#show` but still results in `undefined` with url `http://localhost:3000/quotes/?id=undefined`. – jbk Jun 07 '17 at 19:58
  • @jbk This means that `gon.gon_quote_id` is `undefined`. What do you get when you do `console.log(gon)`? – Michał Perłakowski Jun 07 '17 at 19:59
  • `console.log(gon) Object { } undefined` So so so strange, it was defined not 5 mins ago, as i tested it and saw it rtn the id in alert box, unsure what's changed to stop it being defined now. Can you see from my code how it's undefined now? Thanks Michal – jbk Jun 07 '17 at 20:04
  • @jbk I don't know Ruby, so I don't think I can help you further. – Michał Perłakowski Jun 07 '17 at 20:14
0

The script does not know that it's meant to access a variable from rails, you have to do it like so:

$(document).ready(function() {
    $('.link-panel').click( function() {
        window.location.replace('/quotes/'+<%= gon.gon_quote_id %>);
    });
});