我想扩展类 ActiveStorage::Attachment 并添加一个枚举属性以查看附件。
我最初的方法是在 \app\models 目录中创建一个新文件attachment.rb,如下所示。
class ActiveStorage::Attachment < ActiveRecord::Base
enum visibility: [ :privately_visible, :publicly_visible]
end
Run Code Online (Sandbox Code Playgroud)
这不起作用。
欢迎任何建议。Rails 扩展类的方法是什么?
更新
我有一个现在部分有效的解决方案。为此,我创建了一个扩展 active_storage_attachment_extension.rb 并将其放置在 \lib
module ActiveStorageAttachmentExtension
extend ActiveSupport::Concern
included do
enum visibility: [ :privately_visible, :publicly_visible]
def describe_me
puts "I am part of the extension"
end
end
end
Run Code Online (Sandbox Code Playgroud)
扩展在 extensions.rb 中的初始化期间加载
ActiveStorage::Attachment.send(:include, ::ActiveStorageAttachmentExtension)
Run Code Online (Sandbox Code Playgroud)
不幸的是,它只是部分工作:虽然枚举方法public_visible?和privately_visible?在视图中可用,但在控制器中不可用。当调用控制器中的任何方法时,枚举似乎已经消失了。我收到“NoMethodError - 未定义方法”错误。令人惊讶的是,一旦枚举方法在控制器中被调用一次,它们在视图中也不再可用。我假设 ActiveStorage::Attachment 类会动态重新加载,并且扩展会丢失,因为它们仅在初始化期间添加。
有任何想法吗?
我还有一个非常简单的Rails应用程序,我希望使用带有Cucumber的BDD和带有RSpec的TDD开发.目前,我正在考试,我想检查是否由于验证错误而无法创建组织者的新实例(我的模型).我想检查要创建的对象的错误数组是否为空,以便我可以确保错误消息可用于在视图中显示它们.
需要'spec_helper'
描述OrganizersController做render_views
describe "POST 'create'" do
describe "with invalid arguments" do
before(:each) do
request.env["HTTP_REFERER"] = organizers_new_path
@organizer_args = { :name => "" }
end
it "should return a non-empty list of errors" do
post 'create', :organizer => @organizer_args
@organizer.errors.empty?.should_not be_true
end
end
end
Run Code Online (Sandbox Code Playgroud)
结束
我正在开发基于Rails 3.2.9的RSpec 2和cucumber-rails.
任何建议表示赞赏.谢谢!
我有一个Rails应用程序(Rails 3.0.10),其中用户可以有很多文章,用户可以在文章上留下评论.评论在文章显示页面上进行.
现在我想测试一下CommentsController的创建动作,但是,我遇到了使用正确的参数调用post方法的问题.
这是CommentsController的代码:
class CommentsController < ApplicationController
# create a comment and bind it to an article and a user
def create
@article = Article.find(params[:article_id])
@user = User.find(@article.user_id)
@comment = @article.comments.build(params[:comment])
@comment.user_id = current_user.id
commenters = []
@article.comments.each {
|comment|
commenters << User.find(comment.user_id)
}
commenters.uniq!
respond_to do |format|
if @comment.save
#Notify user who offers article on new comment, else notify the commenters
if @article.user_id != @comment.user_id
UserMailer.new_article_comment_email(@user, @comment).deliver
else
commenters.each {
|commenter|
UserMailer.new_article_comment_email(commenter, @comment).deliver
}
end
format.html {
redirect_to(@article) …Run Code Online (Sandbox Code Playgroud) 运行以下RSpec测试时,我得到一个空页作为响应:
require 'spec_helper'
describe FriendshipsController do
include Devise::TestHelpers
render_views
before(:each) do
@user = User.create!(:email => "max@mustermann.com", :password => "mustermann", :password_confirmation => "mustermann")
@friend = User.create!(:email => "john@doe.com", :password => "password", :password_confirmation => "password")
sign_in @user
end
describe "GET 'new'" do
it "should be successful" do
get 'new', :user_id => @user.id
response.should be_success
end
it "should show all registered users on Friendslend, except the logged in user" do
get 'new', :user_id => @user.id
page.should have_select("Add new friend")
page.should have_content("div.users")
page.should have_selector("div.users …Run Code Online (Sandbox Code Playgroud)