当我正在尝试编写我的视图代码时,我注意到我之前在索引视图上呈现的代码现在只显示了本地服务器上的前两行代码,我不明白为什么。
这是我的index.html.erb代码:
<h1>All Bookmarks</h1>
<%= link_to 'Create a New Bookmark', new_bookmark_path %>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<div class="row">
<div class="col-md-8">
<tbody>
<% @bookmarks.each do |bookmark| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<tr>
<td><%= link_to bookmark.url, "http://#{bookmark.url}" %></td>
<td><%= link_to 'Show', bookmark %></td>
<td><%= link_to 'Edit', edit_bookmark_path(bookmark) %></td>
<td><%= link_to 'Destroy', bookmark, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
这是我的书签控制器代码:
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
有什么想法?
答案 0 :(得分:1)
您的HTML似乎无效。您在rails循环中使用div标记,该标记未被关闭。另一件事是非表格相关的html标签只能在标签内使用。
这可能是有效的解决方案。
<h1>All Bookmarks</h1>
<%= link_to 'Create a New Bookmark', new_bookmark_path %>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @bookmarks.each do |bookmark| %>
<tr>
<td><%= link_to bookmark.url, "http://#{bookmark.url}" %></td>
<td><%= link_to 'Show', bookmark %></td>
<td><%= link_to 'Edit', edit_bookmark_path(bookmark) %></td>
<td><%= link_to 'Destroy', bookmark, method: :delete, data: { confirm: 'Are you sure?' } %> </td>
</tr>
<% end %>
</tbody>
</table>
希望有所帮助。