请帮助解决问题。
在模板中有一段代码。我需要将它包含在链接中。我试着这样做:
<%= link_to(show_news_path) do %>
<div>
<span>title:</span>
</div>
<% end %>
但是我收到以下错误消息:
NameError in News#index
Showing /home/kalinin/rails/visit/app/views/news/index.html.erb where line #4 raised:
undefined local variable or method `show_news_path' for #<#<Class:0x000000022303f0>:0x007fb20405b0a0>
新闻控制员:
class NewsController < ApplicationController
def index
@news = News.all.order(created_at: :desc)
end
def show
end
end
答案 0 :(得分:0)
假设您的路由设置正确,如果您想要进行索引操作,则需要news_path
。如果你想进行show动作,你需要new_path
,并且你想要给它一个新闻对象,这样它就可以正确地为你构建网址(并且可能填写NewsController#show
实际做某事的行动。)
答案 1 :(得分:0)
我假设您正在尝试链接到show动作,就像这样:
news_path
如果您尝试链接到索引操作,它应该是这样的:
news_index_path
如果这些不起作用,请在命令行中键入“rake routes”以获取可用路由列表。