小编mar*_*ion的帖子

如何为多个关联进行急切加载?

我正在使用public_activity和我的一个部分我有这个电话:

<%= link_to "#{activity.trackable.user.name} " + "commented on " + "#{activity.trackable.node.name}", node_path(activity.trackable.node.family_tree_id,activity.trackable.node) %>
Run Code Online (Sandbox Code Playgroud)

这是通过这个电话执行的:

      <% @unread_act.each do |friend| %>
        <li><%= render_activity friend %> </li>
      <% end %>
Run Code Online (Sandbox Code Playgroud)

那个实例变量在我ApplicationController这里分配:

  before_filter :handle_nofications

  def handle_nofications
    if current_user.present?
      @unread_act = Notification.where(owner_id: current_user).includes(:trackable => :user).unread_by(current_user)
    end
  end
Run Code Online (Sandbox Code Playgroud)

我正在使用gem bullet,找到N + 1个查询,我得到的反馈是这样的:

N+1 Query detected
  Comment => [:node]
  Add to your finder: :include => [:node]
N+1 Query method call stack
Run Code Online (Sandbox Code Playgroud)

我最初得到了该消息:trackable,并为:user.这两个我现在都渴望通过includes该赋值语句加载.

但是,我不知道如何在热切的加载中达到3级 - 而不仅仅是两级.

鉴于节点被调用activity.trackable.node.name …

ruby-on-rails query-optimization eager-loading ruby-on-rails-4 public-activity

5
推荐指数
1
解决办法
1149
查看次数

当在当前模态上按下按钮时,如何触发另一个模态?

所以我有一个有形式的模态.当在该模态上按下"提交"按钮时,我想要执行另一个模态.我怎么做?

这是我的第一个模式 - views/shared/_upload_video_popup.html.erb:

<div id="overlay">&nbsp;</div>
<div class="popup" id="add-video-step-1">
  <div class="titles clearfix">
      <h2>Upload a Video</h2>
      <p><i>Step 1 of 2 - TEST</i></p>
  </div>
  <div class="content">
    <% if @family_tree %>
      <%= simple_form_for([@family_tree, @video], :remote => true) do |f| %>
        <div class="column">
              <div class="f-row">
                  <%= f.input :title, label: "Title:" %>
              </div>
              <div class="f-row">
                  <%= f.input :description,label: "Description:" %>
              </div>
              <div class="f-row">
                  <%= f.input :circa, as: :datepicker, start_year: Date.today.year - 5, label: "Circa:" %>
              </div>
              <div class="f-row">
                  <label for="family">Family in this video:</label>
                  <%= …
Run Code Online (Sandbox Code Playgroud)

javascript jquery ruby-on-rails twitter-bootstrap ruby-on-rails-4

5
推荐指数
1
解决办法
343
查看次数

如何将参数传递给通过CSS显示的表单部分?

所以我的表单部分加载到我的div id="secondary",在第一页加载时隐藏.

当用户点击带有被调用类的按钮时toggleSidebar,_form.html.erb会显示该按钮.

update当用户没有像这样登录时,我已覆盖部分以显示新表单(即使被按下):

<%= simple_form_for(Post.new, html: {class: 'form-horizontal' }) do |f| %>  
Run Code Online (Sandbox Code Playgroud)

与看起来像这样的常规版本相反,并且包含在if同一部分的语句中:

<% if current_user and current_user.has_any_role? :editor, :admin %>
    <%= simple_form_for(@post, html: {class: 'form-horizontal' }) do |f| %> 
Run Code Online (Sandbox Code Playgroud)

真正的问题是在我看来,当有人去的时候Update,这就是当用户注销时会发生的事情:

 <%= link_to "Update", "#", class: "togglesidebar" %>
Run Code Online (Sandbox Code Playgroud)

这是完美的,它执行CSS并完美地显示空表单部分.

但是,当用户登录时,我希望它parent_id: @post在执行侧边栏切换时发送参数.

这是它在普通new_post_path视图中的外观(即非侧边栏新帖子视图):

<% if current_user %>                                    
     <%= link_to "Update", new_post_path(parent_id: @post) %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

这就是我的PostController#New样子:

  def new
    @post = Post.new(parent_id: params[:parent_id]) …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-4

5
推荐指数
1
解决办法
277
查看次数

如何正确设置我的CanCanCan权限?

我对如何正确配置CanCanCan感到有点困惑.

对于初学者,我是否必须添加load_and_authorize_resource到我想限制访问的每个控制器资源?

这就是我想做的事情:

  • 管理员可以管理和访问所有控制器和操作
  • 编辑可以阅读所有,管理:新闻室,并可以管理所有帖子
  • 会员可以阅读每个帖子并可以创建和更新帖子(不能编辑/删除/其他任何内容),无法访问新闻编辑室.我们的业务规则中的更新和编辑帖子之间的区别在于更新正在创建一个新帖子,该帖子是当前帖子的子帖子.所以这不是编辑.只是一个有祖先协会的新记录.
  • 访客可以阅读每个帖子,但不能创建帖子或访问新闻室.

这就是我的ability.rb样子:

class Ability
  include CanCan::Ability
  def initialize(user)
    user ||= User.new # guest user (not logged in)
    #Admin
   if user.has_role? :admin
        can :manage, :all
        can :manage, :newsroom
   # Editor
    elsif user.has_role? :editor
      can :read, :all
      can :manage, :newsroom
      can :manage, Post
    #Member
    elsif user.has_role? :member
        can :read, :all
        can :create, Post
        can :status, Post
        can :update, Post do |post|
            post.try(:user) == user
        end
    #Guest
    else
        can :read, :all
        can :create, …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails cancan ruby-on-rails-4 cancancan

5
推荐指数
2
解决办法
8371
查看次数

如何为我的introjs中的下一步开一个模态?

所以IntroJS的工作data-introdata-step属性.

所以例如我的徽标看起来像这样:

<h1 id="logo" data-step="1" data-intro="Welcome to MyApp, where you can start creating an album for generations! Let us help you get started.">
Run Code Online (Sandbox Code Playgroud)

但对于第3步,它是在一个元素上,当按下时,下一步应该是在按下步骤3中的元素时出现的模态.

我有这个,因为我Step 4不起作用:

<div class="popup" id="add-images-step-1" data-step="4" data-intro="GETS TO UPLOADING">
Run Code Online (Sandbox Code Playgroud)

现在,当你到达Step 3并按下时next,它会给你一个空白的盒子,它在屏幕外.

如何让它专注于该模式?

javascript jquery modal-dialog popup intro.js

5
推荐指数
1
解决办法
2316
查看次数

在一个ERB块中渲染多个表达式的输出

我有一个看起来像这样的帮手:

if current_user.find_voted_items(vote_scope: :inspired).include?(post)
   link_to vote_inspired_post_path(post, vote_scope: :inspired), method: :post, data: { confirm: 'Are you sure this post Inspires you?' }, class: "btn btn-default" do
    "<i class='fa fa-lightbulb-o'></i> <br />Inspired".html_safe
   end
   link_to vote_happy_post_path(post, vote_scope: :happy), method: :post, data: { confirm: 'Are you sure this post makes you happy?' }, class: "btn btn-success" do
    "<i class='fa fa-smile-o'></i> <br />Happy".html_safe
   end
   link_to vote_disappointed_post_path(post, vote_scope: :disappointed), method: :post, data: { confirm: 'Are you sure this post disappointed you?' }, class: "btn btn-info" do …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails erb helper ruby-on-rails-4

5
推荐指数
1
解决办法
452
查看次数

为什么这个缓存语句没有写出我期望的密钥?

我有_profile.html.erb部分内容:

<% cache [current_user.roles.first, profile, @selected_profile, params[:rating]] do %>
Run Code Online (Sandbox Code Playgroud)

然而,这是我在服务器日志中看到的:

Read fragment views/profiles/26-20161212033839290582/profiles/52-20161213010040474070/profiles/14-20161213015458288839/profiles/34-20161212035644491093/profiles/33-20161212035644237925/profiles/38-20161207092843851446/profiles/35-20161212040016291016/profiles/36-20161212040016565707/profiles/4-20161213021028862933/profiles/39-20161207092843925084/profiles/46-20161207092844067579/profiles/47-20161207223703646028/profiles/37-20161212040016656625/660bdc6ad0b20e4c5329112cf79946f7 (0.1ms)
Run Code Online (Sandbox Code Playgroud)

我什么roles也没看到.

发生的事情是,如果我以具有admin角色的用户身份登录,然后以具有其他角色的用户身份登录,我看到profiles显示的缓存就像我是管理员而不是具有正确视图的其他用户.

可能导致这种情况的原因以及如何解决?

编辑1

如果我将cache语句更改为:

<% cache [current_user, profile, @selected_profile, params[:rating]] do %>
Run Code Online (Sandbox Code Playgroud)

并刷新,这是日志的样子:

Write fragment views/users/2-20161218005548388099/profiles/37-20161212040016656625///bb163edd4a8c7af2db71a04243338e7b (0.1ms)
  Rendered collection of profiles/_profile.html.erb [1 times] (25.4ms)
Write fragment views/profiles/26-20161212033839290582/profiles/52-20161213010040474070/profiles/14-20161213015458288839/profiles/34-20161212035644491093/profiles/33-20161212035644237925/profiles/38-20161207092843851446/profiles/35-20161212040016291016/profiles/36-20161212040016565707/profiles/4-20161213021028862933/profiles/39-20161207092843925084/profiles/46-20161207092844067579/profiles/47-20161207223703646028/profiles/37-20161212040016656625/83ddeaa031bf68e602ce66af2d268317 (0.1ms)
Run Code Online (Sandbox Code Playgroud)

