ruby on rails - NoMethod Error undefined method `first_name' for nil:NilClass -


running rails 4.1.8 | ruby 2.1.5p273

i'm trying display current username notes created current user using:

<%= "#{note.user.first_name.capitalize} #{note.user.last_name.capitalize[0]}" %> in show.html.erb.

<% @notes.each |note| %>         <tr>           <td>             <h4>              <%= "#{note.user.first_name.capitalize} #{note.user.last_name.capitalize[0]}" %>             </h4>             <p><%= note.created_at.strftime("%-m/%-d/%y") %></p>           </td>            <td>             <p><%= h(note.comment).gsub(/\n/, '<br/>').html_safe %></p> 

if take <%= "#{note.user.first_name.capitalize} #{note.user.last_name.capitalize[0]}" %> app works fine.

i checked notescontroller note , can't seem find issue.

class notescontroller < applicationcontroller   before_action :set_note, only: [:edit, :update, :destroy]   before_action :set_account   before_action :authenticate_user!   before_action :check_user, only: [:edit, :update, :destroy]    # /notes/new   def new     @note = note.new   end    # /notes/1/edit   def edit   end    # post /notes   # post /notes.json   def create     @note = note.new(note_params)     @note.user_id = current_user.id     @note.account_id = @account.id      respond_to |format|       if @note.save         format.html { redirect_to @account, notice: 'note created.' }         format.json { render :show, status: :created, location: @note }       else         format.html { render :new }         format.json { render json: @note.errors, status: :unprocessable_entity }       end     end   end    # patch/put /notes/1   # patch/put /notes/1.json   def update     respond_to |format|       if @note.update(note_params)         format.html { redirect_to account_path(@account), notice: 'note updated.' }         format.json { render :show, status: :ok, location: @note }       else         format.html { render :edit }         format.json { render json: @note.errors, status: :unprocessable_entity }       end     end   end    # delete /notes/1   # delete /notes/1.json   def destroy     @note.destroy     respond_to |format|       format.html { redirect_to account_path(@account), notice: 'note destroyed.' }       format.json { head :no_content }     end   end    private     # use callbacks share common setup or constraints between actions.     def set_note       @note = note.find(params[:id])     end      def set_account       @account = account.find(params[:account_id])     end       def check_user       unless (@note.user == current_user) || (current_user.admin?)         redirect_to root_url, alert: "sorry, note belongs else"       end     end      # never trust parameters scary internet, allow white list through.     def note_params       params.require(:note).permit(:comment)     end end 

i'm sure it's small i'm missing, can't see it.

you have note not have user associated it, or 1 of note users not have first_name or last_name (we had known if had posted error message getting). if want guard against this, have make sure user, first_name , last_name not nil before calling methods on them:

in model:

def display_user_name   if user.nil?     "no user"   else     "#{user.first_name.present? ? user.first_name : '<missing>'} "\     "#{user.last_name.present? ? user.last_name.capitalize[0] : '<missing>'}"   end end 

in template:

<%= note.display_user_name %> 

or, can use great andand gem, in template have:

"#{note.user.andand.first_name.andand.capitalize} #{note.user.andand.last_name.andand.capitalize[0]}" 

just note return " " (a string space in it) if note has not user or has no first name , no last name.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -