Exploring the realm of rails, I am embarking on a quest to achieve the following:
The mission at hand involves crafting a store and unstore button to save and 'un-save' events respectively. My strategy includes leveraging event attributes and current user details to locate the stored event. Through the enchantment of ajax and remote magic in rails, I aim to transform the button's behavior from store to unstore or vice versa.
In my mystical _feed.html.erb.
<% if @feed_items.any? %>
<ul>
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ul>
<% end %>
Each @feed_items treasure trove holds a wealth of data (titles, categories, and more).
Within the enchanted realms of _feed_item.html.erb.
<li id="<%= feed_item.id %>" class="item">
...
<div class="small-3 text-center columns">
<%= render 'shared/store_form', event: feed_item %>
</div>
...
</li>
The magical Event symbol guides the feed_item towards the store_form.
Amidst the mysterious lands of _store_form.html.erb.
<div id="store_form_<%= event.id %>">
<% if event.stored?(current_user) %>
<%= render 'shared/unstore', event: event %>
<% else %>
<%= render 'shared/store', event: event %>
<% end %>
</div>
Once again, a feed_item is summoned into the midst of the partial.
_store.html.erb
<%= form_for(event.storages.build(saver_id: current_user.id,
organizer_id: event.user_id),
remote: true) do |f| %>
<div>
<%= f.hidden_field :organizer_id %>
<%= f.hidden_field :saved_id, value: event.id %>
</div>
<%= f.submit "store" %>
<% end %>
_unstore.html.erb
<%= form_for(event.storages.find_by(saver_id: current_user.id,
organizer_id: event.user_id) ,
html: { method: :delete },
remote: true) do |f| %>
<%= f.submit 'unstore' %>
<% end %>
create.js.erb
$("#store_form").html("<%= escape_javascript(render 'shared/store', event: event )%>")
destroy.js.erb
$("#store_form").html("<%= escape_javascript(render 'shared/unstore', event: event) %>")
Alas, when attempting to store or unstore an event, the dark forces manifest these errors.
ActionView::Template::Error (undefined local variable or method `event' for #<#<Class:0x00000004d69be8>:0x00000004d68d10>):
1: $("#store_form").html("<%= escape_javascript(render 'shared/unstore', event: event) %>")
ActionView::Template::Error (undefined local variable or method `event' for #<#<Class:0x00000004d69be8>:0x00000004fdc6a0>):
1: $("#store_form").html("<%= escape_javascript(render 'shared/store', event: event )%>")
I beseech thee, how can I bestow upon this specific "event" data unto the partial? Utilizing @feed_item within the create and destroy js proves challenging, for it harbors a myriad of events. Might there be a more enlightened approach to rise above this challenge?