Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get undefined method `action' for PostsController(Table doesn't exist)?

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!!

like image 861
Therafer Avatar asked Aug 22 '13 18:08

Therafer


People also ask

What is an undefined method?

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 .

What does undefined method mean in Ruby?

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.

When do you get an undefined error when accessing an object?

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.

Why does JavaScript return undefined when a variable is not initialized?

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.

How to return an undefined string for an undefined value?

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';

How to avoid undefined when accessing a non-existing property from unsafeoptions?

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.


1 Answers

It's not clear why your controller is inheriting from a model class:

class PostsController < ApplicationController
  # ...
end
like image 73
tadman Avatar answered Sep 24 '22 06:09

tadman