最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I have a JavaScript partial in Ruby on Rails? Partial is trying to render in HTML - Stack Overflow

programmeradmin1浏览0评论

I have a function that simple selects all of the checkboxes on the current page, creates a form, and submits it automatically.

Instead of repeating the same exact JavaScript code on every single view, I want to try to just have a shared partial.

Here's all I have thus far:

#views/shared/_delete_multiple_items.js.erb
alert("<%= random_variable %>");

.

#views/users/index.html.erb
...
<script>
  function deleteMulitpleUsers() {
    <%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World" %>
  }
</script>

It looks like Rails is trying to render the partial in HTML though, if I understand this error correctly:

Missing partial shared/_delete_multiple_items with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:

I've tried to include :formats => :js with render, like this:

<%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World", :formats => :js %>

but I still get the same exact error.

I have a function that simple selects all of the checkboxes on the current page, creates a form, and submits it automatically.

Instead of repeating the same exact JavaScript code on every single view, I want to try to just have a shared partial.

Here's all I have thus far:

#views/shared/_delete_multiple_items.js.erb
alert("<%= random_variable %>");

.

#views/users/index.html.erb
...
<script>
  function deleteMulitpleUsers() {
    <%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World" %>
  }
</script>

It looks like Rails is trying to render the partial in HTML though, if I understand this error correctly:

Missing partial shared/_delete_multiple_items with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:

I've tried to include :formats => :js with render, like this:

<%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World", :formats => :js %>

but I still get the same exact error.

Share Improve this question edited Aug 10, 2022 at 23:14 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Sep 2, 2018 at 21:49 LewlSauceLewlSauce 5,89213 gold badges62 silver badges120 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Change this:

<%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World" %>

to this:

<%= render partial: 'shared/delete_multiple_items', formats: :js, :locals => {:random_variable => "Hello World"} %>

partial: at the beginning to tell rails to look for a partial and formats: :js to tell rails to look for a js file (note that formats is NOT a child of locals)

You get that error because server responding format is html, not javascript. Setting response format is done in controller so putting format: :js in templates won't work. Try these steps and see if they work for you.

  1. Rename _delete_multiple_items.js.erb to _delete_multiple_items.html.erb

  2. Put alert("<%= random_variable %>"); inside <script> tag

  3. In index.html.erb, remove all js related code and just do <%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World" %>

Have you tried adding a j on your js.erb file?

#views/shared/_delete_multiple_items.js.erb
alert("<%= j random_variable %>");
发布评论

评论列表(0)

  1. 暂无评论