我是Rails的新手如此宽容,如果这个问题相对简单 - 我在SO上遇到了所有其他类似的错误,没有任何解决方案可供使用。
我正在尝试打印此api调用中的成分列表并获取错误Rails:未定义的方法'成分'为零:NilClass。任何帮助表示赞赏!
我的代码段是:
关于页面:
<% content_for :title do %>About<% end %>
<h3>About the Website</h3>
<p>
This web application was created with
<%= link_to('Rails Composer', 'http://railsapps.github.io/rails-composer/') %>
from the <%= link_to('RailsApps project', 'http://railsapps.github.io/') %>.
Ingredients are <%= @recommendations.ingredients %>
</p>
模型:
class Recommendations
require 'httparty'
attr_accessor :matches, :ingredients
def fetch_recipe
encoded_url = URI.encode("http://api.yummly.com/v1/api/recipes?_app_id=ca19779e&_app_key=125204344682094834d68637a8222adf&q=chicken&allowedCourse[]=course^course-Lunch&maxResult=100&start=1")
parsed_url = URI.parse(encoded_url)
HTTParty.get(parsed_url)
end
def initialize
recipe_hash = fetch_recipe
assign_values(recipe_hash)
end
def assign_values(recipe_hash)
recipe_response = recipe_hash.parsed_response['matches'][0]
self.ingredients = recipe_response['ingredients']
end
end
控制器:
class RecommendationsController < ApplicationController
def home
@recommendations = Recommendations.new
end
end
日志:
Started GET "/pages/about" for 127.0.0.1 at 2014-07-28 17:43:40 -0700
Processing by HighVoltage::PagesController#show as HTML
Parameters: {"id"=>"about"}
Rendered pages/about.html.erb within layouts/application (1.0ms)
Completed 500 Internal Server Error in 4ms
NoMethodError - undefined method `ingredients' for nil:NilClass:
app/views/pages/about.html.erb:7:in `_app_views_pages_about_html_erb___310183375408592724_70240131703120'
actionview (4.1.2) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.2) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.2) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.2) lib/action_view/template.rb:143:in `render'
actionview (4.1.2) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
actionview (4.1.2) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.2) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.2) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.2) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.2) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
actionview (4.1.2) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
actionview (4.1.2) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
actionview (4.1.2) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionview (4.1.2) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.1.2) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.1.2) lib/action_view/rendering.rb:99:in `_render_template'
actionpack (4.1.2) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.1.2) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.1.2) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.1.2) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
actionpack (4.1.2) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.1.2) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.1.2) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.1.2) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
activesupport (4.1.2) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.1.2) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.1.2) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.1.2) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.1.2) lib/action_controller/metal/instrumentation.rb:40:in `render'
high_voltage (2.2.0) app/controllers/concerns/high_voltage/static_page.rb:28:in `show'
actionpack (4.1.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.2) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.2) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.2) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.2) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.2) lib/active_support/callbacks.rb:149:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.2) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.2) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.2) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.2) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.2) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.2) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.2) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.2) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.2) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.2) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.2) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.2) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.2) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.2) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.2) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.2) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:678:in `call'
bullet (4.12.0) lib/bullet/rack.rb:10:in `call'
meta_request (0.3.3) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
rack-contrib (1.1.0) lib/rack/contrib/response_headers.rb:17:in `call'
meta_request (0.3.3) lib/meta_request/middlewares/headers.rb:16:in `call'
meta_request (0.3.3) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.2) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.2) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.2) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.2) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.2) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.0.3) lib/quiet_assets.rb:23:in `call_with_quiet_assets'
actionpack (4.1.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.2) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.2) lib/rails/engine.rb:514:in `call'
railties (4.1.2) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
thin (1.6.2) lib/thin/connection.rb:86:in `block in pre_process'
thin (1.6.2) lib/thin/connection.rb:84:in `catch'
thin (1.6.2) lib/thin/connection.rb:84:in `pre_process'
thin (1.6.2) lib/thin/connection.rb:53:in `process'
thin (1.6.2) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
thin (1.6.2) lib/thin/backends/base.rb:73:in `start'
thin (1.6.2) lib/thin/server.rb:162:in `start'
rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
rack (1.5.2) lib/rack/server.rb:264:in `start'
railties (4.1.2) lib/rails/commands/server.rb:69:in `start'
railties (4.1.2) lib/rails/commands/commands_tasks.rb:81:in `block in server'
railties (4.1.2) lib/rails/commands/commands_tasks.rb:76:in `tap'
railties (4.1.2) lib/rails/commands/commands_tasks.rb:76:in `server'
railties (4.1.2) lib/rails/commands/commands_tasks.rb:40:in `run_command!'
railties (4.1.2) lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:8:in `require'
bin/rails:8:in `<top (required)>'
spring (1.1.3) lib/spring/client/rails.rb:27:in `load'
spring (1.1.3) lib/spring/client/rails.rb:27:in `call'
spring (1.1.3) lib/spring/client/command.rb:7:in `call'
spring (1.1.3) lib/spring/client.rb:26:in `run'
spring (1.1.3) bin/spring:48:in `<top (required)>'
spring (1.1.3) lib/spring/binstub.rb:11:in `load'
spring (1.1.3) lib/spring/binstub.rb:11:in `<top (required)>'
/Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
bin/spring:16:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
答案 0 :(得分:2)
class RecommendationsController < ApplicationController
def home
@recommendations = Recommendations.new
end
def about
@recommendations = Recommendations.new
end
end
将推荐实例化添加到您的约会操作中。
更好的是,将它重构为一种方法:
class RecommendationsController < ApplicationController
before_filter :new_recommendation, :only =>[:home, :about]
private
def new_recommentation
@recommendations = Recommendations.new
end
end
答案 1 :(得分:-1)
nil
的成分在逻辑上也可以是nil
。通过定义
def nil.ingredients
nil
end