[英]Generate HTML with Rails and Jquery doesn't work
Im trying to make a button to add one form to create one Task with many Users, i put this: 我试图制作一个按钮来添加一个表单以创建一个有许多用户的任务,我这样说:
$("#more").click(function(){
i++;
$("#more").after("<%= select_tag 'user_id_"+i+"',options_for_select(User.all.map { |u| [u.email,u.id] }) %>");
});
Right this appear normal but this genetare the tags Rails, appear this: 正确,这看起来很正常,但是这个标签是Rails,显示如下:
< %= select_tag 'user_id_4',options_for_select(User.all.map { |u| [u.email,u.id] }) %><%= select_tag 'user_id_3',options_for_select(User.all.map { |u| [u.email,u.id] }) %><%= select_tag 'user_id_2',options_for_select(User.all.map { |u| [u.email,u.id] }) %>
How can I make to appear this code in Rails? 如何使此代码出现在Rails中?
UPDATE: 更新:
when i try put the code jquery in my .html.erb this dont execute, before was in application.js : 当我尝试将代码jquery放在我的.html.erb中时,此代码不执行,之前在application.js中:
<script type="text/javascript">
$(document).ready(function() {
var i = 1;
$("#more").click(function(){
i++;
$("#more").after("<%= select_tag 'user_id_"+i+"',options_for_select(User.all.map { |u| [u.email,u.id] }) %>");
});
});
</script>
Let's try something, it's a wild guess but I hope it will answer your need. 让我们尝试一下,这是一个疯狂的猜测,但我希望它能满足您的需求。 You can try this in a new application.
您可以在新的应用程序中尝试。 Just create a user model, and seed some data, and add the gem 'jquery-rails' in your assets group.
只需创建一个用户模型,并播种一些数据,然后在资产组中添加gem'jquery-rails'即可。 Run
bundle install
and restart your server. 运行
bundle install
并重新启动服务器。
In your assets/javascripts/application.js , ensure you have at least the following lines: 确保您的asset / javascripts / application.js中至少包含以下几行:
//= require jquery
//= require jquery_ujs
//= require_tree .
In your users_controller.rb , define the index method to fetch all users from the database and render index template, it's very simple you just have to do like this: 在users_controller.rb中 ,定义index方法以从数据库中获取所有用户并呈现索引模板,这非常简单,您只需要执行以下操作:
def index
@users = User.all
respond_to do |format|
format.html
format.js
end
end
Then edit your view file in views/users and be sure it's named index.html.erb . 然后在视图/用户中编辑视图文件,并确保将其命名为index.html.erb 。 This view will be displayed when you try to access localhost:3000/users with your browser (in details: it's a GET HTML request).
当您尝试使用浏览器访问localhost:3000 / users时,将显示此视图(详细信息:这是一个GET HTML请求)。 Inside your view you'll write HTML and embed ruby, just the same way as you usually do.
在视图内部,您将编写HTML并嵌入红宝石,就像平常一样。 For now simply create a div and your give it the id "more" and you also place a link with attribute
remote
现在,只需创建一个div并将其ID设置为“更多”,然后还放置一个带有
remote
属性的链接
<%= link_to 'click me', users_path, remote: true %>
Then create a new view file in views/users and name it index.js.erb . 然后在views / users中创建一个新的view文件,并将其命名为index.js.erb 。 You notice that the extension changed, now we use js.erb instead of html.erb .
您会注意到扩展名已更改,现在我们使用js.erb而不是html.erb 。 In detail: when your page will do a javascript request (a GET JS in this case) to your users#index route, it will answer with the index.js.erb view.
详细信息:当您的页面向用户#index路由发出javascript请求(在这种情况下为GET JS)时,它将使用index.js.erb视图进行回答。 The big nice stuff here is you can write javascript code in this view that will get executed in your browser.
这里最大的好处是,您可以在此视图中编写javascript代码,该代码将在浏览器中执行。
You remember we added remote: true
to the link? 您还记得我们为链接添加了
remote: true
吗? This tells rails to general a link that will make a JS request. 这告诉Rails发出将要发出JS请求的链接。 Magic.
魔法。
So now you just have to write some javascript code in your views/users/index.js.erb file. 因此,现在您只需要在views / users / index.js.erb文件中编写一些javascript代码即可。 Let's start with something super simple:
让我们从超级简单的事情开始:
alert('Hello');
Go to your page (normally localhost:3000/users) and click the 'click me' link, you should see the javascript alert box displaying. 转到您的页面(通常为localhost:3000 / users),然后单击“单击我”链接,您应该看到显示的javascript警报框。
Let's do a second test, and integrate some ruby code. 让我们进行第二次测试,并集成一些ruby代码。 Change the content of your your views/users/index.js.erb by this :
通过以下方式更改您的views / users / index.js.erb的内容:
alert('Ruby compute: <%= 1+1 %>');
And in your web page, you'll see the alert with the test 'Ruby compute: 2'. 并且在您的网页中,您将看到带有测试“ Rubycompute:2”的警报。 You just have to adapt this technique to your case in your application et voila.
您只需要在应用程序中将这种技术适应您的情况即可。
The same technique (but with better explanation) can be found on this railscast: http://railscasts.com/episodes/136-jquery 在以下railscast上可以找到相同的技术(但有更好的解释): http: //railscasts.com/episodes/136-jquery
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.