【问题标题】:Rails 3.2.7 and link_toRails 3.2.7 和 link_to
【发布时间】:2013-03-20 00:32:19
【问题描述】:

首先,对于 Rails 来说还是很陌生的。我一直在关注有关使用“link_to”命令的教程——基本上,我有一些带有“关于我们”、“常见问题”、“联系我们”的链接,我希望它们链接到各自的页面。

按照教程,我的contact_us.html.erb文件中的代码如下:

<%= link_to "About Us", {:controller => ‘static_pages’, :action => ’about_us’} %>

我的控制器名为 static_pages_controller.rb,我在该文件中有一个 about_us 方法,其中没有代码:

def about_us
end

我的控制器代码是:

class StaticPagesController < ApplicationController
  def about_us
  end

  def faq
  end

  def contact_us
  end

  def t_and_c
  end

  def t_and_c_competition
  end

  def show
  end
end

我得到错误:

Static_pages#contact_us 中的名称错误

未定义的局部变量或方法`'static_pages'......等

任何想法有什么问题吗?我认为这可能是因为本教程适用于 ruby​​ 1.8.6 和 Rails 2.0.2,而我有 Ruby 1.8.7 和 Rails 3.2.7。我听说 Rails 因不向后兼容而臭名昭著。我应该更改我的代码吗?到什么?感谢您的帮助。

C.

【问题讨论】:

  • 您好,已在上面添加。将
  • 你有为静态页面声明的路由吗?
  • 您的 Rails 版本已经过时并且容易受到许多安全漏洞的影响!立即升级!。此外,6 月份将不再支持 Ruby 1.8.7,您应该尽快切换到 Ruby 1.9 或 2.0。

标签: ruby-on-rails ruby link-to actioncontroller


【解决方案1】:

您好,我认为您的问题在于您在 link_to 方法中传递参数值时使用的是 而不是普通的单引号 (') 或双引号 (")

改变这个:

<%= link_to "About Us", {:controller => ‘static_pages’, :action => ’about_us’} %>

到这里:

<%= link_to "About Us", {:controller => 'static_pages', :action => 'about_us'} %>

【讨论】:

    【解决方案2】:

    我将此添加到我的 routes.rb 中:

    获取“static_pages/about_us”

    现在它可以工作了。感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多