编辑2

当我binding.pry进入时_profile.html.erb,我得到以下内容:

[3] pry(#<#<Class:0x007f968480ec98>>)> current_user.roles.first
=> #<Role:0x007f969e4422a8 id: 1, name: "admin", resource_id: nil, resource_type: nil, created_at: Mon, 12 Sep 2016 00:38:47 UTC +00:00, …
Run Code Online (Sandbox Code Playgroud)

caching ruby-on-rails russian-doll-caching ruby-on-rails-5

5
推荐指数
1
解决办法
134
查看次数

如何在不使用多步骤表单的情况下迭代一组对象,一次一个地在视图中呈现?

我有一组对象.我想要做的是迭代整个集合,但是自己在页面/视图上显示每个对象,并允许用户单独与每个对象进行交互.理想情况下,我宁愿不使用多部分表格,如果我可以避免它,原因我在问题的最后说明.

我正在尝试实现如下图所示的屏幕.

屏幕1 屏幕2

基本上,应用程序的用户将前往某个位置对该位置的库存(每种产品)进行对帐.这就是屏​​幕显示的内容.对于每种产品,他们都必须更新库存.

摘要和要求如下:

  • location has_many inventory_items.
  • 每当他们想要进行库存检查时,A就会user开始reconciliation.
  • A reconciliation habtm inventory_items&& belongs_to :location.
  • 一个inventory_item habtm reconciliations&& belongs_to :location.
  • 我无法提前预测inventory_items有多少.
  • 可能有几十个或几百个inventory_items.
  • inventory_items如果数字变得笨拙,我可以分成不同的组......类似于分页方法.

所以我的模型看起来像这样:

和解

# == Schema Information
#
# Table name: reconciliations
#
#  id          :integer          not null, primary key
#  location_id :integer
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#

class Reconciliation < ApplicationRecord
  belongs_to :location
  has_and_belongs_to_many :inventory_items
end …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-5 ruby-on-rails-5.1

5
推荐指数
1
解决办法
259
查看次数

为什么number_to_currency中的语言环境设置不起作用?

根据Rails 3.2 API Docs的要求,要使用不同的语言环境number_to_currency,我需要执行以下操作:

<%= number_to_currency(1234567890.506, :locale => :fr) %>
Run Code Online (Sandbox Code Playgroud)

我期待以下输出:

# => 1 234 567 890,51 €
Run Code Online (Sandbox Code Playgroud)

即使我确实在我的应用程序中使用了确切的内容,它仍会输出以下内容:

$1,234,567,890.51
Run Code Online (Sandbox Code Playgroud)

当我available_locales在应用程序中检查时,会得到以下信息:

> I18n.available_locales
=> [:en, :de, :es, :fr, :ja, :pl, :"pt-BR", :ru, :sv, :"zh-CN"]
Run Code Online (Sandbox Code Playgroud)

因此,它应该工作,但是没有。

我想念什么?

更新1

根据@ s3tjan的评论,我在该链接的Rails问题中进行了一些挖掘,并将我引向application.rb发现的地方I18n.enforce_available_locales = false。我将其更改为true并重新启动了服务器。

当我再次尝试上述操作时,现在出现此错误:

ActionView::Template::Error (:fr is not a valid locale):
Run Code Online (Sandbox Code Playgroud)

不确定如何解决此问题。

更新2

所以我才意识到我的从来没有本地文件config/locales。我真正想要的是使用GBP英镑作为货币,因此我在中添加了一个en-GB.yml文件config/locales,然后重新启动了服务器和控制台。

在我的中application.rb,我具有以下内容:

I18n.enforce_available_locales = true
Run Code Online (Sandbox Code Playgroud)

然后,我检查了控制台并得到以下信息:

[1] pry(main)> I18n.available_locales
=> [:en, …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails internationalization ruby-on-rails-3.2

5
推荐指数
1
解决办法
191
查看次数

Rspec让变量产生奇怪的结果

我对RSpec有一个奇怪的问题,我不太了解。

这是我的port_stock_spec.rb文件:

# == Schema Information
#
# Table name: port_stocks
#
#  id                :bigint(8)        not null, primary key
#  portfolio_id      :integer
#  stock_id          :integer
#  volume            :integer
#  transaction_price :float
#  current_price     :float
#  percent_change    :float
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#  current_value     :float
#  dollar_change     :float
#  total_spend       :float
#  transaction_date  :datetime
#  action            :integer
#  position          :integer          default("open")
#  ticker            :string
#  slug              :string
#

require 'rails_helper'

RSpec.describe PortStock, type: …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails

5
推荐指数
1
解决办法
145
查看次数