Tag: ruby on rails 5

无法使用“关注”按钮与Acts_As_Follower一起使用

我正在使用我正在处理的心愿单应用中的Acts_as_follower。 我认为它应该工作,但实际上跟随另一个用户的按钮不是。 跟随按钮位于我的用户索引视图中,我可以在那里看到它,但它没有响应被点击。 任何帮助,将不胜感激。 这是我的代码: index.html.erb followers_controller.rb class FollowersController < ApplicationController before_action :authenticate_user! respond_to :js def create @user = User.find(params[:user_id]) current_user.follow(@user) end def destroy @user = User.find(params[:user_id]) current_user.stop_following(@user) end end users_controller.rb class UsersController < ApplicationController def show @user = User.find(params[:id]) @gifts = @user.gifts end def index @users = User.all if params[:search] @users = User.search(params[:search]).order("created_at DESC") […]

Rails 5.1 group_by年和月,并显示两者

我正在为我的费用开发一个rails应用程序。 我有3个模型,如:用户,支出和货币。 我可以通过表单创建支出,我有标题,描述,金额,currency_id,user_id。 所有的关系都是有效的。 我使用来自用户控制器的show.html.erb来显示current_user的所有费用。 这很好。 然后我将费用分组在一起并按日期排序。 我实际上做了两组,第一组是今天和其他组。 因此,在显示屏上显示我今天创建的费用列表(第一组),然后是月份列表,其中包含前一个日期(第二组)的费用。 现在它显示了这个: Today Expense 1… Expense 2… January … … … December … … 等等… 我想在月份旁边添加年份,以便它显示如下: Today … … January 2018 … … December 2017 … … 除此之外,我希望将这个月和年份作为一个链接,这样当您点击它时,您将转到一个新页面,仅显示该特定月份的所有费用。 多数民众赞成。 是的,我也使用will_paginate。 现在我做的就是这个,让我们从我的UsersController开始: class UsersController params[:page], :per_page => 10) #Retrives all messages and divides into two groups todays messages […]

更新Rails中的每个Array-Object值

基本上我想更新Rails 5中的Model的每个表列。 str = “abc—def” str.split(‘—‘).map do |a| Foo.where(product_id:1).update_all(bar: a) end 旧对象就像: [ [0] { product_id: 1, …, bar: “xxx”, … }, [1] { product_id: 1, …, bar: “xxx”, … } ] 新应该是这样的: [ [0] { product_id: 1, …, bar: “abc”, … }, [1] { product_id: 1, …, bar: “def”, … } ] 但我得到的是bar: “def”每个人都bar: […]

如何使用Ruby on Rails 5将数据从URL拉入表单

我有一个表单,用户可以填写分享设计工作。 但是字段非常类似于dribbble.com。 我看到一些网站允许用户粘贴自动填写表单的链接。 可以在https://www.uplabs.com/submit上查看示例 我不需要如何做到这一点的确切代码。 你能指点一下方向吗? 一些插件,一些例子,……? 例; 填写表格(图片,标题,设计师ID,描述等)来自链接,如https://dribbble.com/shots/1902343-Subscribe-to-our-newsletters 谢谢!

将rails_admin与rails_api一起使用

我最初在rails_api GitHub上发布了这个问题 ,但由于不活动,我现在将它发布在这里。 我正在尝试将rails_admin与Rails 5 API应用程序一起使用。 我添加了额外的ActionController模块,以至于我可以使用有效的rails_admin面板或工作API请求。 问题似乎是rails_admin依赖于ActionView::Layouts ,后面包含导致API请求的问题。 的Gemfile: gem ‘rails’, ‘>= 5.0.0.beta3’, ‘< 5.1' … gem 'rack-pjax', github: 'afcapel/rack-pjax' gem 'remotipart', github: 'mshibuya/remotipart' gem 'kaminari', github: 'amatsuda/kaminari', branch: '0-17-stable' gem 'rails_admin', github: 'sferik/rails_admin' 我将我的应用程序配置为使用ActionDispatch::Flash : module MyApp class Application < Rails::Application … config.middleware.use ActionDispatch::Flash end end 我为Rails API ,ApplicationController 配置了额外的模块 : class ApplicationController […]

