I am pretty new to coding, so I decided to start the Ruby on Rails guide for version 4.0.0 and have had problem after problem. I am currently running version 4.0.0 and I have followed the guide step by step.
Once I got to 5.2 First form I began to get errors and was using other people's posts to solve my questions, but this error doesn't seem to happen for anyone else, so here it is:
NoMethodError in PostsController#index
undefined method `action' for PostsController(Table doesn't exist):Class
Here is my code:
class PostsController < ActiveRecord::Base
attr_accessible :title, :text
def new
end
def create
@post = Post.new(params[:post].permit(:title, :text))
@post.save
redirect_to @post
end
def show
@post = Post.find(params[:id])
end
def index
@post = Post.all
end
def action
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Here is my view:
<p>
<strong> Title: </strong>
<%= @post.title %>
</p>
<p>
<strong> Text: </strong>
<%= @post.text %>
</p>
My form is:
<h1> New Post </h1>
<%= form_for :posts, 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 %>
The error states that I don't have an action method defined, but I'm not sure how to define one in the first place, however this is not all. When I go to my localhost:3000/posts/new page I get the same error but for PostsController#new
Can someone please help me!!
A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .
This is a common Ruby error which indicates that the method or attribute for an object you are trying to call on an object has not been defined.
The standard clearly defines that you will receive undefined when accessing uninitialized variables, non-existing object properties, non-existing array elements, and alike. The above example demonstrates that accessing: a non-existing object property movie.year are evaluated to undefined.
The short answer is that JavaScript interpreter returns undefined when accessing a variable or object property that is not yet initialized. For example: On the other side, null represents a missing object reference.
The ECMAScript specification defines the type of undefined value: Undefined type is a type whose sole value is the undefined value. In this sense, typeof operator returns 'undefined' string for an undefined value: typeof undefined === 'undefined';
For instance, you need to access the properties of unsafeOptions object that doesn’t always contain its full set of properties. To avoid undefined when accessing a non-existing property from unsafeOptions, let’s make some adjustments: Call Object.assign ( { }, defaults, unsafeOptions) to build a new object options.
It's not clear why your controller is inheriting from a model class:
class PostsController < ApplicationController
# ...
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With