ruby on rails - Undefined Method 'underscore' -


i'm trying hand @ sti using thibault's tutorial: https://samurails.com/tutorial/single-table-inheritance-with-rails-4-part-3

it's been working fine till dynamic paths part 'undefined method `underscore' nil:nilclass' snippet

def format_sti(action, type, post)     action || post ? "#{format_action(action)}#{type.underscore}" : "#{type.underscore.pluralize}" end 

routes:

    resources :blogs, controller: 'posts', type: 'blog'            resources :comments, except: [:index, :show]     end     resources :videos, controller: 'posts', type: 'video'     resources :posts 

post controller:

    before_action :set_post, only: [:show, :edit, :update, :destroy]     before_action :set_type     def index         @posts = type_class.all     end     ...     private       def set_type         @type = type     end      def type         post.types.include?(params[:type]) ? params[:type] : "post"     end      def type_class         type.constantize     end      def set_post         @post = type_class.find(params[:id])     end 

posts helper:

    def sti_post_path(type = "post", post = nil, action = nil)         send "#{format_sti(action, type, post)}_path", post      end      def format_sti(action, type, post)         action || post ? "#{format_action(action)}#{type.underscore}" : "#{type.underscore.pluralize}"     end      def format_action(action)         action ? "#{action}_" : ""     end 

post index.html

    <% @posts.each |p| %>      <h2><%= p.title %></h2>     created at: <%= p.created_at %><br>     created by: <%= p.user.name %><p>     <%= link_to 'details', sti_post_path(p.type, p) %><p>     <% end %> 

the error appears when try access index.html, , haven't tried other links yet. i've tried removing 'underscore', '_path' becomes undefined method. i've tried other suggestions such 'gsub', shows undefined method, leads me think it's syntax error...

update: had attr_accessor :type , made 'type' nil. removed , working

in postshelper.rb

def format_sti(action, type, post)     action || post ? "#{format_action(action)}#{type.underscore}" : "#{type.underscore.pluralize}" end 

this method's else portion has type.underscore. type may nil here. verify it, try this:

def format_sti(action, type, post)     action || post ? "#{format_action(action)}#{type.underscore}" : "#{"post".underscore.pluralize}" end 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -