Tag: ruby on rails

如何计算用户对我的特定项目(Rails)所做的所有喜欢

我为新手问题道歉,我对Rails和SQL相对较新。 我正在尝试计算用户对我的特定项目所做的所有喜欢。 在SO社区和Treehouse Coding的帮助下,我能够显示哪些用户喜欢我的特定项目。 此外,我能够研究如何在我的控制器中使用.join显示我收到的所有喜欢(来自Lisa,James,Harry等)。 现在我想显示每个特定用户的计数。 所以,例如,如果詹姆斯喜欢我的4件物品,我想在詹姆斯旁边展示4件。 我在下面列出了所有相关代码,非常感谢你们! Index.html.erb My Fans – Items_controller def index @items = Item.order(“created_at DESC”) if current_user.present? @likers = current_user.items.map(&:likes).flatten.map(&:user).flatten @likersnumero = current_user.items.joins(:likes).map(&:user).count end end item.rb的 class Item true end users.rb的 class User < ApplicationRecord has_many :likes def likes?(post) post.likes.where(user_id: id).any? end #Tried using def total_likes in my index.html.erb using but […]

如何对JavaScript响应执行function测试?

我注意到在Rails中渲染js请求的文本是很常见的,其中嵌入了jquery方法调用中的文本以将其插入到DOM中。 // javascript code $.getScript(“/some_url”); // rails partial code, to make things more clear I have added some simple html $(“#some_id”).text(unescape_javascript(‘bar’)) 我的问题是你如何在这样的响应文本的function测试中执行assert_select或等效的? // class FooBarControllerTest < … test "javascript response" do xhr :get, :new // how could I test the div here assert_select "#foo", "bar" // this doesn't work end end **更新了代码,使其更加清晰

Application.js.coffee rails

我试图从jQuery做一个自动完成,但是我的代码是在CoffeeScript中。 我不确定如何在我的localhost服务器上运行它。 起初我将它放在application.js中,但这不起作用,所以我创建了gens.js.coffee并将代码放在那里,但仍然没有显示。 这是代码: jQuery -> $(‘#querysearch’).autocomplete source: [‘foo’, ‘food’, ‘four’] 这里application.js正在调用文件: //= require gens //= require jQuery.ui.datepicker # file his actually gens.js.coffee 这就是我调用application.js的方式: 我做错了什么?

遍历数组以创建rails对象

我无法找到有关如何遍历数组和创建对象的任何信息。 我的表单创建了一个可选择的用户列表,在选中时,将user_ids作为数组对象传递。 邀请\ new.html.rb @event_selected.id %> 我想通过将attend_event_id与attendee_id数组中的所有对象相结合来创建新的Invitations对象。 经过一些麻烦后,我得到了控制器工作的基础知识,但只是将user_id作为文本条目传递。 以下是我的邀请控制器。 不知道从哪里开始,因为我还没有找到一个好的例子。 invitations_controller.rb def create @invitation = Invitation.new(invite_params) if @invitation.save! flash.now[:success] = “Invited!” redirect_to root_path else flash.now[:error] = “Failure!” redirect_to root_path end end private def invite_params params.require(:invitation).permit(:attended_event_id, :attendee_id) end end

ruby on rails association newbie

这可能被视为rails 3关联错误的重新发布。 但它是我无法理解的东西。 但是好的……继续这个交易……我在这里问了一个与表模式有关的问题。 post是这个表模式结构中的错误 ,查询是: `CREATE TABLE “pages” ( “id” INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, “title” varchar(255), “body” varchar(255), “author” varchar(255), “email” varchar(255), “reference” varchar(255), “created_at” datetime, “updated_at” datetime);` 我被建议我应该为作者创建单独的表,这就是我尝试和搞砸了。 任何人都可以告诉我协会应该如何与我的方案一起工作? 我真的需要很好地理解ROR协会。 编辑-1 这是我的页面迁移代码: class CreatePages true, :limit => 50 t.string :body, :limit =>255 t.string :email, :unique => true, :limit => 50 t.string […]

RoR使用设计 – 加密密码

赦免我的无知,但我是RoR的新手。 我的问题是我正在尝试使我的Devise gem工作,但是当我填写信息并单击Sign-Up时,我得到了回报:“undefined method` encrypted_pa​​ssword =’for”。 我已经尝试过rake db:migrate并清除User.rb模型中的属性,但它仍然不起作用。 请任何指导,不胜感激!

使用Rails 5,我怎样才能使FriendlyId附加一个 – “count + 1”来复制slus而不是UUID?

显然,FriendlyId已经改变了以前默认的方法,即附加一个数字序列来复制slug(这就是我想要的)现在使用UUID: Previous versions of FriendlyId appended a numeric sequence to make slugs unique, but this was removed to simplify using FriendlyId in concurrent code. 此function不是我现在感兴趣的东西,而是更喜欢使用原始方法来生成更清晰的URL。 我发现了一个类似的问题,其中有人提供了以下代码来覆盖friendlyId normalize_friendly_id方法以获得我之后的function,但使用它会导致错误( wrong number of arguments (given 1, expected 0) ): def normalize_friendly_id count = self.count “name = #{name}” super + “-” + count if name > 0 end 我试图将其“转换”成一个友好的“候选人”,但我真的不知道我在做什么,以下不起作用。 […]

Rspec失败,“范围值不好”

我的方法: def swap (order) order = order == ‘asc’ ? ‘desc’ : ‘asc’ end 规格: let(:order) { ‘wrong_order’} it ‘swaps the order’ do expect(swap(order)).to eq(‘asc’) end 这个rspec失败了消息ArgumentError: bad value for range 但如果我通过’desc’或’asc’并改变期望它就可以正常工作。 另外我在irb上尝试了这个只是通过交换(”)它给了我’asc’不知道为什么rspec失败

如何用LDAP进行Ruby on Rails认证?

我正在开发一个Web应用程序,我有一个使用bcrypt gem的身份validation方法它工作正常,但我想将身份validation方法更改为LDAP,因为我正在使用Intranet环境并希望我的用户能够使用Windows登录证书。 我正在寻找使用net-ldap gem但我无法在网上找到关于如何在我的Web应用程序中实现这一点的任何好的总结/解释。 你能帮我解决这个问题吗? 我怎样才能做到这一点?

Rails呈现javascript显示文本

我不确定这种行为是否正常,但是当我这样做时: 渲染js:“window.location.pathname =’#{new_user_session_path}’;” 在我的控制器中,它会短暂显示文本window.location.pathname = ‘/user/sign_in’; 在执行该行并成功重定向之前。 这是完整的控制器动作: def vote_down if user_signed_in? current_user.vote_exclusively_against(@deal = Deal.find(params [:id])) render:partial =>’deals / partials / interaction’, :locals => {:deal => @ deal,:votes_for => @ deal.votes_for,:votes_against => @ deal.votes_against} 其他 渲染js:“window.location.pathname =’#{new_user_session_path}’;” flash [:error] =’请登录进行投票。’ 结束 结束 如何防止这种情况发生并直接呈现该模板?