我正在构建一个博客应用程序.如果"发布"多个"帖子",我希望能够复制"文章"这个词.
像这样:可用文章或可用文章
这就是我拥有的......
Available <%= pluralize @posts.published, "Article" %>:
Run Code Online (Sandbox Code Playgroud)
我试过了
Available <%= pluralize @posts.published.count, "Article" %>:
Run Code Online (Sandbox Code Playgroud)
这工作......但我不想要这个号码.它不应该读取可用的5篇文章....它应该没有数字.
这是我第一次异步处理作业我在我的应用程序中实现Sidekiq进行后台处理.我会将它用于提醒电子邮件和应用内通知.我很困惑我是否应该使用Active Job创建一个发送电子邮件的工作或者Sidekiq Worker来发送电子邮件.他们似乎做同样的事情,Rails 4.2 Active Job似乎很新......它是否取代了对Sidekiq工作者的需求?
以下是使用Active Job作业和Sidekiq Worker发送邮件代码的相同内容.我正在使用Whenever gem进行调度.
my_mailers.rb
class MyMailers < ActionMailer::Base
def some_mailer(r.user_id)
@user = User.find(r.user_id)
mailer_name = "ROUNDUP"
@email = @user.email
@subject ="subject text"
mail(to: @email,
subject: @subject,
template_path: '/notifer_mailers',
template_name: 'hourly_roundup.html',
)
end
end
Run Code Online (Sandbox Code Playgroud)
使用Sidekiq"Worker"
some_worker.rb
class SomeWorker
include Sidekiq::Worker
def perform()
@user = User.all
@reminders = @user.reminders.select(:user_id).uniq.newmade
@reminders.each do |r|
MyMailers.some_mailer(r.user_id).deliver_later
end
end
end
Run Code Online (Sandbox Code Playgroud)
使用活动作业"作业"
some_job.rb
class SomeJob < ActiveJob::Base
queue_as :mailer
def perform()
@user = User.all
@reminders = @user.reminders.select(:user_id).uniq.newmade
@reminders.each do |r| …Run Code Online (Sandbox Code Playgroud) 我正在使用Jquery Server端数据表,并且无法使页面2 +上的分页工作.
我有200条记录.第一页显示50.当我单击页面底部的分页编号时,它导航到第2页,但第2页为空.
我在同一个应用程序(但另一个表)中使用非服务器端数据表,它工作正常.
除了分页之外,选择每页显示的#of记录的选择菜单不起作用,并且列排序不起作用.
搜索工作正常
我正在使用:"1.12.4"Rails 5 DataTables 1.10.16
index.json.jbuilder
json.draw params[:draw]
json.recordsTotal @cooks.length
json.recordsFiltered @cooks.length
json.data do
json.array! @paginated do |cook|
json.DT_RowClass "js-cook-row js-cook-row-#{donor.id}"
if browser.device.mobile?
json.partial! 'cooks/mobile_data', cook: cook, project: @project
else
json.partial! 'cooks/desktop_data', cook: cook, project: @project
end
end
end
Run Code Online (Sandbox Code Playgroud)
带桌子的页面(苗条)
table.table.table-striped.table-bordered.table-hover.js-cook-table.cooks-table.dataTables-example.small-font.kitchen-table id="#{table_id}" data-toggle="table" width="100%"
thead
tr
- if source == RouteSourceConsts::WIZARD
th.updated-th data-field="cook-checkbox"
th.cook-category data-field="given" data-sortable="true" = t('cooks.table_title.given')
th.cook-vendor data-field="cook_name" data-sortable="true" = t('cooks.table_title.name')
th data-field="recipes" = t('cooks.table_title.recipes')
th data-field="helper" data-sortable="true" = t('cooks.table_title.helper')
th.status-info-th data-field="status" …Run Code Online (Sandbox Code Playgroud) 错误:
param is missing or the value is empty: color
Run Code Online (Sandbox Code Playgroud)
我正在制作一个表单,我可以将嵌套属性添加到父记录,我想通过复选框添加它们.我有父模型"汽车"和儿童模型"颜色"......但我想从一些默认的"颜色"开始...所以我也有模型"Sample_Colors",它根据"Car_Models"改变.
我正在尝试使用复选框将多个"颜色"添加到关联的"汽车"...我不希望与"Sample_Colors"和"颜色"的HABTM关系,因为我需要"颜色"记录可编辑而不仅仅是一个连接表.我使用HABTM完成了这个,所以我真的不明白为什么我不能以类似的方式创建非连接表记录.
我遇到麻烦的地方是质量分配......它要么抛出上面的错误,要么找不到colors_id ......
来澄清我要做的事情:
复选框需要
1.创建一个新的"颜色"记录(@ color.new)与@car记录父项关联
2.将@ color.new记录
的"value_one"列设置为sample_color.value_one值3.设置@的"value_two"列color.new记录到sample_color.value_two值
4.创建的复选框数量= =迭代的@sample_colors.
car_model.rb
class CarModel
has_many :sample_colors, dependent: :destroy
has_many :cars, dependent: :destroy
Run Code Online (Sandbox Code Playgroud)
car.rb
class Car
has_many :colors, dependent: :destroy
belongs_to :car_model
accepts_nested_attributes_for :colors, allow_destroy: true
Run Code Online (Sandbox Code Playgroud)
sample_color.rb
class SampleColor
belongs_to :car_model
Run Code Online (Sandbox Code Playgroud)
color.rb
class Color
belongs_to :car
accepts_nested_attributes_for :finishes, allow_destroy: true
Run Code Online (Sandbox Code Playgroud)
_form(用于添加颜色)
<%= form_for @car do |f| %>
<%= f.fields_for 'car[color_attributes][]', @color, index: nil do |f| %>
<label class="form-label dk-aqua">Colors …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我在当前用户显示帖子(posts.current_user).我想通过comment.date排序来显示最近评论过的帖子.它似乎不想这样做...我一直在:
PG :: UndefinedTable:错误:缺少表"注释"的FROM子句条目
我的控制器
def_index
@posts = current_user.posts.includes(:comment).order("comment.date ASC").includes(:image)
end
Run Code Online (Sandbox Code Playgroud)
我试过加入并包括一个我似乎无法解决这个问题.谢谢.
我按照第 4 节(服务器端问题)在页面上设置 ajax。我已经完全复制了教程文本(用我自己的模型名称替换模型名称),它创建并保存我的“参与者”记录,但不会自动刷新 ajax 部分。
\n\n这是我得到的错误...看起来它引用了我的 create.js.erb
\n\nActionView::Template::Error (\'nil\' is not an ActiveModel-compatible object. It must implement :to_partial_path.):\n 1: $("<%= escape_javascript(render @participant) %>").appendTo("#participants");\n 2: // $(\'#participants\').html("<%= j (render @participants) %>");\n app/views/participants/create.js.erb:2:in `_app_views_participants_create_js_erb___1675277149181037111_70181034249880\'\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的代码
\n\nclass ParticipantsController < ApplicationController\ndef new\n @participant = Participant.new\n @participants = @participants.recently_updated\nend\n\ndef create\n @participant = Participant.new(participant_params)\n\n respond_to do |format|\n if @participant.save\n format.html { redirect_to @participant, notice: \'Helper Invited!\' }\n format.js {}\n format.json { render json: @participant, status: :created, location: @participant }\n else\n format.html …Run Code Online (Sandbox Code Playgroud) 这是我对Rails的第一次重大升级(v4到v5).我做了一些小的升级,比如4.1.1到4.2等...
我已升级到5.1.1,我在控制台中遇到此依赖性错误
Bundler could not find compatible versions for gem "actionmailer":
In Gemfile:
mail_form (= 1.7.0) was resolved to 1.7.0, which depends on
actionmailer (< 5.2, >= 3.2)
rails (>= 5.1.1, ~> 5.1) was resolved to 5.1.1, which depends on
actionmailer (= 5.1.1)
Run Code Online (Sandbox Code Playgroud)
但是,我安装了actionmailer 5.1.1.这看起来像它要求的.这些似乎是一个noob问题,但看起来rails必须有actionmailer 5.1.1,mail_form可以有小于5.2或等于/大于3.2的版本.5.1.1满足这两个条件......所以发生了什么?
它还说这进一步下降
Could not find gem 'rails (>= 5.1.1, ~> 5.1)' in any of the sources.
Run Code Online (Sandbox Code Playgroud)
但我已经卸载并重新安装了rails 5.1.1来确保.
我也试过用5.0.0这样做,并遇到了同样的问题.
这只是许多中的一个,很多"Bundler找不到兼容的gem版本"错误.
ruby '2.3.1'
source 'https://rubygems.org'
gem 'rails', '~> 5.1', '>= 5.1.1'
gem 'pg'
gem 'sass-rails', '~> …Run Code Online (Sandbox Code Playgroud) 3/13更新:
我用我的模型,控制器逻辑和几个表单版本做了一个小样本项目.
我正在构建一个表单,用户可以在其中添加"任务"和"里程碑".(即.任务='真空'在里程碑里面''干净的房子').它基本上是一个任务/子任务类型模型,父级为"里程碑",子级为"任务".
任务和里程碑都属于"项目"....所以我试图通过嵌套表单添加任务和里程碑以及更新操作.我想要的方法是为每个@task_template实例创建一个表单并一次更新多个表单.
我的问题是我也试图通过名为"MilestoneTemplates"和"TaskTemplates"的表动态设置"启动里程碑/任务"....
用户提取"添加里程碑/任务"页面,并根据其类型项目,他们在复选框旁边看到一系列预建任务(@task_templates)和里程碑(@milestone_templates).然后,用户检查他们要添加的任务或里程碑旁边的复选框.这应该为用户创建一个具有预建@ task_template.name,@ task_template.description ...等的特定任务
我甚至无法创建1.我正在使用Rails 4,我认为我已经正确设置了我的strong_params.以下是我对此的看法:
楷模:
class Task < ActiveRecord::Base
belongs_to :user
belongs_to :project
belongs_to :milestone
class Milestone < ActiveRecord::Base
belongs_to :project
belongs_to :user
has_many :tasks, dependent: :destroy, inverse_of: :milestone
accepts_nested_attributes_for :tasks, allow_destroy: true
class Project < ActiveRecord::Base
has_many :milestones, dependent: :destroy
has_many :tasks, dependent: :destroy
accepts_nested_attributes_for :tasks, allow_destroy: true
accepts_nested_attributes_for :milestones, allow_destroy: true
#the "Starter Milestones & Tasks"
class MilestoneTemplate < ActiveRecord::Base
has_many :task_templates, dependent: :destroy, inverse_of: :milestone_template
class TaskTemplate < ActiveRecord::Base
belongs_to …Run Code Online (Sandbox Code Playgroud) forms checkbox ruby-on-rails nested-attributes strong-parameters
我使用以下宝石:
timezonetzinfo我正试图在user时区发送一份时事通讯,以便他们在9:00:00收到时事通讯; 不是我的服务器所在的09:00:00 PST.
我按名称存储时区,如"太平洋时间(美国和加拿大)".每个用户都有一个列user.time_zone,其中存储了特定的时区.
我已经建立了job一个每小时检查user可以收到通讯的人(即user当地时间是上午9点的s).
class NewsletterTimezoneJob < ActiveJob::Base
def self.perform
users = User.all.gets_newsletter
users.each do |user|
d = Date.current
t = "09:00:00 -0700"
t = t.to_time
newsletter_sendtime = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec, t.zone)
newsletter_sendtime = newsletter_sendtime.to_s
if user.is_in_time_around(newsletter_sendtime)
NewsletterMailer.send_mailer(user, newsletter_sendtime).deliver_later
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
我已经构建了一个方法user,试图查看用户的本地时间是否匹配newsletter_sendtime.
我添加了一个+/- 5分钟的窗口来解释缓慢的服务器.
User.rb:
def is_in_time_around(timeofday)
timeofday = timeofday.to_time
user_zoned = timeofday.in_time_zone(self.time_zone) …Run Code Online (Sandbox Code Playgroud) 我正在 Rails 4 中创建一个简单的约会管理员,我需要创建一个时间数组,供用户在最小值/最大值之间进行选择。用户还可以选择是创建 30 分钟还是 60 分钟的约会。
我试图像这样创建数组
def time_range
return [] if time_start.blank? || time_end.blank?
(time_start.to_time .. time_end.to_time).to_a
end
Run Code Online (Sandbox Code Playgroud)
但我不断收到错误
can't iterate from Time
Run Code Online (Sandbox Code Playgroud)
我也不知道如何将其分解为增量。
我只是将它们显示为列表
ul.list-unstyled
- @meeting.time_range.each do |calendar_time|
li
= calendar_time
Run Code Online (Sandbox Code Playgroud) checkbox ×2
forms ×2
ruby ×2
activerecord ×1
ajax ×1
arrays ×1
asynchronous ×1
datatables ×1
include ×1
javascript ×1
join ×1
jquery ×1
nested-forms ×1
pluralize ×1
rubygems ×1
server-side ×1
sidekiq ×1
timezone ×1
updating ×1
whenever ×1