Sunday, October 20, 2013

Re: [Rails] Rails 4 - I got NoMethodError undefined method. Please help

On Oct 18, 2013, at 10:17 PM, Jim <koyca2002@gmail.com> wrote:

> I got a NoMethodError when i called <%= @post.admin_user.name %>, don't know how to fix it.
>
>
>
> Extracted source (around line #4):
>
> <h1><%= @post.title %></h1>
> <p><%= @post.body %></p>
> <small>Post created at <%= @post.created_at.strftime('%b %d. %Y') %></small><br/><br/>
> <%= @post.admin_user.name %>

If this is line #5 (where the error was reported below), then admin_user is nil.

> <p>Category: <%= link_to @post.category.name, category_path(@post.category.id) %></p>

If this is line #5, then category is nil.

If this were me, I'd poke in the rails console and see what the record settings are, why either of those might be nil, and where they were supposed to be set for the post record.

> <%= link_to 'Edit Post', edit_post_path %> | <%= link_to 'Go Back', posts_path %> | <%= link_to 'Delete Post', @post, :confirm => "Don't do it man!", :method => :delete %>
> </div>
>
>
>
> Showing c:/Sites/blog/app/views/posts/show.html.erb where line #5 raised:
>
> undefined method `name' for nil:NilClass
>
>
> ---------------
>
> This is my posts_controller.rb
>
>
>
> class PostsController < ApplicationController
> def index
> @post = Post.all
>
>
> end
>
> def new
> @post = Post.new
> @category = Category.all
> end
>
>
> def create
> @post = Post.new(post_params)
> if @post.save
> redirect_to posts_path, :notice => 'Your post has been posted!'
> else
> render 'new'
> end
> end
>
>
> def post_params
> params.require(:post).permit(:title, :body, :category_id, :admin_user_id, :admin_user, :name)
> end
>
>
> def edit
> @post = Post.find(params[:id])
> end
>
> def update
> @post = Post.find(params[:id])
> if @post.update_attributes(post_params)
> redirect_to post_path, :notice => 'Your post has been updated.'
> else
> render 'new'
> end
> end
>
> def show
> @post = Post.find(params[:id])
> @user = AdminUser.all
> end
>
> def destroy
> @post = Post.find(params[:id])
> @post.destroy
> redirect_to posts_path, :notice => 'Your post has been deleted.'
> end
>
>
>
>
> end
>
>
>
>
>
>
>
>
> This is my post.rb
>
> class Post < ActiveRecord::Base
>
> belongs_to :category
>
> belongs_to :admin_user
>
>
> end
>
> My admin_user.rd
>
> class AdminUser < ActiveRecord::Base
> # Include default devise modules. Others available are:
> # :confirmable, :lockable, :timeoutable and :omniauthable
> devise :database_authenticatable,
> :recoverable, :rememberable, :trackable, :validatable
>
> has_many :posts
>
>
> end
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/eefca17e-5fad-4f56-bc5e-531551c5cfdc%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1C6AC6A7-9540-4847-A9DC-10A9EFE2AAA2%40gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate