小编lll*_*lll的帖子

使用has_secure_password Rails update_attributes

我正在一个用户可以拥有的项目中工作admin: true / false.该应用程序只允许管理员用户登录系统,其他用户只是其设置只有管理员可以编辑的客户端.这是某种商业应用程序.(Rails 3.2.13)

我想将这两个概念保存在同一个表中,因为将来可能会选择登录非管理员用户并与他们的配置文件进行交互,因此所有逻辑都已经实现.

我有这个User资源:

ActiveRecord::Schema.define(:version => 20131204200554) do

  create_table "users", :force => true do |t|
    t.string   "name"
    t.string   "surname"
    t.boolean  "admin"
    t.string   "password_digest"
    t.string   "email"
    t.string   "auth_token"
  end

end
Run Code Online (Sandbox Code Playgroud)

这是用户模型:

class User < ActiveRecord::Base
    has_secure_password
    attr_accessible :name, :surname,:email, :password, :password_confirmation

    validates :name, presence:true, length: { maximum:50}
    validates :first_surname, presence:true, length: { maximum:50}
    VALID_EMAIL_REGEX= /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true,format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive:false}
    validates :password, length:{minimum:6}
    validates :password_confirmation, presence:true …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails update-attributes

2
推荐指数
1
解决办法
2868
查看次数

Rails单表继承.共享控制器,如何更新(CRUD)

我有三个类Admin,Client,Agent所有继承User < ActiveRecord::Base.系统使用STI设计,因此所有类共享同一个users表.

我有兴趣保持CRUD功能几乎相同,因此现在我使用单个UsersController.

在实施更新功能时,我遇到了疑问.这是我的编辑表格:

#Edit Form
<%= form_for(@user,{url:user_path(@user),method: :put}) do |f| %>
    <%= render 'edit_fields',f:f %>
    <%= f.submit "Save", class: "btn btn-large btn-primary"%>
<% end %>
Run Code Online (Sandbox Code Playgroud)
#UsersController
def edit
    @user=User.find(params[:id])
end
def update
    binding.pry
   #if @user.update_attributes(params[:user])   #<---BEFORE
   #WORKAROUND BELOW
   if @user.update_attributes(params[@user.type.downcase.to_sym])
       flash[:success]="User was updated successfully."
       redirect_to user_path(@user)
   else
        flash[:danger]="User could not be updated."
        render 'new'
   end
end
Run Code Online (Sandbox Code Playgroud)

我的"问题"是params取决于@user.type该的@user实例.因此有时会有一个params[:client],有时则是params[:admin] …

ruby ruby-on-rails crud sti

2
推荐指数
1
解决办法
1206
查看次数

如何仅在第一次通过时以 root 身份运行 Ansible?

这是我试图通过 Ansible 完成的一步一步:

  1. 以 root 身份进行 SSH
  2. 安装python(ubuntu中不存在)和其他基本包。
  3. 创建新deploy用户和配置,/etc/ssh/sshd_config以便PasswordAuhentication noPermitRootLogin no
  4. 重新启动 ssh 服务。

后来我用新任务、角色等更新我的剧本。所以我想针对同一台服务器(访问被root阻止)重新运行剧本,只是这次以新创建的用户身份进行访问。

我预计会返回Permission denied访问权限,因为 Ansible 正在尝试以 root 身份进行访问。

问题

  • 我怎样才能以 root 身份执行第一遍,然后在下一个剧本运行时跳过根任务(在本例中为pre_tasks )?

一种选择是将其分成两本单独的剧本:一本用于配置,一本用于其余。

# playbook.yml
---
- name: Prepare server
  hosts: webserver
  gather_facts: False
  pre_tasks:
    - name: Install python for Ansible
      remote_user: root
      raw: type /usr/bin/python || (apt -y update && apt install -y python)
    - name: Create user
      remote_user: root
      include_role: …
Run Code Online (Sandbox Code Playgroud)

python ssh ubuntu ansible devops

2
推荐指数
1
解决办法
2175
查看次数

现有工作应用程序上的"rails server"无法启动

我很高兴配置我的新笔记本电脑来处理我现有的项目.我复制了一个应用程序及其所有子文件夹的文件夹,现在将它放在新的笔记本电脑中.看来我无法通过rails server在项目的根目录键入命令来启动服务器.我试图创建一个新项目并启动它,它工作正常,所以我真的不知道该怎么做.Rails版本相同且相同Gemfile.这是rails server命令的结果:

Usage:
  rails new APP_PATH [options]

Options:
  -r, [--ruby=PATH]              # Path to the Ruby binary of your choice
                             # Default: /home/toni/.rvm/rubies/ruby-1.9.3-p374/bin/ruby
  -b, [--builder=BUILDER]        # Path to a application builder (can be a filesystem   path or URL)
  -m, [--template=TEMPLATE]      # Path to an application template (can be a filesystem path or URL)
  [--skip-gemfile]           # Don't create a Gemfile
  [--skip-bundle]            # Don't run bundle install
  -G, [--skip-git]               # Skip Git ignores and keeps
  -O, …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

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

Vim在冒号后使用Visual块选择删除/插入

我有这段代码:

fill_in "Name",with: "John"
fill_in "Email",with: "user@example.com"
fill_in "Password",with: "supersafe"
Run Code Online (Sandbox Code Playgroud)

我想更换之后所写的一切事:user.,所以我可以只需添加完成代码name,emailpassword经过相应的,user..

fill_in "Name",with: user.name
fill_in "Email",with: user.email
fill_in "Password",with: user.password
Run Code Online (Sandbox Code Playgroud)

此外,从选择的视觉块的一行导航到另一行的方式(可能是一个vim插件?)当然是最佳的,虽然不是这里的要点.

一个更简单的选择是不用替换它user.,而是删除它d$; 然而选择问题仍然存在.

我尝试了正常Ctrl-V选择但由于:字符放在每行的不同列中,我没有所需的行为.

谢谢

vi vim editor

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

标签 统计

ruby-on-rails ×3

ansible ×1

crud ×1

devops ×1

editor ×1

python ×1

ruby ×1

ssh ×1

sti ×1

ubuntu ×1

update-attributes ×1

vi ×1

vim ×1