0

In my Rails app, I'm trying to view a model via its' URL /models/:id.

I'm generating the URL two different ways: in JavaScript and Rails, though it seems to be randomly that the controller does not recognize the id-parameter in its' action.

1st way in JavaScript:

<a href='/conversations/" + conv_id + "'>" + conv_subject + "</a>

where conv_id is the id of the model I want to visit.

Same bug occurs when I'm doing it the Rails way:

<%= link_to "Conv" , conversation_path(22) %>

Where I've just put the id of the conversation by hand.

The controller action looks like this:

def show
   gon.current_user_id = current_user.id
   gon.conversation_id = params[:id]
 end

I'm displaying the contents via AJAX. There's more code handling that in the action. The key point is, though, that the controller does not seem to recognize params[:id]. Only after I pressed F5, after the first visit of /conversations/:id, the controller recognizes it.

Thanks in advance!

EDIT:

It happens that the controller recognizes the id, though GON wouldn't pass it to the javascript in time

EDIT:

The problem probably lies under the asset-pipeline, because all my scripts requiring GON are laying in the pipeline, which is only reloaded with the proper GON-Variables, after I've pressed F5

EDIT:

Since the problem lies in my JavaScript here's the approach I've taken so far. I tried to outsource all GON-Variables used in the Asset-Pipeline via function-parameters, which are passed to the functions in the views.

Asset-Pipeline function:

// Load n messages
function loadMessages(page, n, conv_id) {
    $.ajax({
        url: '/conversations/ajax/fetch_messages',
        data: 'n=' + n + '&page=' + page + '&id=' + conv_id,
        dataType: 'JSON',
        type: 'GET'
        ...
    });
}

View function:

<script>
    $(function() {
        // Load 10 Messages when the page is loaded for the first time
        loadMessages(1, 10, gon.conversation_id);
    });
</script>

EDIT:

This is what GON gives me when rails says it doesn't know the ID. It's kind of weird.

What the debugger tells me:

//<![CDATA[
window.gon={};gon.current_user_id=3;gon.conversation_id=22;
//]]>

What the inspector tells me:

//<![CDATA[
window.gon={};gon.load_n_feedbacks_path="/feedbacks/ajax/load_n_feedbacks";gon.user_id="3";
//]]>

2 Answers 2

1

Your problem is you're passing a naked object to the link_to url option. Rails either needs a URL helper, :action / :controller arguments or a pure URL

You're best using something like this:

<%= link_to "Conv" , conversation_path(22) %>
Sign up to request clarification or add additional context in comments.

8 Comments

Yeah, thanks. Made a big typo there when I posted the question ^.^ I'll edit my question
Thanks for update - have you tested the conversation_path way? I mean, is your path actually called conversation_path? I just used that as an example
Using the path method or the raw model does not make any difference, tbh. The link_to helper works both ways :-)
Okay, so your problem is with your javascript links?
Yes, I already tried to outsource all GON-Variables used in the asset-pipeline to function-parameters, which I pass to the functions in my views. I'll post an example
|
0

I've been including GON via include_gon in my application.html.erb.

The html was not refreshing when clicking on links and thus weren't the GON-Variables. I simply followed this answer: gon is not defined error in javascript. It says to put all your include_gons in the views who require it in their JS.

Comments

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.