Edit/delete buttons in lists
Closed this issue · 5 comments
How would you go on for adding edit/delete buttons for each item in a collection so that they can be shown alongside the post name (for instance)?
Do you create each link using the Proc syntax and pass them in as keys which seems a little messy or is there a way to pass in post name as a key and pass in an object or an id or something for the edit / delete buttons?
posts/index.html.erb
<%= render_cached 'post',
collection: @posts,
replace: {
name: -> (post) { post.name },
...
%>
The kind of output I'm trying to get is (simplified):
<tr>
<td>Post name here</td>
<td><a href="/posts/1/edit">Edit</a></td>
<td><a href="/posts/1" data-method="delete">Delete</a></td>
</tr>
Thanks
You could make helper methods for the buttons and do it like this:
posts_helper.rb
def edit_button(post)
link_to "edit post", edit_post_path(post)
end
def delete_button(post)
# delete button code
end
_index.html.erb
<%= render_cached 'post',
collection: @posts,
replace: {
edit_button: -> (post) { edit_button(post) },
delete_button: -> (post) { delete_button(post) }
}
%>
_post.html.erb
<tr>
<td>Post name here</td>
<td><%= cache_replace_key :edit_button %></td>
<td><%= cache_replace_key :delete_button %></td>
</tr>
Thanks @teeparham but is it not possible to mix keys and partials when replacing?
On Mon, Jan 19, 2015 4:37 PM GMT Tee Parham wrote:
You could make helper methods for the buttons and do it like this:
posts_helper.rb
def edit_button(post) link_to "edit post", edit_post_path(post) end def delete_button(post) # delete button code end_index.html.erb
<%= render_cached 'post', collection: @posts, replace: { edit_button: -> (post) { edit_button(post) }, delete_button: -> (post) { delete_button(post) } } %>_post.html.erb
<tr> <td>Post name here</td> <td><%= cache_replace_key :edit_button %></td> <td><%= cache_replace_key :delete_button %></td> </tr>
Reply to this email directly or view it on GitHub:
You cannot currently pass a partial as the replace value for each item in a collection. I'm not exactly sure how that would work. You could try something like:
<%= render_cached 'post',
collection: @posts,
replace: {
something: -> (post) { render partial: 'dynamic', post: post }
}
%>
Yep, that almost worked, but needed to be:
<%= render_cached 'post',
collection: @posts,
replace: {
something: -> (post) { render partial: 'dynamic', locals: { post: post } }
}
%>
Thanks @teeparham
@teeparham it might also be worth pointing out in the Use section of the gem's readme that you need to include loading cache_rocket from the lib directory because I don't think autoloading lib files is done by default.
I did this by adding an initializer file called cache_rocket.rb that just contained:
require 'cache_rocket'