Answers:
要调用助手,请使用helper
对象:
$ ./script/console
>> helper.number_to_currency('123.45')
=> "R$ 123,45"
如果您要使用默认情况下未包含的帮助程序(例如,因为您已helper :all
从中删除ApplicationController
),则只需包含该帮助程序即可。
>> include BogusHelper
>> helper.bogus
=> "bogus output"
至于与控制器打交道,我引用了尼克的回答:
> app.get '/posts/1' > response = app.response # you now have a rails response object much like the integration tests > response.body # get you the HTML > response.cookies # hash of the cookies # etc, etc
url_for
从控制台打电话。为此,我做了app.url_for(...)
NoMethodError: undefined method `protect_against_forgery?' for nil:NilClass
定义protect_against_forgery?
在控制台中返回的函数false
ActionDispatch::Integration::Session.include(Warden::Test::Helpers); Warden.test_mode! ; app.login_as(User.find(1), scope: :user)
。
从脚本/控制台调用控制器动作并查看/操纵响应对象的简单方法是:
> app.get '/posts/1'
> response = app.response
# You now have a Ruby on Rails response object much like the integration tests
> response.body # Get you the HTML
> response.cookies # Hash of the cookies
# etc., etc.
该应用程序对象是ActionController :: Integration :: Session的实例
这对于使用Ruby on Rails 2.1和2.3的我来说是有效的,并且我没有尝试更早的版本。
如果您需要从控制台进行测试(在Ruby on Rails 3.1和4.1上进行了测试):
呼叫控制器动作:
app.get '/'
app.response
app.response.headers # => { "Content-Type"=>"text/html", ... }
app.response.body # => "<!DOCTYPE html>\n<html>\n\n<head>\n..."
ApplicationController方法:
foo = ActionController::Base::ApplicationController.new
foo.public_methods(true||false).sort
foo.some_method
路线助手:
app.myresource_path # => "/myresource"
app.myresource_url # => "http://www.example.com/myresource"
查看助手:
foo = ActionView::Base.new
foo.javascript_include_tag 'myscript' #=> "<script src=\"/javascripts/myscript.js\"></script>"
helper.link_to "foo", "bar" #=> "<a href=\"bar\">foo</a>"
ActionController::Base.helpers.image_tag('logo.png') #=> "<img alt=\"Logo\" src=\"/images/logo.png\" />"
渲染:
views = Rails::Application::Configuration.new(Rails.root).paths["app/views"]
views_helper = ActionView::Base.new views
views_helper.render 'myview/mytemplate'
views_helper.render file: 'myview/_mypartial', locals: {my_var: "display:block;"}
views_helper.assets_prefix #=> '/assets'
ActiveSupport方法:
require 'active_support/all'
1.week.ago
=> 2013-08-31 10:07:26 -0300
a = {'a'=>123}
a.symbolize_keys
=> {:a=>123}
库模块:
> require 'my_utils'
=> true
> include MyUtils
=> Object
> MyUtils.say "hi"
evaluate: hi
=> true
这是通过控制台执行此操作的一种方法:
>> foo = ActionView::Base.new
=> #<ActionView::Base:0x2aaab0ac2af8 @assigns_added=nil, @assigns={}, @helpers=#<ActionView::Base::ProxyModule:0x2aaab0ac2a58>, @controller=nil, @view_paths=[]>
>> foo.extend YourHelperModule
=> #<ActionView::Base:0x2aaab0ac2af8 @assigns_added=nil, @assigns={}, @helpers=#<ActionView::Base::ProxyModule:0x2aaab0ac2a58>, @controller=nil, @view_paths=[]>
>> foo.your_helper_method(args)
=> "<html>created by your helper</html>"
创建一个新的实例可以ActionView::Base
使您访问助手可能使用的常规视图方法。然后扩展YourHelperModule
将其方法混合到您的对象中,让您查看其返回值。
如果方法是POST
方法,则:
app.post 'controller/action?parameter1=value1¶meter2=value2'
(此处的参数将根据您的适用性。)
否则,如果是该GET
方法,则:
app.get 'controller/action'
app.methods.grep(/_path/)
:)
controler/action
,取决于您的路线,例如可能是/users/all
并映射到Api::UsersController#index
:)
rake routes
。:)
另一种方法是使用Ruby on Rails调试器。在http://guides.rubyonrails.org/debugging_rails_applications.html上有关于调试的Ruby on Rails指南。
基本上,使用-u选项启动服务器:
./script/server -u
然后在您的脚本中插入一个断点,以便您可以访问控制器,助手等。
class EventsController < ApplicationController
def index
debugger
end
end
并且,当您发出请求并点击代码中的该部分时,服务器控制台将返回提示,然后您可以在其中从命令提示符下发出请求,查看对象等。完成后,只需键入“继续”即可继续执行。也有用于扩展调试的选项,但这至少可以帮助您入门。
这是如何以精炼厂为例,发出经过身份验证的POST请求的方法:
# Start Rails console
rails console
# Get the login form
app.get '/community_members/sign_in'
# View the session
app.session.to_hash
# Copy the CSRF token "_csrf_token" and place it in the login request.
# Log in from the console to create a session
app.post '/community_members/login', {"authenticity_token"=>"gT7G17RNFaWUDLC6PJGapwHk/OEyYfI1V8yrlg0lHpM=", "refinery_user[login]"=>'chloe', 'refinery_user[password]'=>'test'}
# View the session to verify CSRF token is the same
app.session.to_hash
# Copy the CSRF token "_csrf_token" and place it in the request. It's best to edit this in Notepad++
app.post '/refinery/blog/posts', {"authenticity_token"=>"gT7G17RNFaWUDLC6PJGapwHk/OEyYfI1V8yrlg0lHpM=", "switch_locale"=>"en", "post"=>{"title"=>"Test", "homepage"=>"0", "featured"=>"0", "magazine"=>"0", "refinery_category_ids"=>["1282"], "body"=>"Tests do a body good.", "custom_teaser"=>"", "draft"=>"0", "tag_list"=>"", "published_at(1i)"=>"2014", "published_at(2i)"=>"5", "published_at(3i)"=>"27", "published_at(4i)"=>"21", "published_at(5i)"=>"20", "custom_url"=>"", "source_url_title"=>"", "source_url"=>"", "user_id"=>"56", "browser_title"=>"", "meta_description"=>""}, "continue_editing"=>"false", "locale"=>:en}
如果遇到错误,您可能也会发现这些有用:
app.cookies.to_hash
app.flash.to_hash
app.response # long, raw, HTML
NameError: undefined local variable or method app for main:Object
ApplicationController.allow_forgery_protection = false
在Ruby on Rails 3中,尝试以下操作:
session = ActionDispatch::Integration::Session.new(Rails.application)
session.get(url)
body = session.response.body
正文将包含URL的HTML。
较早的答案是调用助手,但是以下内容将帮助调用控制器方法。我在Ruby on Rails 2.3.2上使用过此功能。
首先,将以下代码添加到您的.irbrc文件(可以位于您的主目录中)
class Object
def request(options = {})
url=app.url_for(options)
app.get(url)
puts app.html_document.root.to_s
end
end
然后,在Ruby on Rails控制台中,您可以输入类似...
request(:controller => :show, :action => :show_frontpage)
...并且HTML将被转储到控制台。
在任何控制器动作或视图中,都可以通过调用console方法来调用控制台。
例如,在控制器中:
class PostsController < ApplicationController
def new
console
@post = Post.new
end
end
或在视图中:
<% console %>
<h2>New Post</h2>
这将在您的视图内呈现一个控制台。您无需关心控制台调用的位置;它不会在调用时立即呈现,而是在HTML内容旁边呈现。
请参阅:http : //guides.rubyonrails.org/debugging_rails_applications.html
对于控制器,您可以在Ruby on Rails控制台中实例化控制器对象。
例如,
class CustomPagesController < ApplicationController
def index
@customs = CustomPage.all
end
def get_number
puts "Got the Number"
end
protected
def get_private_number
puts 'Got private Number'
end
end
custom = CustomPagesController.new
2.1.5 :011 > custom = CustomPagesController.new
=> #<CustomPagesController:0xb594f77c @_action_has_layout=true, @_routes=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil>
2.1.5 :014 > custom.get_number
Got the Number
=> nil
# For calling private or protected methods,
2.1.5 :048 > custom.send(:get_private_number)
Got private Number
=> nil
def show response = @user.contributions end
如何覆盖@user
变量?
在Ruby on Rails控制台中进行Helper方法测试的一种可能方法是:
Struct.new(:t).extend(YourHelper).your_method(*arg)
对于重新加载,请执行以下操作:
reload!; Struct.new(:t).extend(YourHelper).your_method(*arg)
如果您添加了自己的帮助器,并且希望其方法在控制台中可用,请执行以下操作:
include YourHelperName
method_name(args)
。示例:假设您my_method
在'app / helpers / my_helper.rb`中拥有MyHelper(带有方法),然后在控制台中执行以下操作:
include MyHelper
my_helper.my_method
app.get
(随之而来的是线程错误)。有什么方法可以刷新系统并执行更多的获取?