mahu mahu - 1 year ago 89
Javascript Question

Rails 4 and JQuery - load specific data after navigating

I have no idea where I should begin with the following, that I want to do.

I have a "Welcome Page" where the logged in user gets displayed a list of tickets assigend to him.
In the navigation he can navigate to the "normal" tickets list view, where he gets a list of tickets and if he chooses one in a sepreate div container the _show view of this single ticket is loaded with this code:


$("#current_ticket").html("<%= escape_javascript(render partial: 'tickets/show', locals: { ticket: @ticket } ) %>");

Now I want, that if the user clicks on the Ticket on his welcome page, that he gets navigated to tickets_path (which works:

<td><%= link_to, tickets_path %></td>

) and the ticket he clicked on gets loaded in the div container on this tickets_path page.

This is the show part of the tickets controller

def show
@tickets = Ticket.all
respond_to do |format|
format.js {render layout: false}

Can you help me finding a solution for that?

Sorry, I think you missunderstood, what I am wanted to tell you - maybe because of me describing wrong. I have the following views (see the screenshots):

Welcome Page:
Welcome Page

Tickets "index"
Tickets index

if a user uses the navigation and clicks tickets he is directed to the tickets index page where all tickets are listed (typically for rails apps). When he now clicks on a ticket, the tickets "show" data gets rendered as a partial in a div on the right via JQuerie. (The code I posted above).

What I now want is, that if a user is on the welcome page and clicks on a ticket, he should get directed to the tickets index page (tickets_path) -> works, and the ticket he selected on his welcome page should be rendered as partial in the div, just as it works when he navigates to tickets and chooses a ticket there.
Thats the point where I am struggeling.


Answer Source

I resolved it myself. In my welcome view I call the tickes_path with the id of the ticket I want to render

<td><%= link_to, tickets_path(id: %></td>

In the tickets controller index, I find this ticket and save it in a variable

  def index
unless params[:id].nil?
  id = params[:id]
  @ticket = Ticket.find(id)


And in the ticket index view, I render the show partial with that ticket from the variable

<div id="current_ticket">
  <% unless params[:id].nil? %>
      <%= render partial: 'tickets/show', locals: { ticket: @ticket } %>
  <% end %>


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download