Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8370963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:02:34+00:00 2026-06-09T14:02:34+00:00

I’m having trouble getting my view to render. I think I know why I’m

  • 0

I’m having trouble getting my view to render. I think I know why I’m getting the error:

undefined method `name' for nil:NilClass

Extracted source (around line #21): (showing 18-24)

<ul>
     <%= form_tag(default_hero_user_path, :method=>'post') do %>
         <%= label_tag "Name" %>
         <%= text_field_tag "name", @user.default_hero.name %> #line 21
         <%= submit_tag 'Set hero', class: "btn btn-large btn-primary"  %>
     <% end %>
   </ul>

I know you can’t call .name on a nil object but I don’t understand why @user.default_hero is nil. I want the user to be able to set their ‘hero’ but having trouble setting the default obviously.

Here is the users controller:

# creates or updates the default hero
def default_hero
 @user = User.find(params[:id])
 hero = @user.default_hero
 if hero.nil?
   # we don't have a default hero so we need to add one'
   hero = Hero.new
   @user.heros << hero
 end
 hero.default = true
 hero.name = params[:name]
 hero.save
 redirect_to @user # shows the user again to see any updates
end

And here is where I believe I am having the problem in setting a default view-

def show
@user = User.find(params[:id]) 
if @user.default_hero.nil?
  name = params[:q]
else
  name = @user.default_hero.name
end

Thanks for your guys’ time and attention if you can point me in the right direction in how to solve this I would ppreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:02:36+00:00Added an answer on June 9, 2026 at 2:02 pm

    Your show controller action doesn’t make much sense. You are setting a local variable (which won’t be accessible in the view) if @user.default_hero.nil?. This doesn’t accomplish anything and @user.default_hero will still be nil in the view.

    That being said, to get rid of your error you can simply do this:

    <%= text_field_tag "name", @user.default_hero.nil? ? '' : @user.default_hero.name %>
    

    Based on your comment I would does something like this:

    Model

    class User < ActiveRecord::Base
      has_many :heros
    
      def set_default_hero(name)
        hero = self.heros.find_by_name(name) # check if the hero exists already
        hero = self.heros.build(:name => name) if hero.nil? # new object if not
        hero.default = true # set it as default
        hero.save
      end
    end
    

    Controller:

    def default_hero
      @user = User.find(params[:id])
      @user.set_default_hero(params[:name])
      redirect_to @user
    end
    
    def show
      @user = User.find(params[:id])
      @default_hero = @user.default_hero.nil? ? '' : @user.default_hero.name
    end
    

    View:

    <%= text_field_tag "name", @default_hero %>
    

    You ignored my question about params[:q], so I don’t know what to do with that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a view passing on information from a database: def serve_article(request, id): served_article
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.