1

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.

3
  • You should research stimulus turbo_stream. stimulus.hotwired.dev/handbook/hello-stimulus Commented Oct 28, 2022 at 3:46
  • is it possible in the above scenario or not. Commented Oct 28, 2022 at 4:42
  • We can add contact? i will help you check (my profile has Facebook) Commented Oct 28, 2022 at 4:48

3 Answers 3

2

The condition is set and can be checked on the server and the view is rendered on the server. That means you can simple use ERB to check the condition and then add a <script> block when it is true. The script block will run on the client.

# in the controller
def index
  @posts = Post.all.order(id: :desc)
  @post_loaded = @posts.length > 0
end

# in the view
<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>

<% if @post_loaded %>
<script>
  console.log("All Posts has been loaded successfully.")
</script>
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

0

I have a solution for the above question, Which you have asked.

app/controllers/posts_controller.rb

def index
   @posts = Post.all.order(id: :desc)
   if @posts.length > 0
     session[:post] = "some value"
   end
end

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>
   let post = "<%= session[:post] %>";
   console.log(post);
</script>

Comments

0

You can also try this; if your application has a lot of code, it will become easier to handle that and improve code quality as well.

app/controllers/posts_controller.rb

def index
   @posts = Post.all.order(id: :desc)
   if @posts.length > 0
     session[:post] = true
   end
end

app/views/posts/index.html.erb

<%= javascript_include_tag "my_javascript" %>
<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>

app/javascript/my_javascript.js

if(some condition){
  console.log("All Posts has been loaded successfully.")
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.