如何使用控制器加载自定义js文件?
How to load a custom js file with a controller?
如果我遵循 user_create_path
,文件 "views/users/create.js.erb" 将是 运行,如果我使用此代码:
users_controller.br
def create
#...
respond_to do |format|
format.html { redirect_to root_path }
format.js
end
end
如何有条件地加载不同的js文件?
我正在考虑以下思路:
def create
#...
if User.all.count < 50
respond_to do |format|
format.html { redirect_to root_path }
format.js { create_a.js.erb }
end
else
respond_to do |format|
format.html { redirect_to root_path }
format.js { create_b.js.erb }
end
end
end
也许你应该 运行 根据条件使用不同的 js?
users_controller.rb
def create
respond_to do |format|
format.html { redirect_to root_path }
format.js { @users_count = User.all.count }
end
end
create.js.erb
<% if @users_count < 50 %>
alert('a');
<% else %>
alert('b');
<% end %>
如果我遵循 user_create_path
,文件 "views/users/create.js.erb" 将是 运行,如果我使用此代码:
users_controller.br
def create
#...
respond_to do |format|
format.html { redirect_to root_path }
format.js
end
end
如何有条件地加载不同的js文件?
我正在考虑以下思路:
def create
#...
if User.all.count < 50
respond_to do |format|
format.html { redirect_to root_path }
format.js { create_a.js.erb }
end
else
respond_to do |format|
format.html { redirect_to root_path }
format.js { create_b.js.erb }
end
end
end
也许你应该 运行 根据条件使用不同的 js?
users_controller.rb
def create
respond_to do |format|
format.html { redirect_to root_path }
format.js { @users_count = User.all.count }
end
end
create.js.erb
<% if @users_count < 50 %>
alert('a');
<% else %>
alert('b');
<% end %>