Tag: ruby on rails

Typeahead填充活动记录对象的所有属性

我正在使用rails 4,我正试图让它与最新的typeahead一起工作。 下拉列表显示匹配的活动记录对象的所有属性,而不仅仅是我想要的名称。 item.rb def self.search(search) if search where([‘lower(name) LIKE ?’, “%#{search}%”]) else Item.all end end items_controller.rb def index @items= Item.search(params[:query]) end def typeahead render json: Item.where(‘name ilike ?’, “%#{params[:query]}%”) end _header.html.erb . . . $(document).ready(function(){ var bloodhound = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace(‘name’), queryTokenizer: Bloodhound.tokenizers.whitespace, limit: 10, remote: {url: ‘/typeahead/%QUERY’, wildcard: ‘%QUERY’} }); bloodhound.initialize(); $(‘#typeahead’).typeahead(null, […]

为什么这个方法不能渲染我的.js.erb文件?

我的video控制器中有这种方法: def notification_go current_user.render_read respond_to do |format| format.html format.js { render ‘notification_go.js.erb’ } end end 它没有在我的videos视图目录中呈现我的notification_go.js.erb文件。 为什么是这样? 我该如何呈现它? (我的notification_go方法是通过AJAX请求调用的。不确定这是否有所不同。)

Gmaps4rails:动态加载时地图未显示

当我通过Jquery调用( $(‘#id’).load(/xyz) )使用Gmaps4rails显示地图时,不会显示地图(而是显示空白区域)。 此页面: https : map.reloadMap ?hl= map.reloadMap建议在页面加载后调用map.reloadMap ,但我不知道应该调用的元素的id。 gmaps4rails文档仅提到包含地图的div的id,并且阅读源代码没有帮助。 怎么解决这个问题?

Capybara测试失败,AJAX响应

我有以下的Capybara测试,它应该点击并更改评论的内容。 问题是内容表单被加载到一个模式中,该模式在单击编辑按钮时弹出,并且我的模态未在测试中呈现。 (此function适用于应用程序)。 save_and_open_page打开一个只包含json对象的页面。 feature_spec.rb require ‘spec_helper’ describe ‘Edit comment’ do let(:commented_post) { FactoryGirl.create(:post_with_comments) } describe “when current_user is the comment’s author” do it ‘should edit the comment content’ do visit post_path(commented_post) within (“#comment-#{commented_post.comments.first.id}”) do click_on “edit” end Capybara.default_wait_time = 15 save_and_open_page fill_in ‘comment_content’, with: ‘No, this is the best comment’ click_on ‘Edit Comment’ expect(page).to […]

Ruby on Rails 3上的jQuery Validator插件?

目前我想知道为什么jqueryvalidation器不能在ruby on rails上使用以下代码? 我注意到它适用于text_field标签但不适用于f.text_field有没有人知道如何使它与f.text_field一起使用? 谢谢 文档就绪function $(“#user_signup_form”).validate({ rules: { user_username:{ required: true, minlength: 3 }, user_email:{ required: true, email: true }, user_password:{ required: true, minlength: 7 }, user_password_confirmation:{ required: true, minlength: 7, equalTo: “#user_password” } }, messages: { user_username:{ required: “Required”, minlength: “Min Length is 3 characters” }, user_email:{ required: “Required”, email: “A valid email […]

Rails 4:使用AJAX仅更新一个参数的自定义操作

在我的Rails 4应用程序中,我有一个Calendar和Post模型,使用shallow路线: resources :calendars do resources :posts, shallow: true end 日历has_manypost和postbelong_to日历。 我已经在show.html.erb视图中的post show.html.erb操作上使用AJAX调用来更新post的自定义:approval参数: 我有respond_to do |format| format.js end respond_to do |format| format.js end在我的Posts#Update respond_to do |format| format.js end控制器中结束 我在app/views/posts/有一个update.js.erb视图,用于重新加载后期show.html.erb视图的相应部分 当然,我的链接设置为remote: true 现在,我需要实现一个类似的function,来更新post的自定义:approval参数,但是从日历 show.html.erb视图中显示所有post。 这是我目前在我的日历 show.html.erb视图中的内容: post.id, “post[approval]” => “ok”), remote: true, :method => :patch do %> post.id, “post[approval]” => “edit”), remote: true, :method => :patch […]

RAILS自动刷新部分

我有一个带有动作节目的控制器事件: def show @event = Event.get(params[:id]) if !connected? || !@event return redirect_to request.referrer || root_path end @requests = @event.get_requests end 在视图show.html.erb我有 $(document).ready( function() { setInterval(function() { $(‘#requests’).load(‘/events/reload_requests’); }, 2000); }); … … javascript函数调用控制器方法events / reload_requests : def reload_requests @requests = @event.get_requests render partial: ‘events/requests’ end 部分_requests.html.erb的内容: … 问题:javascript函数在div #requests中加载我的show.html.erb页面的所有内容,但在div中的show.html.erb副本中,它正确刷新div #request: 在此输入图像描述你可以看到,在show#requests中我有show的内容。 比你

使用基于前一个字段选择的数据填充rails表单

我有一个带有两个集合选择字段的表单。 我想根据第一个字段中选择的内容填充第二个字段中的数据。 #clientform = form_for(@booking, html: { class: “form-horizontal”, role: “form” }) do |f| – if @booking.errors.any? .alert.alert-danger.alert-dismissable role=”alert” button.close type=”button” data-dismiss=”alert” span aria-hidden=”true” | × span.sr-only | Close h4= “#{pluralize(@booking.errors.count,”error”)} prohibited this booking from being saved:” ul – @booking.errors.full_messages.each do |msg| li= msg #selectcourse .form-group = f.label :course = f.collection_select :course_id, Course.order(:title),:id,:title, {prompt: “Choose […]

:在Nokogiri有CSS伪类

我正在寻找伪类:has在Nokogiri有 。 它应该像jQuery的选择器一样工作。 例如: ex1sdfsdf ex2 ex3 CSS选择器应该只返回第一个链接,一个带有非空的span.string兄弟。 在jQuery中,这个选择器运行良好: $(‘li:has(span.string:not(:empty))>h1>a’) 但不是在Nokogiri: Nokogiri::HTML(html_source).css(‘li:has(span.string:not(:empty))>h1>a’) :not and :empty运行良好,但不是:has 。 在Nokogiri有CSS选择器的文档吗? 也许有人可以写一个自定义:has伪类吗? 以下是如何编写:regexp选择器的示例 。 我可以选择使用XPath。 如何为li:has(span.string:not(:empty))>h1>a编写XPath li:has(span.string:not(:empty))>h1>a ?

重构link_to_function的最佳UJS方式是什么?

我有一个针对我的棒球选秀爱好项目的以下界面,让球队老板建立一套标准来对球员进行排名: 单击属性会将该属性添加到列表中,为其赋予重要性因子和排序方向。 在Rails 2和Rails 3(带有原型助手)中 – 我通过以下方式做到了这一点: “add_ranking_attribute_link(attributename)”的位置是: def add_ranking_attribute_link(attributename) link_to_function attributename do |page| page.insert_html :bottom, :rankingattributes, :partial => ‘ranking_attribute’, :locals => {:attributename => attributename } end end 我将它转换为jquery,但我不确定最好的方法 – 主要是因为涉及的循环。 我可以做这个: ‘attributeappend’, :onclick => “$(‘#rankingattributes’).append(‘#{escape_javascript(render(:partial => ‘ranking_attribute’,:locals => {:attributename => attributename}))}’)”) %> 这基本上只是替换link_to_function。 但有更好的方法吗? ujs的方式会让我这样做: $(“.attributeappend).click(function() { $(‘#rankingattributes’).append(‘#{escape_javascript(render(:partial => ‘ranking_attribute’,:locals => {:attributename => attributename}))}’) […]