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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:03:02+00:00 2026-06-17T22:03:02+00:00

I have a small encyclopedia of articles with my Article.rb: class Article < ActiveRecord::Base

  • 0

I have a small encyclopedia of articles with my Article.rb:

class Article < ActiveRecord::Base
  attr_accessible :name, :content
end

I now want to automatically link within the articles if I find text in one article that corrisponds to the name of another article. E.g. in the article named “Example One” the content is “You can also check Example Two for further reading.” On save of “Example One” I want to set a link to the article “Example Two”. My approach is to add to Article.rb

class Article < ActiveRecord::Base
  attr_accessible :name, :content

  before_save :createlinks

  def createlinks
    @allarticles = Article.all
    @allarticles.each do |article|
      self.content = changelinks(self.content)
    end
  end

  def changelinks(content)
    content = content.gsub(/#{article.name}/, "<%= link_to '#{article.name}', article_path(article) %>")
  end

My articles_controller is:

def update
  @article = Article.find(params[:id])
  if @article.update_attributes(params[:article])
    redirect_to admin_path
  else
    render 'edit'
  end
end

But obviously there is an error refering to the line content = content.gsub(etc…):

NameError in ArticlesController#update
undefined local variable or method `article’ for #

How can I fix this so that it checks all other article names and creates the links for the current article I want to save?

  • 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-17T22:03:04+00:00Added an answer on June 17, 2026 at 10:03 pm

    Your changelink method does not “know” what is the article variable. You have to pass it as an argument:

      def createlinks
        @allarticles = Article.all
        @allarticles.each do |article|
          self.content = changelinks(self.content, article)
        end
      end
    
      def changelinks(content, article)
        content = content.gsub(/#{article.name}/, "<%= link_to '#{article.name}', article_path(article) %>")
      end
    

    But this way to implement links instead of the Articles’ name is not the best in my opinion.

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

Sidebar

Related Questions

I have small image hosting and I realized there many duplicate content. I want
I have small class called 'Call' and I need to store these calls into
I have small problem with displaying cyrillic app name on PlayBook and BB10 simulator.
I have small TcpClient app to connect to a server using SSL (SSlStream class)
I have small view with a UITextField in it. I want to make the
I have small android app. I made APK file from phonegap.com. Now I tried
I have small problem. I learn java SE and find class ClassLoader. I try
I have small problem with mysql. I have data in table and I want
I have small problem with name spaces in php 5.3. I've made declaration for
I have small container with the users name aligned all the way to 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.