0

我是 Ruby on Rails 的新手。我正在关注这里的教程Getting Started Ruby on Rails

在创建帖子的中途,我偶然发现了一个错误,即 NoMethodError in PostsController#create 在我的控制器中。我现在很困惑。

这是我从教程中遵循的代码。我在这里犯了一个愚蠢的错误吗?

post_controller.rb

 class PostsController < ApplicationController

  def new

  end

  def create
    @post = Post.new(post_params)

    @post.save
    redirect_to @post
  end

  def show
    @post = Post.find(params[:id])
  end

  private
  def post_params
    params.require(:post).permit(:title, :text)
  end

end

我的帖子/new.html.erb

<%= form_for :post, url: posts_path do |f| %>
    <p>
      <%= f.label :title %><br>
      <%= f.text_field :title %>
    </p>

    <p>
      <%= f.label :text %><br>
      <%= f.text_area :text %>
    </p>

    <p>
      <%= f.submit %>
    </p>
<% end %>

路线.rb

Blog::Application.routes.draw do

  resource :posts
  root to: "welcome#index"

end

模型/post.rb

class Post < ActiveRecord::Base
end

和堆栈跟踪

actionpack (4.0.0) lib/action_dispatch/routing/polymorphic_routes.rb:129:in `polymorphic_url'
actionpack (4.0.0) lib/action_dispatch/routing/url_for.rb:159:in `url_for'
actionpack (4.0.0) lib/action_controller/metal/redirecting.rb:100:in `_compute_redirect_to_location'
turbolinks (1.3.0) lib/turbolinks.rb:15:in `_compute_redirect_to_location_with_xhr_referer'
actionpack (4.0.0) lib/action_controller/metal/redirecting.rb:70:in `redirect_to'
actionpack (4.0.0) lib/action_controller/metal/flash.rb:40:in `redirect_to'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:61:in `block in redirect_to'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:60:in `redirect_to'
app/controllers/posts_controller.rb:11:in `create'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.0) lib/active_support/callbacks.rb:403:in `_run__506468238__process_action__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241: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.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__153816630__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) 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.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
4

2 回答 2

1

我想你想定义

resources :posts

而不是文件中的单一资源routes.rb。如果我没记错的话redirect_to,实际上是在尝试做post_path(id)不存在的事情。

于 2013-09-21T19:22:02.400 回答
0

有时查看别人的代码对我很有帮助,就像很多人在 github 上发布的那样。无论如何,所有rails库都在那里。为什么不看看。也许是 Michael Hartl 书的教程代码。https://github.com/mhartl/sample_app_3rd_edition 这样的事情对我有帮助。除了这个之外,github 上还有许多其他完整的 rails 应用程序。如果您还没有帐户,那么您应该获得一个帐户来存储您的教程应用程序,我们可以将其视为代码粘贴。

我相信这是我所获得的 Rails Guides 博客应用程序,尽管我已经有一段时间没有使用它了。https://github.com/DouglasAllen/Rails-projects/blob/master/rails_docs/blog/config/routes.rb

这就是 Rails 和一些 Ruby 错误的问题。你(我)并不总是确定它来自哪里。有时你只需要尝试不同的东西。不要放弃。保存您的代码并阅读其他文章。一个是官方的很好,但它不是最终的决定。数以百万计的其他用户也试图让事情为他们服务。至少您启用了日志记录,不是吗?我希望这就是您在此处收到错误帖子的地方。如果没有,去了解如何使用它。然后将其与您保存的应用程序一起发布。

另一个学习和编写代码的好地方是 Heroku 开发中心。如https://devcenter.heroku.com/articles/getting-started-with-rails4

玩得开心!

您可以使用但有点不安全的是环境变量。

在 routes.rb 中试试这个

root 'application#env_vars'

这在 application_controller.rb

def env_vars
  vars = request.env.each
  render plain: "env #{vars.each {|v| v}}"
end

几乎和追逐那些堆栈跟踪一样糟糕。

如果您曾经玩过 Sinatra,那么您会发现 Rails 的路线非常相似,因为这就是 Rack 的用途。

get '/vars' => 'application#env_vars'

只是一个 HTTP GET 请求。[URL]:[port]/map to controller-name#method or action as they call it。资源不仅包括控制器,还涉及模型。MVC 是 Rails 的方式。它使您可以通过生成器一次创建所有零件。但是现在火车真的使用交流发电机。;-)

于 2015-07-31T20:32:10.417 回答