Nomethoderror on Section 5.7 of Rails Guide

NoMethodError on section 5.7 of Rails Guide

Just write down show method after create method, as your show method is below the keyword private it is taking private as a Access Modifier and hence can't access directly through browser

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

Rails getting started 5.7

What you've depicted is the show member for the posts resource routes. It's not actually code, but rather, a pattern for URL routing. You can see all your routes in this fashion by typing rake routes from the command line.

Breaking down the route:

post GET /posts/:id(.:format) posts#show
# `post` => named route name (available by default only to singular routes)
# `GET` => HTTP method
# `/posts/:id(.:format)` => path made accessible by route
# :id => specifies that the argument passed in as `:id` is available to the controller as `params[:id]`
# `posts#show` => controller is `posts`, action is `show`

You need to create a corresponding show controller action that the route will map to:

# app/controllers/posts_controller.rb
def show
@post = Post.find(params[:id])
end

NoMethodError undefined method ` ' for nil:NilClass

Your show action is private, therefore the instance variable @post cannot be used in the view. Move it to a public scope and the problem should be fixed.

Getting Started with Rails Tutorial 5.7: Article object is not being created correctly

Based on this, from your comment, #<Article id: nil, created_at: nil, updated_at: nil>" the problem is that when you created your Article model, you didn't add the title and text columns. Try this:

rails g migration add_title_to_articles title:string text:text
rake db:migrate

That should add a title attribute to your articles model and you should be good to go.

After reading section 5.4 of the guide, I suspect that you forgot a little part of the generate model command and are missing those fields. Instead of this:

rails generate model Article title:string text:text

You may have done this:

rails generate model Article

This would have generated just an :id, :created_at, and :updated_at

http://guides.rubyonrails.org/getting_started.html#creating-the-article-model

#NoMethodError: undefined method `permit' for \derp\:String,

In this scenario the top-level key store is expected to have an attribute on it named store also. Try passing { "store": { "store": "derp" } } in your POST request, and see if that works.

Here is some useful documentation on Rails params that might help: https://api.rubyonrails.org/classes/ActionController/Parameters.html

Getting Started With Rails Tutorial: 5.7 Showing posts -- No Forbidden Attributes Error

The documentation is actually misleading, you're right.

If you coded your controller as shown in chapter 5.6

def create
@post = Post.new(post_params)

@post.save
redirect_to @post
end

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

you're already permitting the use of the parameters title and text.

The next chapter (5.7) assumes you didn't use the permit-method already.

If you'd change Line 2 to:

 @post = Post.new(post_params)

as seen in the screenshot, the error will be thrown. Additionally, the 'fix' in chapter 5.7 doesn't define a new private method post_params as you did, but applies the fix inline.

@post = Post.new(params[:post].permit(:title, :text))

NoMethodError in PostsController#create following the getting started guide

In your routes.rb I see you've

resource :posts

I believe, it should be:

resources :posts

How/Where to add action

It is:

post GET    /posts/:id(.:format)      posts#show

piece of output rake routes. In order the rule to work, you should add the action show as def show method into Posts controller, then add app/views/posts/show.html.erb view file (if you use ERubies template renderer).



Related Topics



Leave a reply



Submit