Neil Neil - 6 months ago 10
Ruby Question

Organizing .html.erb and .js.erb files within a view directory

I have a view directory called

blogs
. Within that directory you can find all the standard, conventional html view templates:

# html files within the app/views/blogs directory
_form.html.erb
edit.html.erb
index.html.erb
new.html.erb
show.html.erb


However: I also have some ajax views that dynamically change content on pages (mostly just content on the form). currently they are all all mixed in with the
.html.erb
files which feels kind of cluttered and unorganized:

# now showing the html AND js files within the app/views/blogs directory
_form.html.erb
add_foobar.js.erb
add_barbazz.js.erb
edit.html.erb
filter_by_team.js.erb
filter_by_city.js.erb
filter_by_state.js.erb
index.html.erb
new.html.erb
show.html.erb


I think it would make sense to seperate the
.html.erb
files from the
js.erb
files, though I am not sure how to do this because they all belong within the
blogs
directory.

How can I organize the
.js.erb
and
.html.erb
files within this directory?

Answer

You could say to your action where exactly template is

respond_to do |format|
  format.js { render "blogs/js/filter_by_team" }
end

And place js template into views/blogs/js

Comments