Search code examples
ruby-on-rails

undefined method `humanize' for nil:NilClass


I know i should put the if statement but how do i have to put. I am new to rails. Can someone figure it out for me ?

 <div class="row">
      <div class="col-md-12 col-sm-12">
        <div class="col-md-6 col-sm-6">
          <h4><i><%[email protected]_s + " " %></i>
            <%= params[:state].humanize.pluralize + " on"%><i><%= @title %></i></h4>
        </div>
        <%= render "admin/report/appointments/filter_form.html.erb" %>
      </div>

Solution

  • please use try block instead of if condition , you can find out more about try from here https://apidock.com/rails/v3.2.1/Object/try

    rails 5

    params[:state]&.humanize&.pluralize
    

    rails 4

    params[:state].try(:humanize).try(:pluralize)
    

    if you don't want to use try and just want to use if you can use ternary condition

    <h4><i><%[email protected]_s + " " %></i> <%= params[:state].nil? ? params[:state] : params[:state].humanize.pluralize "on" %>
    

    But try block will do same work of your if condition