我是编程的新手,我想知道如何在两个不同的视图中使用相同的代码,并且一个视图在浏览器中呈现而没有和错误,另一个视图在两个不同的视图中抛出错误浏览器。以下是<li><%= link_to "##{topic.name}", topic %></li>
抛出的浏览器错误消息:
Showing /home/vagrant/code/bookmarks/app/views/user_bookmarks/index.html.erb where line #5 raised:
undefined method `model_name' for #<Class:0xad3a8d8>
Extracted source (around line #5):
<ul class="topics">
<% @topics.each do |topic| %>
<li><%= link_to "##{topic.name}", topic %></li>
<ul class="bookmarks">
<%= render topic.bookmarks %>
</ul>
app/views/user_bookmarks/index.html.erb:5:in `block in _app_views_user_bookmarks_index_html_erb__225534477__624396848'
app/views/user_bookmarks/index.html.erb:4:in `each'
app/views/user_bookmarks/index.html.erb:4:in `_app_views_user_bookmarks_index_html_erb__225534477__624396848'
这是我的主题索引视图代码:
<h1>All Bookmarks</h1>
<ul class="topics">
<% @topics.each do |topic| %>
<li><%= link_to "##{topic.name}", topic %></li>
<ul class="bookmarks">
<%= render topic.bookmarks %>
</ul>
<% end %>
</ul>
<br>
这是我的用户书签索引视图代码的副本:
<h1>My Bookmarks</h1>
<ul class="topics">
<% @topics.each do |topic| %>
<li><%= link_to "##{topic.name}", topic %></li>
<ul class="bookmarks">
<%= render topic.bookmarks %>
</ul>
<% end %>
</ul>
<br>
编辑:在此处添加控制器代码:
这是我的主题控制器:
class TopicsController < ApplicationController
before_action :set_topic, only: [:show, :edit, :update, :destroy]
def index
@topics = Topic.all
end
def show
@bookmarks = @topic.bookmarks
end
def new
@topic = Topic.new
end
def edit
end
def create
@topic = Topic.new(topic_params)
respond_to do |format|
if @topic.save
format.html { redirect_to @topic, notice: 'Topic was successfully created.' }
format.json { render action: 'show', status: :created, location: @topic }
else
format.html { render action: 'new' }
format.json { render json: @topic.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @topic.update(topic_params)
format.html { redirect_to @topic, notice: 'Topic was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @topic.errors, status: :unprocessable_entity }
end
end
end
def destroy
@topic.destroy
respond_to do |format|
format.html { redirect_to topics_url }
format.json { head :no_content }
end
end
private
def set_topic
@topic = Topic.find(params[:id])
end
def topic_params
params.require(:topic).permit(:name)
end
end
这是我的书签控制器:
class BookmarksController < ApplicationController
before_action :set_bookmark, only: [:show, :edit, :update, :destroy]
def index
@bookmarks = Bookmark.all
end
def show
end
def new
@bookmark = Bookmark.new
end
def edit
end
def create
bookmark = Bookmark.where(url: params[:bookmark][:url]).first
@bookmark = bookmark.present? ? bookmark : Bookmark.new(bookmark_params)
if @bookmark.save
@bookmark.users << current_user
Rails.logger.info ">>>>>>>>>>>>> Bookmark: #{@bookmark.inspect}"
topic_names = params[:topic_names].split(' ')
topic_names.each do |topic_name|
name = topic_name.sub(/#/, '')
@bookmark.topics << Topic.find_or_create_by_name(name)
end
respond_to do |format|
format.html { redirect_to @bookmark, notice: 'Bookmark was successfully created.' }
format.json { render action: 'show', status: :created, location: @bookmark }
end
else
respond_to do |format|
format.html { render action: 'new' }
format.json { render json: @bookmark.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @bookmark.update(bookmark_params)
format.html { redirect_to @bookmark, notice: 'Bookmark was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @bookmark.errors, status: :unprocessable_entity }
end
end
end
def destroy
@bookmark.destroy
respond_to do |format|
format.html { redirect_to bookmarks_url }
format.json { head :no_content }
end
end
private
def set_bookmark
@bookmark = Bookmark.find(params[:id])
end
def bookmark_params
params.require(:bookmark).permit(:url)
end
end
这是我的传入(书签和主题)控制器:
class BookmarksController < ApplicationController
before_action :set_bookmark, only: [:show, :edit, :update, :destroy]
def index
@bookmarks = Bookmark.all
end
def show
end
def new
@bookmark = Bookmark.new
end
def edit
end
def create
bookmark = Bookmark.where(url: params[:bookmark][:url]).first
@bookmark = bookmark.present? ? bookmark : Bookmark.new(bookmark_params)
if @bookmark.save
@bookmark.users << current_user
Rails.logger.info ">>>>>>>>>>>>> Bookmark: #{@bookmark.inspect}"
topic_names = params[:topic_names].split(' ')
topic_names.each do |topic_name|
name = topic_name.sub(/#/, '')
@bookmark.topics << Topic.find_or_create_by_name(name)
end
respond_to do |format|
format.html { redirect_to @bookmark, notice: 'Bookmark was successfully created.' }
format.json { render action: 'show', status: :created, location: @bookmark }
end
else
respond_to do |format|
format.html { render action: 'new' }
format.json { render json: @bookmark.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @bookmark.update(bookmark_params)
format.html { redirect_to @bookmark, notice: 'Bookmark was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @bookmark.errors, status: :unprocessable_entity }
end
end
end
def destroy
@bookmark.destroy
respond_to do |format|
format.html { redirect_to bookmarks_url }
format.json { head :no_content }
end
end
private
def set_bookmark
@bookmark = Bookmark.find(params[:id])
end
def bookmark_params
params.require(:bookmark).permit(:url)
end
end
我的用户书签控制器:
class UserBookmarksController < ApplicationController
def index
@bookmarks = current_user.bookmarks
#@topics = @bookmarks.collect(&:topics).uniq
@liked_bookmarks = current_user.likes.collect(&:bookmark)
@liked_topics = @liked_bookmarks.collect(&:topic).uniq
end
end
如果你能帮助我理解为什么会这样,我真的很感激!谢谢!
答案 0 :(得分:1)
您可能会因调用<%= link_to "text", topic %>
<强>对象强>
这是一个问题的原因是因为当您调用link_to
helper时,需要使用路径填充它,以便Rails为您创建链接。虽然您的语法是正确的,但我相信您使用topic
对象会引发错误。
有两种方法可以解决:
- 更改您的
link_to
路径以直接引用路径助手- 修改您的
醇>topic
对象以确保其正确
底线是当Rails使用&#34;帮助&#34;方法(其中link_to
绝对是一个),它将依赖于对象中的大部分metadata
来为您的系统提供功能。
model_name
属性只是您的对象可以拥有的众多属性之一。我将看到的解决方案将解决缺少model_name
:
链接强>
&#34;快速修复&#34;将直接在link_to中使用路径助手:
<%= link_to "##{topic.name}", topic_path(topic.id) %>
这只是语法改进,并认为您已正确定义topic_path
路由:
#config/routes.rb
resources :topics #-> domain.com/topics/:id
这更像是一种外观变化 - 我相信真正的问题出现在你的对象声明中。
<强>声明强>
从topic
调用您的@topics
对象。从@topics
操作调用TopicsController#index
:
#app/controllers/topics_controller.rb
class TopicsController < ApplicationController
def index
@topics = Topic.all
end
end
出于某种原因,这可能会导致Rails出现问题。我的直接想法是,您的Topic
模型将无法正确设置,从而阻止Rails正确填充model_name
属性
要解决此问题,我会确保您的Topic
模型首先连接到您的数据库,其次是您修复任何关联。
答案 1 :(得分:0)
我发现我收到了错误,因为没有直接的关系。改变这行代码:
@topics = @bookmarks.collect(&:topic).uniq
到这行代码工作:
@topics = @bookmarks.collect(&:topics).flatten.uniq
以下是我最终的工作代码在我的控制器中的样子:
class UserBookmarksController < ApplicationController
def index
@bookmarks = current_user.bookmarks
@topics = @bookmarks.collect(&:topics).flatten.uniq
@liked_bookmarks = current_user.likes.collect(&:bookmark)
@liked_topics = @liked_bookmarks.collect(&:topic).uniq
end
end