Rails nested layouts - possible to use turbolinks 3 replace partial?

274 views Asked by At

After learning some react-router and ember routing, I really appreciate the idea of nested ui.

Trying to figure out a good way to push partials into divs in 'native' rails and stumbled up on this simple way of editing something in place.

plans/index.html.erb

<% @posts.each do |post| %>
  <div id="content">
    <a onclick="Turbolinks.visit('/posts/8/edit', {change: 'content'})" >Edit</a>
  </div>
<% end %>

plans/edit.html.erb

<div id="content">
  <%= form_for @post do |form| %>
    <%= form.text_field :name %>
  <% end %>
</div>

Since both files have an id="content" it correctly puts the edit.html.erb content into the index file in place while maintaining the rest of the page state.

I like the way this feels, much nicer than creating a remote request, adding a controller action, creating a new js.erb file, and then forcing the dom update with jquery. It would be super handy for making something like nested tabbed content or master/detail pages with simple html.

However with the code above, how would I access the post ID in the Turbolinks.visit call? Currently I just hard coded it to an id I know exists to see if I could get it up and working. But now that it is I can't figure out how to put the post ID into the nested string.

1

There are 1 answers

2
Yury Lebedev On

You could pass the post ID via string interpolation. Try the following code:

<% @posts.each do |post| %>
  <div id="content">
    <a onclick="Turbolinks.visit("/posts/#{post.id}/edit", {change: 'content'})" >Edit</a>
  </div>
<% end %>