我在这里找到了一个示例,展示了如何安装Rails插件.他们的例子表明:
./script/plugin install calendar_helper
Run Code Online (Sandbox Code Playgroud)
这对我不起作用,因为令人困惑的是,plugin我的script目录中没有任何调用.我只有rails:
$ ls script/
rails
Run Code Online (Sandbox Code Playgroud)
那么Rails没有附带安装插件所需的脚本?你需要一个插件来安装插件?这似乎不太可能.我的Rails项目有问题吗?
在模型中说任务,我有以下验证
validates_presence_of :subject, :project, :user, :status
Run Code Online (Sandbox Code Playgroud)
如何使用其他控制器为这些验证呈现错误消息.
在我使用的CustomController里面,
Task.create(hash)
Run Code Online (Sandbox Code Playgroud)
在hash中传递nil值会产生以下错误,
ActiveRecord::RecordInvalid in CustomController#create
Validation failed: Subject can't be blank
Run Code Online (Sandbox Code Playgroud)
控制器的创建操作中的代码
@task = Task.create(hash)
if @task.valid?
flash[:notice] = l(:notice_successful_update)
else
flash[:error] = "<ul>" + @task.errors.full_messages.map{|o| "<li>" + o + "</li>" }.join("") + "</ul>"
end
redirect_to :back
Run Code Online (Sandbox Code Playgroud)
如何向用户呈现错误消息.
任务模型已经构建并呈现正确的消息.我想从插件中使用它的功能.
我想在我的rails应用程序之间共享2个(可能更多)之间的模型.我找不到任何明确的建议,但我找到了一些我已阅读过的问题和答案,并得出结论,必须使用"gemmed"插件引擎.我决定使用插件,因为我读到引擎只是一种"完整"的插件.
所以我使用以下方法创建了一个插件:( rails plugin new my_models --skip-active-record --skip-test-unit --dummy-path=spec/dummy选项用于跳过activerecord作为ORM并使用rspec进行测试).
创建插件后,我收到了以下文件:
my_models.gemspec Gemfile Gemfile.lock lib MIT-LICENSE Rakefile README.rdoc spec
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下方法包含模型:
app/models目录并将我的模型放入其中他们两个都失败了,然后我决定采用引擎建议(通过添加--mountable到"rails new"命令的选项列表),我得到了完整的rails应用程序结构(使用app,bin,db和其余的目录),把我的模型放在app/models目录中,它就像魔术一样!
因为我相信我是程序员而不是魔术师,所以我不会做这样的魔法,所以你能告诉我我的两个瘦插件解决方案(使用生成器/创建模型)有什么问题吗?而且,使用这些发电机有什么好处?
我附加了我的生成器代码,也许我想念一些东西:
require 'rails/generators/named_base'
require 'mongoid'
module Mongoid
module AttackGenerator
def generate_model
invoke "mongoid:model", [name] unless model_exists? && behavior == :invoke
end
def inject_field_types
inject_into_file model_path, migration_data, after: "include Mongoid::Document\n" if model_exists?
end
def migration_data
field :link_url, type: String
field :token, type: String
end
def model_exists?
File.exists?(File.join(destination_root, model_path)) …Run Code Online (Sandbox Code Playgroud) 我找到了一个名为acts_as_habtm_list的旧插件 - 但它适用于Rails 1.0.0.
这个功能现在是在acts_as_list中构建的吗?我似乎无法找到任何相关信息.
基本上,我有一个artists_events表 - 没有模型.通过指定的两个模型处理关系:has_and_belongs_to_many
在这种情况下如何指定订单?
ruby-on-rails has-and-belongs-to-many ruby-on-rails-plugins acts-as-list
我在rails应用程序中安装了restful_authentication插件,其session_controller具有如下所示的destroy方法:
def destroy
self.current_user.forget_me if logged_in?
cookies.delete :auth_token
reset_session
flash[:notice] = "You have been logged out."
redirect_back_or_default('/')
end
Run Code Online (Sandbox Code Playgroud)
在应用程序控制器中我有:
before_filter :login_required
Run Code Online (Sandbox Code Playgroud)
在sessions_controller我有:
skip_before_filter :login_required
Run Code Online (Sandbox Code Playgroud)
我的问题是,当用户使用http基本身份验证进行身份验证时,他/她不会注销.会话被销毁,但用户可以毫无问题地导航到受限制的页面.通过插件进行会话身份验证不会发生此问题.如何使这种方法摆脱基本的认证?
我有2个型号:
# user.rb
class User < ActiveRecord::Base
has_one :profile, :dependent => :destroy
end
# profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
validates_presence_of :user
end
# user_factory.rb
Factory.define :user do |u|
u.login "test"
u.association :profile
end
Run Code Online (Sandbox Code Playgroud)
我想做这个:
@user = Factory(:user)
=> #<User id: 88,....>
@user.profile
=> #<Profile id:123, user_id:88, ......>
@user = Factory.build(:user)
=> #<User id: nil,....>
@user.profile
=> #<Profile id:nil, user_id:nil, ......>
Run Code Online (Sandbox Code Playgroud)
但这不起作用!它告诉我,我的个人资料模型不正确,因为没有用户!(它在用户之前保存配置文件,因此没有user_id ...)
我怎样才能解决这个问题?尝试了一切.. :(我需要调用Factory.create(:user)...
UPDATE
修复了这个问题 - 现在正在使用:
# user_factory.rb
Factory.define :user do |u|
u.profile { Factory.build(:profile)}
end …Run Code Online (Sandbox Code Playgroud) testing ruby-on-rails ruby-on-rails-plugins ruby-on-rails-3 factory-bot
我只是尝试创建一个没有任何参数的插件迁移生成器,例如:$rails generate yaffle这应该将迁移文件(lib/generators/yaffle/template/create_yaffle.rb)复制到db/migrate/[timestamp] _create_yaffle.rb.
$rails generate yaffle它时,给我一条消息,表明没有提供参数,它希望采用这种格式rails generate yaffle NAME [options].我不想要任何选项/参数,它应该只是rails generate yaffle.我该怎么办?
我跟着acts_as_commentable中使用的生成器,它看起来很简单,但我不知道在哪里修改这些设置......有人可以帮忙吗?
发电机代码:
require 'rails/generators'
require 'rails/generators/migration'
class ThumbitGenerator Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
def self.next_migration_number(path)
Time.now.utc.strftime("%Y%m%d%H%M%S")
end
def create_model_file
template "like.rb", "app/models/like.rb"
template "liking.rb", "app/models/liking.rb"
template "create_likes.rb", "db/migrate/create_likes.rb"
template "create_likings.rb", "db/migrate/create_likings.rb"
end
end
Run Code Online (Sandbox Code Playgroud) 完成了Hartl伟大的Rails教程后,我现在正在通过非常好的jQuery -由Castledine和Sharkie 为Ninja的新手工作.虽然我能够在我的rails项目中包含jquery.js和jquery-ui.js,但在添加其他jQuery插件时我会陷入困境.
我已经发现当插件托管在github上时,我可以使用命令行将它们导入到我的项目中:
[my rails app]$ rails install [github URL]
Run Code Online (Sandbox Code Playgroud)
该插件成功导入/ vendor/plugin /目录,其中javascript_include_tag无法找到它们.这种情况提示3个问题:
有没有办法将jQuery插件导入我的Rails应用程序,将它们放入它们所属的public/javascripts /目录中?
如果1的答案是"否",有没有办法让javascript_include_tag搜索vendor/plugins /中的脚本?这似乎是不好的做法,因为jQuery脚本隐藏在public/javascript /目录之外,人们通常会在那里查找它们.
如果1和2都是"不",那么有没有更优雅的方式从vendor/plugins /移动文件而不是$ mv或复制和粘贴?
注意:通过网络搜索,我在http://getsprockets.org/上看到了37signals的sprokets gem,这似乎在某些方面可能有用,但超出了我目前的需求和理解.
感激任何想法!
jquery plugins jquery-plugins ruby-on-rails-plugins ruby-on-rails-3
我用
<%= wicked_pdf_stylesheet_link_tag "pdf" %>
它在html中显示以下输出
<link href="/stylesheets/pdf.css?1302860585" media="screen" rel="stylesheet" type="text/css">
<link href="file:///home/likewise-open/NEXTBRIDGE/nazar.hussain/osd/development/atlantis/public/stylesheets/pdf" media="screen" rel="stylesheet" type="text/css">
但是在创建pdf时它没有任何风格.如果我将所有css从文件复制到页面的标题,它包括所有样式.问题是什么以及如何解决.
ruby-on-rails ruby-on-rails-plugins ruby-on-rails-3 wicked-pdf
主要目标:允许插件,宝石向预定义表单添加其他表单字段.
例如,应用程序有一个设计登录表单:
所有注册表单中添加一个附加字段.<%= form_for(resource, :as => resource_name, ...) do |f| %>
<%= devise_error_messages! %>
...
<% end %>
有没有办法从我的rails-plugin/railtie向表单添加一个额外的字段,并定义一个promo code回调方法(对我的附加字段数据采取行动)?
优点:
看看ActionView代码,似乎没有内置的方法.你的想法是什么?
注意:Drupal的on_submit钩子就是一个很好的例子.