传递的参数未在Rails中读取

我之前发布了这个问题: 在Rails中点击按钮时传递参数 答案非常有效。 但是,我传递的参数无法在erb中调用,即使是从book_now方法传递的book_now也是book_now 。 它在我的服务器日志中显示为Parameters: {“event”=>”4”, “event_option_id”=>”5”}但是当我尝试使用它返回空白,当我刚刚测试它为to_i ,它显示0而不是5 。 我的EventsController有这个方法: def book_now @event_option_id = params[:event_option_id] end 我的链接传递这样的参数: @event.id, :event_option_id => e.id), :id => “book-now-button”, :class => “button” %> 我试图传递这个@event_option_id以便我可以从我的数据库中访问相应的值。 每个EventOption都有一个独特的price , description和name ,当我尝试以多种方式访问​​它们时,我得到(NoMethodError: undefined method ‘price’ for nil:NilClass) 。 知道发生了什么事吗?

有效地迭代和改变关联

我们的代码有效地做到了这一点。 obj.things.each |thing| … do some stuff … obj.things.destroy(thing) … do some more stuff… end 我们发现,在更改CollectionProxy的同时迭代CollectionProxy会导致只有一半的项被迭代。 目前我们正在通过将代理扁平化为数组来解决这个问题。 但这意味着将所有things复制到内存中。 obj.things.to_a.each |thing| … end 有没有办法迭代和变异集合而不将整个关联拉入内存? 或者,是否有比我们使用的模式更好的模式? 例如,包装代码是我们每次销毁关联时都不想做的事情,因此我们不使用关联钩子。 我们可以写一个可以使用钩子的子类或范围吗? 更新 : 我发布了更大的问题 。

Rails :: ActiveRecord在深拷贝副本中保留auto_increment id

我需要创建一个包含要操作的模型对象的临时数组。 我需要auto_increment主键,但我不希望保持关联,因为当我从数组中删除它时,它也将它从数据库中删除(感谢rails,非常不方便)。 我使用object.dup创建的深层副本的id标记为nil。 如何将它们保存在副本中?

使用计算/操作的用户名参数设计登录

我正在尝试使用带有我已计算的用户名的设计创建登录。 作为一个例子,我们假设我们想要命名我们的用户名 – 我希望从登录表单接收用户名,并使用namespaced_username进行实际身份validation。 所以在我的User::SessionsController#create ,我可能有: def create params[:user][:namespaced_username] = “namespace/#{params[:user][:mobile_number]}” super end 尽管设计正在侦听namespaced_username(在初始化程序或模型本身中配置了authentication_keys),并且用户设置为namespace/username ,但我仍然会被告知Invalid Namespaced username or password is not valid 我如何获得设计(特别是看守策略)来阅读新的参数?

Rails:虽然由球童正确使用,但在生产模式中无法找到资产

我几乎几天都试图解决这个问题。 我试图在生产模式下在rails网站上运行我的ruby,但是(某些)资产无法提供。 图像位于自定义子文件夹(app / assets / audio / testfolder / demo.png)中,该子文件夹已添加到Rails.application.config.assets.paths并正确预编译但我总是收到错误 ActionView::Template::Error (The asset “demo.png” is not present in the asset pipeline.) 或者更具体: I, [2017-09-25T00:38:32.859541 #41037] INFO — : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Started GET “/” for 127.0.0.1 at 2017-09-25 00:38:32 +0200 I, [2017-09-25T00:38:32.860377 #41037] INFO — : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Processing by TestControllerController#index as HTML I, [2017-09-25T00:38:32.861240 #41037] INFO […]