Keluang Yang Ko Kenal
Keluang Yang Ko Kenal

Reputation: 86

NoMethodError while learning the tutorial

I'm a newbie in Ruby on Rails. I'm following the tutorial here Getting Started Ruby on Rails

Half way through creating posts, I stumbled on an error which is NoMethodError in PostsController#create in my controller. I'm confused right now.

Here's the code that I followed from the tutorial. Am I making a silly mistake here?

posts_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

my posts/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 %>

routes.rb

Blog::Application.routes.draw do

  resource :posts
  root to: "welcome#index"

end

models/post.rb

class Post < ActiveRecord::Base
end

and the stack trace

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'

Upvotes: 0

Views: 147

Answers (2)

Douglas G. Allen
Douglas G. Allen

Reputation: 2261

Sometimes it's helpful to me to look at somebody else's code like so many have posted on github. All the libraries for rails are on there anyway. Why not have a look. Maybe the tutorial code for the Michael Hartl book. https://github.com/mhartl/sample_app_3rd_edition Just things like that help me. There are many other full rails apps on github though besides this one. If you don't already have an account then you should get one to store your tutorial app on and we could just go look at it as if it were a code paste.

I believe this one is that Rails Guides blog app as far as I have gotten although I haven't worked with it for a while now. https://github.com/DouglasAllen/Rails-projects/blob/master/rails_docs/blog/config/routes.rb

That's the thing about Rails and some Ruby errors. You're (I'm) not always sure where it's coming from. Sometimes you just have to try different things. Don't give up. Save your code and go read some other articles. One that is official is fine but it's not the final word. Millions of other users are out there trying to make things work for them also. At least you have logging enabled don't you? I hope that is where you got your post for the error on here. If not, go find out how to use it. Then post it with your saved app.

Another great place to get some learning and some code is the Heroku Dev Center. Such as https://devcenter.heroku.com/articles/getting-started-with-rails4

Have fun!

Something you can play with but is a little insecure is environmental variables.

Try this in routes.rb

root 'application#env_vars'

and this in application_controller.rb

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

Almost as bad as chasing those stack traces.

If you have ever played around with Sinatra then you see that Rails routes are very similar because that's what Rack is for.

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

is just an HTTP GET request. [URL]:[port]/map to controller-name#method or action as they call it. Resources include more than just the controller as models are involved as well. MVC is the Rails way. It lets you create all the parts at one time through the generators. But trains really use alternators these days. ;-)

Upvotes: 0

Jakub K
Jakub K

Reputation: 1711

I think you want to define

resources :posts

instead of a singular resource in your routes.rb file. If I'm not mistaken the redirect_to is actually trying to do post_path(id) which doesn't exist.

Upvotes: 1

Related Questions