Valerie Mettler
Valerie Mettler

Reputation: 485

Why is only part of my code not rendering in view

While I was working trying to code my views, I noticed that my code that was previously rendering on the index view is now only showing the first two lines of code on my local server, and I don't understand why.

Here is my index.html.erb code:

<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>

And here is my bookmark controller code:

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

Any thoughts?

Upvotes: 0

Views: 32

Answers (1)

Dmitry D.D.
Dmitry D.D.

Reputation: 46

It seems that your html is invalid. You are using div tags in rails loop, which is not being closed. The other thing is that non-table related html tags can be used only inside tags.

This might be the working solution.

<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>

Hope that helps.

Upvotes: 1

Related Questions