Rails ruby ​​iterating through partial in a directory

To those who help .. Thank you. Still no solution, but getting closer. The errors that I think are related to the fact that my "file" is rated as "app / views / main / show / _partial.html.erb". and the render adds its own relative path. Therefore, I think I need to specify the file names .. maybe the dir.foreach or dir.glob command. I will continue to dig. THANKS!

I have a directory with full particles that I would like to call on the page. So I wrote a loop, but it gives errors.

<% Dir["app/views/main/show/*"].each do |file| %> <%= render #{file} %> <% end %> 

When I replace the visualization string with a simple

file

it lists file names, so I know that the loop and Dir work. The problem I THINK is that the render is looking for a string. So I tried all kinds of things from google search and here it’s like # {file}, creating the variable first, raw, ... etc.

Also I think the render may look in a different directory relative to Dir. I will try something there.

How can I handle this? I am going to switch from Dir to Dir.foreach or to any other strategy that makes sense.

Thanks.

EDIT : here is the solution I implemented (the directory path changed from above):

 <% Dir["app/views/partials/show/*.html.erb"].each do |file| %> <p> <%= render 'partials/show/' + File.basename(file,'.html.erb').slice!(1..-1) %></p> <% end %> 
+4
source share
1 answer

file already a string, so there is no need to run. In addition, you must give the full path. This should work:

 <% Dir["app/views/main/show/*"].each do |file| %> <%= render :file => Rails.root.join(file).to_s %> <% end %> 

#{} only works inside double quotes. For instance. "#{file}" , but in this case it is not necessary. You can just use file .

+8
source

Source: https://habr.com/ru/post/1441581/


All Articles