当我使用--apiRails 5 beta中的命令创建一个新的REST API时,它会安装我不需要的actioncable和sprocketsgems.
如何在没有这些宝石的情况下创建API?
我在Rails 5 beta 3中遇到了gem acts-as-taggable-on的问题.
project.rb:
class Project < ActiveRecord::Base
acts_as_taggable
acts_as_taggable_on :skills
end
Run Code Online (Sandbox Code Playgroud)
的routes.rb
get 'tags/:skill', to: 'projects#index', as: :skill
Run Code Online (Sandbox Code Playgroud)
projects_controller.rb:
class ProjectsController < ApplicationController
def index
if params[:category] && Category.exists?(params[:category])
@category = Category.find(params[:category])
@projects = @category.projects.order("projects.created_at DESC")
elsif params[:skill]
@projects = Project.tagged_with(params[:skill])
else
@projects = Project.all
end
@categories = Category.all
end
end
Run Code Online (Sandbox Code Playgroud)
在线@projects = Project.tagged_with(params[:skill])我得到以下错误:
ArgumentError:来自/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0.beta3/lib/active_record/sanitization.rb:8:in的参数数量错误(给定2,预期为1) `的sanitize"
我正在关注RailsTutorial,并且已经创建了具有适当的attrs和验证的Users模型.我遇到了这个错误,无法看到问题出在哪里.使用Rails5配置项目,因此继承ApplicationRecord的模型应该是正确的:
User.rb:
class User < ApplicationRecord
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
Run Code Online (Sandbox Code Playgroud)
结束
的Gemfile:
source 'https://rubygems.org'
ruby '2.3.0'
gem 'rails', '5.0.0.1'
gem 'bcrypt', '3.1.11'
gem 'faker', '1.6.3'
gem 'carrierwave', '0.11.2'
gem 'mini_magick', '4.5.1'
gem 'fog', '1.38.0'
gem 'will_paginate', '3.1.0'
gem 'bootstrap-will_paginate', …Run Code Online (Sandbox Code Playgroud) 今天我尝试使用一个很好的回调:after_commit在对象写入数据库时触发,但是,我收到了来自Rails的错误消息:
ActionController::RoutingError (undefined method `after_commit' for ImagesController:Class
Did you mean? after_action):
Run Code Online (Sandbox Code Playgroud)
嗯,这很令人尴尬!似乎这个回调被弃用了!通过搜索,我尝试使用:after_create_commit,它给了我同样的错误.
第三步是尝试:after_action.这里提出了一个问题: 如何使其工作方式与:after_commit?
我已经尝试过apidock.com - 它真的很小!我也尝试过api.rubyonrails.org - 它是关于块的,但我不是一个了解它的红宝石忍者.所以我真的很感激你是否可以在它上面洒一些光!
ImagesController:
class ImagesController < ApplicationController
after_create_commit :take_image_to_album
def take_image_to_album
if check_album
add_inner_to_album(@image)
end
end
def create
@image = Image.create(image_params.merge(:user_id => current_user.id)
respond_to do |format|
unless @image.save
format.html { render :show, notice: "Error!" }
format.json { render json: @image.errors, status: :unprocessable_entity }
else
format.html
format.json { render :show, status: :ok, location: @image }
end
end
end
...
def add_inner_to_album(image)
contents …Run Code Online (Sandbox Code Playgroud) 我需要发送请求给我的Rails API中包含的关键:ids: [null, 1, 2, null, 3]。不幸的是,Rails削减了该数组中的所有null,因此params[:ids]返回了[1, 2, 3]。我需要数组中的那些空值。
如何防止Rails移除它们?我可以发送空字符串而不是null,但这不是很优雅。
我有很多模型(> 50),每个模型共享相同的组关联.
像这样.
class Foo < ActiveRecord::Base
belongs_to :created_by_user, foreign_key: :created_by, class_name: 'User'
belongs_to :updated_by_user, foreign_key: :updated_by, class_name: 'User'
belongs_to :deleted_by_user, foreign_key: :deleted_by, class_name: 'User'
# other associations
end
Run Code Online (Sandbox Code Playgroud)
由于我的所有模型的关系完全相同(我们需要跟踪哪个用户更改了记录),无论如何要将这些关联包含在一个调用中?
像这样的东西?(这不起作用)
基本上我想要像:
class Foo < ActiveRecord::Base
include DefaultUserAssociation
# other associations
end
Run Code Online (Sandbox Code Playgroud) 在我的控制器/动作中,我有一些零值
def my_action
@var1 = get_boolean_value || nil
@var2 = get_int_value || nil
@var3 = get_string_value || nil
# there many more, any one can happen to be nil
end
Run Code Online (Sandbox Code Playgroud)
当我在视图中创建JSON对象时,nil值将呈现为空白空间:
:javascript
window.MyObj = {
var1: #{@var1},
var2: #{@var1},
var2: #{@var3}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当值为nil时,它将呈现为空白空间,而不是nil本身.
:javascript
window.MyObj = {
var1: ,
var2: 33,
var2:
}
Run Code Online (Sandbox Code Playgroud)
怎么治愈呢?
我跟随迈克尔·哈特尔(Michael Hartl)第13章的导轨教程,但不是微博,我正在创造动物.
我对动物的看法显示了一个"删除"超链接,该超链接应该从我的列表中删除动物记录,但是视图动作似乎没有达到它使用destroy方法的程度.
我在这里阅读了类似帖子中的所有答案,但在我的案例中找不到那些答案.
我正按照本教程中的说明,在AWS cloud9上的开发环境中.我真的很感激任何指针,因为我几天都在努力争取这个.
这是我的视图中的代码:
<li id="animal-<%= animal.id %>">
<%= link_to gravatar_for(animal.user, size: 50), animal.user %>
<span class="user"><%= link_to animal.user.name, animal.user %></span>
<span class="ear_tag"><%= animal.ear_tag %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(animal.created_at) %> ago.
<% if current_user?(animal.user) %>
<%= link_to "delete", animal, method: :delete, data: { confirm: "You sure?" } %>
<% end %>
</span>
</li>
Run Code Online (Sandbox Code Playgroud)
从Feed视图调用此视图:
<% if @feed_items.any? %>
<ol class="animals">
<%= render @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %> …Run Code Online (Sandbox Code Playgroud) 我是Ruby的新手。对不起我的英语不好。
我需要创建用于销毁所有用户对象的按钮(名为Relations和ListRelations的模型)。
这是我的config / routes.rb的一部分:
devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks' }
resources :relations
resources :list_relations
delete '/relations/destroy_member_data' => 'relations#destroy_member_data'
Run Code Online (Sandbox Code Playgroud)
Relationships_controller.rb
def destroy_member_data
if current_user.relations.destroy_all && current_user.list_relations.destroy_all
redirect_to(relations_path, :notice => 'All relations were successfully destroyed')
else
redirect_to(relations_path, :warning => 'Something went wrong. Please, try again.')
end
end
Run Code Online (Sandbox Code Playgroud)
Relationships / index.html.slim:
= link_to 'Destroy all data', relations_destroy_member_data_path, method: :delete, data: {confirm: 'Are you sure?'}
Run Code Online (Sandbox Code Playgroud)
当我单击此链接时,出现此错误:
Couldn't find Relation with 'id'=destroy_member_data
Extracted source (around line #59):
58: def destroy
59: @relation = …Run Code Online (Sandbox Code Playgroud) 我已经编写了一段代码,需要对其进行重构。
Reviewing.where(reviewing_status_condition(employee_ids)).group(:employee_id).count.map{
|employee_id, reviewings_count_per_employee|
employee_id if reviewings_count_per_employee >= @cycle.min_required_anon_feedback
}.compact
Run Code Online (Sandbox Code Playgroud)
有人能帮忙吗?
ruby-on-rails-5 ×10
ruby ×8
associations ×1
callback ×1
controller ×1
environment ×1
javascript ×1
json ×1