Twitter Bootstrap Modal在Rails中不起作用 [英] Twitter Bootstrap Modal not working in Rails
问题描述
我对Rails
和twitter bootstrap
还是很陌生.我正在研究模态,但收到"NoMethodError-未定义的方法'render.'".
I am still fairly new Rails
and to twitter bootstrap
. I am working on a modal, but I receive a "NoMethodError - undefined method 'render.'"
当我从my_release.js.erb
页中删除("<%= escape_javascript(render 'project/new_release') %>"
代码时,它消失了,但没有出现窗口.
When I remove the ("<%= escape_javascript(render 'project/new_release') %>"
code from my_release.js.erb
page, it fades but no window appears.
还有错误,它甚至不允许我的项目从初始索引页加载,它引用了我的views/layouts/application.html.erb
第6行,即我的<%= javascript_include_tag "application" %>
Also with the error, which won't even allow my project to load from the initial index page, it references my views/layouts/application.html.erb
line #6 which is my <%= javascript_include_tag "application" %>
所以我假设我的.js完全不合时宜
So I am assuming my .js is all out of whack
我一直在处理下一页中看到的示例- https://coderwall.com/p /ej0mhg
I have been working off the the example I saw on the following page- https://coderwall.com/p/ej0mhg
我正在尝试将此模式示例添加到我现有的项目中.
I am trying to add this modal example to an existing project of mine.
以下是我的代码片段供参考:
Here are some snippets of my code for reference:
宝石文件:
gem 'rails', '3.2.13'
gem 'bootstrap-sass', '2.3.2.1'
....
....
group :assets do
gem 'sass-rails', '~> 3.2.3'
end
....
....
Application.js
Application.js
....
//= require bootstrap-modal
//= require bootstrap
....
Routes.rb
Routes.rb
get "project/new_release" => 'project#new_release', :as => :new_release
controller.rb
controller.rb
def new_release
respond_to do |format|
format.html
format.js
end
end
project/index.html.erb
project/index.html.erb
<div id="modal-window" class="modal hide fade" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true"></div>
<%= link_to 'Add release', new_release_path, {:remote => true,
'data-toggle'=> "modal", 'data-target' => '#modal-window'} %>
project/_new_release.html.erb
project/_new_release.html.erb
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
**here comes whatever you want to show!**
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
new_release.js.erb
new_release.js.erb
$("#modal-window").html("<%= escape_javascript(render 'project/new_release') %>");
编辑评论
因此仍然没有弹出窗口...它消失了,然后什么也没有.这和class="modal hide fade"
有关吗?
EDIT COMMENT
So still no Pop-up window....It fades then nothing. Does this have to do with the class="modal hide fade"
?
推荐答案
只需阅读该文章并说
Now we need some javascript to make this works, make a file named same way as the controller action but with the suffix .js.erb for this example should be:
作者提到了文件名,但忘了告诉您js文件的路径.您的js文件路径为 views/controller_name/new_release.js.erb ,而不是asset/javascript/new_release.js.erb
Writer mentioned about name of file but forgot to tell you about the path of js file. Your js files path would be views/controller_name/new_release.js.erb and not assets/javascript/new_release.js.erb
如果您查看控制器方法
def new_release
respond_to do |format|
format.html #If it's a html request this line tell rails to look for new_release.html.erb in your views directory
format.js #If it's a js request this line tell rails to look for new_release.js.erb in your views directory
end
end
有关详细信息,请参见 Working with Javascript in Rails
For details checkout Working with Javascript in Rails
这篇关于Twitter Bootstrap Modal在Rails中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!