How to insert a custom logo into a bootstrap navbar on a Ruby on Rails website

I have encountered a similar issue to this, but I haven't been able to resolve it. I am currently working on my first application using rails and bootstrap. I've successfully created a navbar with a name and two buttons (you can view the screenshot here). However, I am facing difficulty adding a logo to the left of the "navbar." The image is not loading as expected when I run the code locally.

What I have attempted:

Initially, I tried directly referencing the path of the image in my code, but that didn't work.

Next, I added the path to my logo image in app/assets/images as logo.png and then used the following code:

   <nav class="navbar navbar-light bg-light">
      <a class="navbar-brand" href="http://localhost:3000/posts">
        <%= image_tag 'logo.png' %>
        Navbar
      </a>
      <form class="form-inline">
          <button type="button" class="btn btn-light">Sign Up!</button>
        <div style="padding-left: 10px;">
          <button type="button" class="btn btn-dark">Login</button>
        </div>
      </form>
   </nav>

If anyone could offer guidance or assistance, I would be extremely grateful!

Thank you in advance.

Answer №1

I tried out your code and it seems to be working perfectly: check out the screenshot

I placed the logo.png in the app/assets/images/ directory and used the exact same code you provided in application.html.erb. Make sure the image name is correct and that it is a valid file.

Another option:

If you're open to using the asset pipeline, you could simply create a public/images folder and load the image with something like this:

<%= image_tag '/images/logo.png' %>

Answer №2

If you want to include a navigation bar in your website, consider using link_to:

<nav class="navbar navbar-light bg-light">
  <%= link_to 'Navbar', '/posts', :class=>'navbar-brand' do %> 
    <%= image_tag 'logo.png' %>
  <% end %>
  <form class="form-inline">
    <button type="button" class="btn btn-light">Sign Up!</button>
    <div style="padding-left: 10px;">
      <button type="button" class="btn btn-dark">Login</button>
    </div>
  </form>
</nav>

Remember not to add the full path like https://localhost:3000 when specifying the route.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prevent the onclick function of a span element from being triggered when the user clicks on a dropdown menu contained within

Just delving into the world of web development and currently tackling a React project. Encountered an issue where I need to make a span element clickable. However, there's a dropdown within that span that doesn't have any onClick event attached t ...

Only display one div element when in print mode, hiding all others

My print style CSS code includes the following: * { display:none; } #printableArea { display:block; } Initially, I anticipated that this CSS would hide all elements except for the printableArea. Surprisingly, everything on the page became hidden when ...

Determining the margin-left value of a centrally positioned table

I have a table with variable columns, meaning the columns of the table are dynamic. The number of columns will keep changing, causing the table to be centralized and the margin-left value to adjust accordingly. Now, I need to calculate the margin-left val ...

Using Jquery to toggle the visibility of div elements with the same structure, but ensuring only one is

Many people have posed this question. I am attempting to create a functionality where a div can expand and collapse using a +/- button. I have 5 divs with class=="expanderContent". When the function below is triggered by a click, all 5 items expand or coll ...

Alignment of slider arrows in a CSS carousel with Bootstrap 4.1.1

Is there a way to position the carousel control arrows at the left and right ends of the screen in Bootstrap 4.1.1? I attempted the code below but it didn't yield the desired result. .carousel-control .icon-prev{ left: 5px } .carousel-control .icon ...

How to dynamically adjust font size using Javascript

Is there a way to adjust the font size of the left-column using JavaScript? <div id="left-column"> <a href="">1</a><br> <a href="">2</a><br> <a href="">3</a><br> <a href=""> ...

Tips for adjusting the width of v-text-field in Vuetify?

I have a v-text-field component within a toolbar that is taking up too much space. How can I adjust the width of the text-field to make it smaller? <v-toolbar dense > <v-app-bar-nav-icon></v-app-bar-nav-icon> <v-toolba ...

Retrieve the chosen option from a Select Dropdown and display it in a modal before carrying out the form submission

I need help figuring out how to send a warning message when a "delete" button is clicked on my webpage. I want the message to appear in a modal and display the selected value from a dropdown menu. Here is the code I have so far: <form action="elim ...

The attribute 'checked' is not a valid property for the 'TElement' type

Learning about typescript is new to me. I have a functional prototype in fiddle, where there are no errors if I use this code. http://jsfiddle.net/61ufvtpj/2/ But in typescript, when I utilize this line - if(this.checked){ it presents an error [ts] Pro ...

Unresponsive Bootstrap Navigation Bar

I have been struggling to integrate Bootstrap into my Django project. The issue I'm facing is that the navbar button does not expand when clicked. I have a suspicion that the problem lies with how Django manages templates, possibly causing Jquery to b ...

I seem to be encountering an issue with the image tag while working with HTML code

I have been studying through freecodecamp.org and this is my first tribute project. Everything has been going well, but I am encountering an issue with the image tag. I need to submit my project online, so I have been trying to input image links from Googl ...

Using HTML and CSS to stack a DIV on top of another using z-index

I have 3 main layers on my website: 1) The main view with elements inside (#views in jsbin) - BOTTOM LAYER 2) An overlay (with a white background opacity of .8 #overlay in jsbin) - MIDDLE LAYER 3) A context menu (#contextmenu in jsbin) - TOP LAYER Wh ...

Before the <table> tag, web browsers display a mysterious Phantom <br> tag

Why is the browser inserting a <br> tag before my <table> element? The page is meant to be displayed within one table, but an unwanted whitespace is being created by this tag that is not included in the code. The code appears like this, even w ...

The text consistently remains positioned to the right of my image

Photo Gallery Title Issue I'm working on a photo gallery project where I want to add titles underneath each image. However, I'm facing a problem where the title is appearing next to the image instead of below it. You can see the issue in this sc ...

In CSS3, utilize the calc() function to vertically center elements using viewport height (vh) and viewport

One of the most common cases involves using vh for setting the height of a div, and using vm for adjusting font size. Using CSS3 div.outer { height: 20vh; } div.inner { font-size: 3vw; height: auto; } div.inner2 { font-size: 2vw; } HTML - Scenario 1 <d ...

a guide on incorporating jQuery ajax requests with node.js

Although I came across a similar question on Stream data with Node.js, I felt the answers provided were not sufficient. My goal is to transfer data between a webpage and a node.js server using a jQuery ajax call (get, load, getJSON). When I try to do this ...

Include information from various models in a JSON object

Implementing a Like function in my rails application is my current goal. At the moment, I have three models: Question, Answer, and Like. The Question model contains details such as user_id and question_text, while the Answer model includes user_id and ans ...

What is the optimal way for text selection to operate within a clickable component on a web application?

We're currently developing a web application with expandable/collapsible elements. Here's an example of one of the clickable items: https://i.stack.imgur.com/jdLOu.png The text selection behavior when clicking on the element is causing some an ...

Leveraging django model information within a bootstrap layout

Struggling with integrating Django model data into a Bootstrap 5 collapse function for an expanding list. After taking a long coding break, I'm finding it challenging to make the collapse function work with the data from Django. It works when using a ...

What is the best way to trigger form submission when the value of an input element changes?

I am currently developing a website using Bootstrap and I am exploring the idea of incorporating a toggle button to alter the color scheme by simply toggling it. I have successfully created a toggle button similar to a checkbox, but now I am looking for ...