有人能告诉我在Rails 4中执行以下行的等效方法是什么?
has_many :friends, :through => :friendships, :conditions => "status = 'accepted'", :order => :first_name
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
has_many :friends, -> { where status: 'accepted' }, :through => :friendships , :order => :first_name
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Invalid mix of scope block and deprecated finder options on ActiveRecord association: User.has_many :friends
Run Code Online (Sandbox Code Playgroud) 我有两种选择形式remote: true
; 一个发送Ajax请求以执行create
操作,另一个发送Ajax请求以执行destroy
操作.
启用JavaScript后所有工作都会被罚款,但如果我禁用JavaScript,那么我点击,我收到此错误:
ActionController::InvalidAuthenticityToken PersonsController#create
Run Code Online (Sandbox Code Playgroud)
为什么会显示此错误,如何解决?
注意:我正在使用Rails 4
当我使用没有选项的普通表单时remote: true
,rails会自动为身份验证令牌插入一个隐藏字段,但是当我remote: true
在表单中使用时,HTML代码中没有这样的字段.看起来当有remote
选项时,Rails会以不同的方式处理身份验证令牌,那么我如何才能在两种情况下都能使用它?
psql --version
psql (PostgreSQL) 9.4.1
rails -v
Rails 4.2.0
Run Code Online (Sandbox Code Playgroud)
我通过迁移添加了一个jsonb列
class AddPreferencesToUsers < ActiveRecord::Migration
def change
add_column :users, :preferences, :jsonb, null: false, default: '{}'
add_index :users, :preferences, using: :gin
end
end
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
PG::UndefinedObject: ERROR: type "jsonb" does not exist
LINE 1: SELECT 'jsonb'::regtype::oid
Run Code Online (Sandbox Code Playgroud)
任何帮助?
我尝试为has_one关联创建一个嵌套的模型表单.(我正在使用Rails 4)
在我的用户和地址模型中,我有以下内容:
class User < ActiveRecord::Base
has_one :address
accepts_nested_attributes_for :address
end
class Address < ActiveRecord::Base
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
我的用户控制器:
class UsersController < ApplicationController
.
.
.
def edit
@user = User.find(params[:id])
@user.build_address if @user.address.nil?
end
def update
@user = User.find(params[:id])
if @user.update(params.require(:user).permit(:user_name, address_attributes: [:street]))
flash[:success] = "Profile updated successfully"
sign_in @user
redirect_to @user
else
flash.now[:error] = "Cannot updating your profile"
render 'edit'
end
end
end
Run Code Online (Sandbox Code Playgroud)
终于在我看来我有:
= form_for(@user) do |f|
= render 'shared/error_messages', object: f.object
%div
= f.label :user_name, …
Run Code Online (Sandbox Code Playgroud) 当您按contentEditable
元素上的Enter键时,每个浏览器都会以不同方式处理生成的代码:Firefox会插入BR标记,Chrome会在Internet Explorer插入P标记时插入DIV标记.
我拼命寻找解决方案,至少为所有浏览器使用BR或P,最常见的答案是:
插入BR标签:
$("#editableElement").on("keypress", function(e){
if (e.which == 13) {
if (window.getSelection) {
var selection = window.getSelection(),
range = selection.getRangeAt(0),
br = document.createElement("br");
range.deleteContents();
range.insertNode(br);
range.setStartAfter(br);
range.setEndAfter(br);
selection.removeAllRanges();
selection.addRange(range);
return false;
}
}
});
Run Code Online (Sandbox Code Playgroud)
但是这不起作用,因为似乎浏览器不知道如何设置插入符号,<br>
这意味着以下操作没有做任何有用的事情(特别是如果在插入符号放在文本末尾时按Enter键):
range.setStartAfter(br);
range.setEndAfter(br);
Run Code Online (Sandbox Code Playgroud)
有些人会说:使用double <br><br>
但是当你在文本节点中按Enter键时会产生两个换行符.
其他人会说总是<br>
在contentEditable的末尾添加一个额外的,但是如果你有一个<div contenteditable><p>text here</p></div>
并且你把光标放在文本的末尾然后点击回车,你将得到错误的行为.
所以我对自己说,也许我们可以使用P而不是BR,常见的答案是:
插入P标签:
document.execCommand('formatBlock', false, 'p');
Run Code Online (Sandbox Code Playgroud)
但这也不能始终如一地发挥作用.
如您所见,所有这些解决方案都有待改进.还有其他解决方案可以解决这个问题吗?
我想存储一个大的json哈希(或内容,像你想要的那样调用)和"大"我的意思是超过1000个键值对,我不想在那个json字段上做任何搜索,我只是想要从数据库中检索它并将其传递给javascript来解析它并构造一个可视化结果.
在Postgresql上有一个json
类型和jsonb
类型(也许我也可以使用一个text
字段来存储json).我只想为此目的做出正确的选择,所以我希望有经验的人提供一些建议.
这个问题已经被问到,但是直到现在还没有工作的答案,所以我很想再次打开它,希望我们可以找到它.
我有一个contentEditable段落和一个文本输入,当我选择一些文本并单击输入时,选择就消失了.
所以我试图在输入mousedown上保存选择并在mouseup上恢复它,是的它可行(如预期在firefox中)但是...在chrome 中输入失去焦点 :(
看到它在行动(使用铬):https://jsfiddle.net/mody5/noygdhdu/
这是我用过的代码:
HTML
<p contenteditable="true">
Select something up here and click the input below
<br> on firefox the input get the focus and the text still selected.
<br> on chrome the text still selected but the input lose focus
</p>
<input type="text" id="special" style="border: solid blue 1px">
Run Code Online (Sandbox Code Playgroud)
JavaScript的
function saveSelection() {
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
return …
Run Code Online (Sandbox Code Playgroud) 我想捕获textarea(keyup,以及copy和past)中发生的变化,我使用的keyup选项:
$("textarea").keyup(function(){
// ajax call here
});
Run Code Online (Sandbox Code Playgroud)
我添加了这个以捕获粘贴或通过鼠标切割然后触发textarea上的keyup事件:
$("textarea").on('input propertychange', function() {
$(this).trigger(keyup);
});
Run Code Online (Sandbox Code Playgroud)
这里的问题是,如果我按下键盘上的一个键,我得到2个ajax调用,因为第二个函数捕获也是键盘事件.
有没有办法防止$("textarea").on('input propertychange'...
检测到按键?
我创建了一个虚拟应用程序
Rails new myapp --skip-test
Run Code Online (Sandbox Code Playgroud)
然后向其中添加一个 Dockerfile,如下所示:
FROM ruby:2.6
RUN apt-get update -yqq
RUN apt-get install -yqq --no-install-recommends nodejs
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN bundle install
Run Code Online (Sandbox Code Playgroud)
每当我构建图像时
docker build . # let's say the image id will be b2b0674325d1
Run Code Online (Sandbox Code Playgroud)
然后我尝试运行服务器sudo docker run -p 3000:3000 b2b0674325d1 bin/rails s -b 0.0.0.0
我收到这个错误
=> Booting Puma
=> Rails 6.0.2.1 application starting in development
=> Run `rails server --help` for more startup options
sh: 1: yarn: not found
========================================
Your Yarn packages …
Run Code Online (Sandbox Code Playgroud) 我是redis的新手,我发现学习redis的基础非常简单,但是当我试着理解我如何使用它时,它会变成流动,我找不到任何好的tutoriel从头开始解释步骤,因为例如,我找到这样的代码:
class User < ActiveRecord::Base
# follow a user
def follow!(user)
$redis.multi do
$redis.sadd(self.redis_key(:following), user.id)
$redis.sadd(user.redis_key(:followers), self.id)
end
end
# unfollow a user
def unfollow!(user)
$redis.multi do
$redis.srem(self.redis_key(:following), user.id)
$redis.srem(user.redis_key(:followers), self.id)
end
end
Run Code Online (Sandbox Code Playgroud)
但是在这个例子中,没有其他例子说明如何使用follow
方法,我需要传递给这个方法的对象是什么(这个对象来自关系数据库?或者是什么)等等......
我在搜索中找到的所有示例都是不完整的,当我们决定将它与rails一起使用时,这种情况并不容易!
我还发现redis的使用是在继承ActiveRecord的模型中,我无法理解:如果在大多数情况下使用redis使用关系数据库或单独使用,以及最常用的方式是什么,以及具体如何?
我知道我的问题是巨大的,但我搜索的是如何使用redis和rails,如果你有一个很好的资源,我将非常感激.谢谢
javascript ×3
postgresql ×2
range ×2
selection ×2
docker ×1
jquery ×1
json ×1
jsonb ×1
redis ×1
textarea ×1