I need to combine ruby code with javascript, to show something which depends on the server's response.
app/views/posts/index.html.erb
<h1 class="text-center m-4 text-success">Posts</h1>
<div id="posts" data-controller="like" data-like-url-value="<%= like_posts_path %>">
<div class="row">
<% @posts.each do |post| %>
<%= render post %>
<% end %>
</div>
</div>
<script>
if(some condition){
console.log("All Posts has been loaded successfully.")
}
</script>
app/controllers/posts_controller.rb
def index
@posts = Post.all.order(id: :desc)
if @posts.length > 0
session[:post] = true
end
end
I have to use the index action session variable in the index template, I don't have any idea about how to combine ruby code with javascript.
stimulusturbo_stream. stimulus.hotwired.dev/handbook/hello-stimulus