如何在Rails中从控制台调用控制器/视图方法?

时间:2008-09-29 22:36:22

标签: ruby-on-rails console

当我加载script/console时,有时我想要使用控制器的输出或视图辅助方法。

有办法:

  • 模拟请求?
  • 根据上述请求从控制器实例调用方法?
  • 通过所述控制器实例或其他方式测试助手方法?

14 个答案:

答案 0 :(得分:466)

要调用助手,请使用helper对象:

$ ./script/console
>> helper.number_to_currency('123.45')
=> "R$ 123,45"

如果您想使用默认情况下未包含的帮助程序(例如,因为您从helper :all删除了ApplicationController),只需包含帮助程序。

>> include BogusHelper
>> helper.bogus
=> "bogus output"

至于处理控制器,我引用Nick's回答:

> 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

答案 1 :(得分:142)

从脚本/控制台调用控制器操作并查看/操作响应对象的简单方法是:

> 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

app对象是ActionController::Integration::Session

的一个实例

这适用于我使用Rails 2.1和2.3,我没有尝试早期版本。

答案 2 :(得分:101)

如果您需要从控制台进行测试(在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}

Lib模块:

> require 'my_utils'
 => true 
> include MyUtils
 => Object 
> MyUtils.say "hi"
evaluate: hi
 => true 

答案 3 :(得分:73)

以下是通过控制台执行此操作的一种方法:

>> 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扩展到您的对象中,让您查看其返回值。

答案 4 :(得分:15)

另一种方法是使用rails调试器。有关于http://guides.rubyonrails.org/debugging_rails_applications.html

的调试的Rails指南

基本上,使用-u选项启动服务器:

./script/server -u

然后在您希望有权访问控制器/助手/等的脚本中插入一个断点。

class EventsController < ApplicationController
  def index
    debugger
  end
end

当您发出请求并在代码中点击该部分时,服务器控制台将返回一个提示,您可以从命令提示符处创建请求,查看对象等。完成后,只需输入'cont'即可继续执行。还有扩展调试的选项,但这至少应该让你开始。

答案 5 :(得分:14)

如果方法是POST方法,那么

app.post 'controller/action?parameter1=value1&parameter2=value2'

[此处参数将根据您的适用性]

否则,如果是GET方法则

app.get 'controller/action'

答案 6 :(得分:12)

以下是使用Refinery作为示例进行经过身份验证的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

答案 7 :(得分:10)

您可以在Rails控制台中访问您的方法,如下所示

controller.method_name
helper.method_name

答案 8 :(得分:8)

在rails 3中,试试这个:

session = ActionDispatch::Integration::Session.new(Rails.application)
session.get(url)
body = session.response.body

正文将包含网址的HTML。

How to route and render (dispatch) from a model in Rails 3

答案 9 :(得分:7)

之前的答案是调用助手,但以下内容将有助于调用控制器方法。我在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

然后在rails控制台中输入类似......

的内容
request(:controller => :show, :action => :show_frontpage)

...并且html将被转储到控制台。

答案 10 :(得分:3)

在任何控制器操作或视图中,您可以通过调用控制台方法来调用控制台。

例如,在控制器中:

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

答案 11 :(得分:2)

在rails控制台中进行Helper方法测试的一种可能方法是

Struct.new(:t).extend(YourHelper).your_method(*arg)

并重新加载并执行

reload!; Struct.new(:t).extend(YourHelper).your_method(*arg)

答案 12 :(得分:2)

对于控制器,您可以在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

答案 13 :(得分:1)

如果你已经添加了自己的帮助器,并且希望它的方法在控制台中可用:

  1. 在控制台中执行include YourHelperName
  2. 您的帮助方法现在可以在控制台中使用,在控制台中使用它们调用method_name(args)
  3. 示例:假设您在'app / helpers / my_helper.rb`中有MyHelper(使用方法my_method),然后在控制台中执行:

    1. include MyHelper
    2. my_helper.my_method