我正在使用Devise进行身份验证,当有人访问特定的URL(URL中有特定的params)时,我想将"current_user"设置为db中的特定值.
我怎么做 ?
另外,我是否必须确保满足所有验证要求?例如,我的用户模型有:
validates_presence_of :username, :email
Run Code Online (Sandbox Code Playgroud)
如果我设置current_user,我是否必须同时设置:username&:email,或者我可以只做一个或另一个?
这个 select2 jquery库看起来很棒.有一个Rails宝石,但它在文档上非常轻松.我想使用自动完成功能生成一个简单的多下拉菜单.我怎么做?
这是我的simple_form_for调用:
<%= f.input_field :neighborhood_names, url: autocomplete_neighborhood_name_searches_path, as: :autocomplete, data: { delimiter: ',', placeholder: "Where do you want to live?"}, multiple: true, id: "selectWhereToLive", class: "span8" %>
Run Code Online (Sandbox Code Playgroud)
我已经成功安装了select2-railsgem,但不太确定如何让它工作.
我把它添加到我的home.js.coffee文件中:
jQuery ->
$('#selectWhereToLive').select2()
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
Uncaught query function not defined for Select2 selectWhereToLive
Run Code Online (Sandbox Code Playgroud)
思考?
编辑1:
上面的simple_form_for调用产生了这个HTML:
<input class="autocomplete optional span8" data-autocomplete="/searches/autocomplete_neighborhood_name" data-delimiter="," data-placeholder="Where do you want to live?" id="selectWhereToLive" multiple="multiple" name="search[neighborhood_names][]" size="30" type="text" url="/searches/autocomplete_neighborhood_name" value="" />
Run Code Online (Sandbox Code Playgroud)
指示id正确设置属性.
编辑2 - 更新 …
ruby-on-rails ruby-on-rails-3 simple-form jquery-select2 select2-rails
我目前正在学习Rails,但答案不一定是Rails特定的.
所以,据我所知,安全密码系统的工作原理如下:
登录尝试后:
据我所知,这种方法受到彩虹攻击 - 其中可能发生以下情况.
攻击者可以编写一个基本上尝试每个字符,数字和符号排列的脚本,使用相同的加密算法创建一个哈希值,并将它们与数据库中的哈希值进行比较.
因此,围绕它的方法是将哈希与唯一的盐结合起来.在许多情况下,用户注册的当前日期和时间(低至毫秒).
但是,此salt存储在数据库列'salt'中.
所以我的问题是,这是如何改变这样一个事实:如果攻击者首先获得了对数据库的访问权并且为"真实"密码创建了哈希并且还具有盐的哈希值,那么这又不仅如此受彩虹袭击?因为,理论上他会尝试每个排列+盐哈希,并将结果与密码哈希进行比较.可能需要更长时间,但我不知道它是多么万无一失.
原谅我的无知,我只是在学习这些东西,这对我来说从来没有多大意义.
我记得有一篇或者几篇关于作者如何根据SSH地址将终端颜色从绿色(用于开发)更改为红色(用于生产)的文章.
但我不知道如何使用Heroku控制台复制它.
理想情况下,当我这样做时,我希望它可以说是蓝色,heroku console --remote staging然后是红色heroku console --remote production.
有人建议吗?
我有一个帮助方法dashboard_helper.rb,看起来像这样:
def show_number_of_comments(node)
if node.comments_count == 1
"#{node.comments_count} Comment"
else
"#{node.comments_count} Comments"
end
end
Run Code Online (Sandbox Code Playgroud)
在我的常规dashboard#index视图中,我称之为:
<h4 class="card-comments-title"><%= show_number_of_comments(node) %></h4>
Run Code Online (Sandbox Code Playgroud)
但是我想在添加新注释时通过AJAX更新渲染的元素,所以在我看来comment#create.js.erb,我想引用那个帮助方法,但是当我尝试这个时,它不起作用:
$('#<%= @card_id %> .card-comments-title').html('<%= show_number_of_comments(@node) %>');
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,它的工作原理是:
$('#<%= @card_id %> .card-comments-title').html('<%= @comment_count %> Comments');
Run Code Online (Sandbox Code Playgroud)
后者的问题是它不处理多元化.
什么是最好的方法来解决这个问题?
编辑1
当我说它不起作用时,这就是我的意思:
NoMethodError at /nodes/5/comments
==================================
> undefined method `show_number_of_comments' for #<#<Class:0x007fbd3715e5b8>:0x007fbd3715d4d8>
app/views/comments/create.js.erb, line 5
----------------------------------------
Run Code Online (Sandbox Code Playgroud)
此外,该@node对象在我Comments#Create这样声明:
def create
@node = Node.find(params[:node_id])
@comment = current_user.comments.new(comment_params)
@comment.node = @node
@card_id = params[:card_id]
@comment_count = @node.comments_count …Run Code Online (Sandbox Code Playgroud) 如果我有一个字符串c1,我可以通过执行以下操作将其打印出来:
c1.each_line do |line|
puts line
end
Run Code Online (Sandbox Code Playgroud)
我想用每行给出每行的编号,如下所示:
c1.each_with_index do |line, index|
puts "#{index} #{line}"
end
Run Code Online (Sandbox Code Playgroud)
但这不适用于字符串.
我试过用$..当我在上面的迭代器中这样做时:
puts #{$.} #{line}
Run Code Online (Sandbox Code Playgroud)
它打印每行最后一行的行号.
我也试过使用lineno,但这似乎只在我加载文件时起作用,而不是在我使用字符串时.
如何打印或访问字符串上每行的行号?
我正在使用middleman-blog和middleman-deploy.
我想要做的是,在我部署的分支内,我希望将静态文件部署到repo中的子文件夹(即不是根文件夹).
我尝试在我这样做config.rb:
activate :deploy do |deploy|
deploy.build_before = true
deploy.deploy_method = :git
deploy.branch = 'gh-pages-2'
deploy.remote = 'github'
deploy.path = 'blog'
end
Run Code Online (Sandbox Code Playgroud)
但这不起作用,它仍然部署到根目录.事实上,它甚至没有创建/blog我正在寻找的文件夹.
当我在本地访问配置设置时,这些是我在下面看到的设置:deploy:
:deploy
:branch = "gh-pages"
:build_before = true
:clean = false
:commit_message = nil
:deploy_method = :git
:flags = nil
:host = nil
:password = nil
:path = "blog"
:port = 22
:remote = "github"
:strategy = :force_push
:user = nil
Run Code Online (Sandbox Code Playgroud)
这向我表明正确设置了path属性.
我也尝试过deploy.path = '/blog' …
这是用于在我的Rails应用程序中触发Plupload的代码:
<% content_for :deferred_js do %>
$("#uploader").pluploadQueue({
runtimes : 'gears,html5,flash,browserplus,silverlight,html4',
url : '/uploads.js',
//browse_button : 'pickfiles',
max_file_size : '10mb',
chunk_size : '2mb',
unique_names : false,
container: 'uploader',
autostart: true,
//RoR - make sure form is multipart
//multipart: true,
// Specify what files to browse for
filters : [
{title : "Image files", extensions : "jpg,gif,png,bmp"}
],
// PreInit events, bound before any internal events
preinit : {
UploadFile: function(up, file) {
up.settings.multipart_params = {"upload[stage_id]" : compv.steps.selectedStage.getID(), "authenticity_token" : compv.tools.csrf_token()};
} …Run Code Online (Sandbox Code Playgroud) 我有两个型号 - Banner和BannerType.
他们的架构如下所示:
旗帜
# Table name: banners
#
# id :integer not null, primary key
# name :string(255)
# image :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# url :string(255)
# banner_type_id :integer
Run Code Online (Sandbox Code Playgroud)
BannerType
# Table name: banner_types
#
# id :integer not null, primary key
# name :string(255)
# created_at :datetime not null
# updated_at :datetime not null
Run Code Online (Sandbox Code Playgroud)
Banner belongs_to :banner_type 和 BannerType has_many :banners
我在BannerType中有两条记录:
BannerType.all
BannerType Load (0.3ms) …Run Code Online (Sandbox Code Playgroud) 我正在使用CanCanCan,Devise和Rolify.
我ApplicationController看起来像这样:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :new_post
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, :alert => exception.message
end
def new_post
@post = Post.new
end
end
Run Code Online (Sandbox Code Playgroud)
我routes.rb看起来像这样:
authenticate :user, lambda { |u| u.has_role? :admin or :editor } do
get 'newsroom', to: 'newsroom#index', as: "newsroom"
get 'newsroom/published', to: 'newsroom#published'
get 'newsroom/unpublished', to: 'newsroom#unpublished'
end
# truncated for …Run Code Online (Sandbox Code Playgroud) devise ×2
javascript ×2
jquery ×2
ruby ×2
ajax ×1
arel ×1
bash ×1
cancan ×1
cancancan ×1
cryptography ×1
deployment ×1
encryption ×1
git ×1
heroku ×1
middleman ×1
passwords ×1
plupload ×1
rolify ×1
salt ×1
simple-form ×1
string ×1
terminal ×1