Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 循环中的多个gsub_Ruby On Rails_Ruby On Rails 3_Loops_Callback_Gsub - Fatal编程技术网

Ruby on rails 循环中的多个gsub

Ruby on rails 循环中的多个gsub,ruby-on-rails,ruby-on-rails-3,loops,callback,gsub,Ruby On Rails,Ruby On Rails 3,Loops,Callback,Gsub,我的文章中有一个小百科全书。rb: class Article < ActiveRecord::Base attr_accessible :name, :content end 但是很明显,引用content=content.gsub(etc…)行时出现了一个错误: ArticlesController更新中的名称错误 未定义的局部变量或方法“article”# 如何修复此问题,使其检查所有其他文章名称并为我要保存的当前文章创建链接?您的changelink方法不“知道”文章变量是什

我的文章中有一个小百科全书。rb:

class Article < ActiveRecord::Base
  attr_accessible :name, :content
end
但是很明显,引用content=content.gsub(etc…)行时出现了一个错误:

ArticlesController更新中的名称错误 未定义的局部变量或方法“article”#


如何修复此问题,使其检查所有其他文章名称并为我要保存的当前文章创建链接?

您的changelink方法不“知道”文章变量是什么。您必须将其作为参数传递:

  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
def createlinks
@allarticles=Article.all
@所有物品。每件物品|
self.content=changelinks(self.content,文章)
结束
结束
def变更链接(内容、文章)
content=content.gsub(/#{article.name}/,“”)
结束

但在我看来,这种实现链接而不是文章名称的方式并不是最好的。

谢谢。错误现在消失了。总之内容没有改变,似乎gsub没有改变文章中创建的链接。我试图找出原因……事实上,在我将gsub合并到如下循环中后,它起了作用:self.content.gsub!(/#{article.name}/,“”)
def update
  @article = Article.find(params[:id])
  if @article.update_attributes(params[:article])
    redirect_to admin_path
  else
    render 'edit'
  end
end
  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