简体   繁体   English

Twitter Bootstrap Modal无法在Rails中运行

[英]Twitter Bootstrap Modal not working in Rails

I am still fairly new Rails and to twitter bootstrap . 我还是相当新的Railstwitter bootstrap I am working on a modal, but I receive a "NoMethodError - undefined method 'render.'" 我正在处理一个模态,但我收到一个“NoMethodError - undefined方法'渲染。'”

When I remove the ("<%= escape_javascript(render 'project/new_release') %>" code from my_release.js.erb page, it fades but no window appears. 当我从my_release.js.erb页面中删除("<%= escape_javascript(render 'project/new_release') %>"代码时,它会消失,但不会出现任何窗口。

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" %> 还有错误,甚至不允许我的项目从初始索引页面加载,它引用我的views/layouts/application.html.erb第6行,这是我的<%= javascript_include_tag "application" %>

So I am assuming my .js is all out of whack 所以我假设我的.js完全失控了

I have been working off the the example I saw on the following page- https://coderwall.com/p/ej0mhg 我一直在研究我在下面的页面上看到的例子 - 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 File: 宝石文件:

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 项目/ 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 项目/ _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') %>");

EDIT COMMENT So still no Pop-up window....It fades then nothing. 编辑评论所以仍然没有弹出窗口....它没有消失。 Does this have to do with the class="modal hide fade" ? 这与class="modal hide fade"吗?

Just read that article and it says 刚看完那篇文章就说了

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:

Writer mentioned about name of file but forgot to tell you about the path of js file. 作家提到文件的名称,但忘了告诉你js文件的路径。 Your js files path would be views/controller_name/new_release.js.erb and not assets/javascript/new_release.js.erb 你的js文件路径是views / controller_name / new_release.js.erb而不是assets / javascript / new_release.js.erb

If you look at your controller method 如果你看看你的控制器方法

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

For details checkout Working with Javascript in Rails 有关详细信息Working with Javascript in Rails

Change view file name from "_new_release.html.erb" to "new_release.html.erb" . 将视图文件名从“_new_release.html.erb”更改为“new_release.html.erb”。 I dont know why you are using underscore before file name . 我不知道为什么你在文件名之前使用下划线。

if it is partial than use : ("<%= escape_javascript(render (partial:=>'project/new_release')) %>" 如果它是部分而不是使用:(“<%= escape_javascript(render(partial:=>'project / new_release'))%>”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM