简体   繁体   English

如何在 Rails 7 中将 ruby 代码与 javascript 结合起来

[英]how to combine ruby code with javascript in rails 7

I need to combine ruby code with javascript, to show something which depends on the server's response.我需要将 ruby 代码与 javascript 结合起来,以显示取决于服务器响应的内容。

app/views/posts/index.html.erb应用程序/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应用程序/控制器/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.我必须在索引模板中使用索引操作会话变量,我不知道如何将 ruby 代码与 javascript 结合起来。

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 .这意味着您可以简单地使用 ERB 来检查条件,然后在它为true时添加一个<script>块。 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 %>

I have a solution for the above question, Which you have asked.对于上述问题,我有一个解决方案,您已经问过。

app/controllers/posts_controller.rb应用程序/控制器/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应用程序/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>

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应用程序/控制器/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应用程序/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应用程序/javascript/my_javascript.js

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM