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 6173435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:34:22+00:00 2026-05-23T23:34:22+00:00

I just started working with rails, and I know it must be something simple

  • 0

I just started working with rails, and I know it must be something simple I’m doing wrong, but for the life of me I can’t figure it out.

I’m trying to define a simple method in my controller for a “post” called “shorten” that will return a shortened version of whatever string I pass to it. In my posts_controller.rb, I put the following;

def shorten(theString, length = 50)
if theString.length >= length
  shortened = theString[0, length]
else
  theString
end
end

Trying to call it from within my view gives me an undefined method error. I am calling it from within an instance of the post, so I assumed I didn’t need self.shorten. I went ahead and tried prepending self to the method definition anyway and it still didn’t work.

  • 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-05-23T23:34:22+00:00Added an answer on May 23, 2026 at 11:34 pm

    By default, methods defined within your controller are only available to your controller, not your view.

    The preferred way to handle this would be to move your method to your app/helpers/posts_helper.rb file, then it will work fine in your view.

    If you need to be able to access the method in both your controller and view though, you can just leave it defined in your controller and add a helper_method line:

    helper_method :shorten
    def shorten(theString, length = 50)
      if theString.length >= length
        shortened = theString[0, length]
      else
        theString
      end
    end
    

    Lastly, if you want to be able to apply this directly to a model, put it in your app/models/posts.rb file instead (without the helper_method line). However, I assume that rather than passing it a string, you’d just want to use one of your fields:

    def shorten(length = 50)
      if description.length >= length
        description[0, length]
      else
        description
      end
    end
    

    Then you can call it like this:

    <%= post.shorten %>
    

    However, Rails already has a truncate method build in that you could use instead:

    <%= truncate("My really long string", :length => 50) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started building a prototype application in Django. I started out by working
I just started working with assembly so this is probably a really simple question,
I just started working with Yaws to try to create some simple RESTful web
I just started working for a pretty large company and my group manages all
I've just started working on an ASP.NET project which I hope to open source
I've just started working with ASP.NET MVC now that it's in beta. In my
I'm just getting started working with foreign keys for the first time and I'm
I recently started working on a large complex application, and I've just been assigned
I just started thinking about creating/customizing a web crawler today, and know very little
I just started using Git and I want to know if this is the

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.