_cart.html.erb部分未在导航栏中显示

时间:2016-07-04 12:13:17

标签: css ruby-on-rails twitter-bootstrap navbar

我在铁轨上的E商店工作。我在部分_navbar中有<%= render 'cart' %>部分。问题是cart部分没有出现在navbar中,我真的无法弄清楚为什么?我没有在代码中看到任何应该让它消失的东西。

这是我的_navbar.html.erb部分

  <div class="navbar navbar-default navbar-fixed-top">
      <div class="container-fluid">     
        <!-- Header -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" 
                    data-toggle="collapse" 
                    data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>

            <%= link_to "Hlín Reykdal", root_path, class: 'navbar-brand' %>

        </div>
        <!-- Navbar Links -->
        <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li class="dropdown full-width">
                    <a href="#" class="dropdown-toggle" 
                    data-toggle="dropdown">
                        Shop
                    </a>


                    <ul class="dropdown-menu" role="menu">


                        <% @categories.each do |category| %>
                            <li><%= link_to category.name, category %></li>
                        <% end %>    

                    </ul>
                </li>       


                  <li class="dropdown full-width">
                    <a href="#" class="dropdown-toggle" 
                    data-toggle="dropdown">
                        Designers
                    </a>


                    <ul class="dropdown-menu" role="menu">


                       <% @designers.each do |designer| %>
                            <li><%= link_to designer.designer_name, designer %></li>
                        <% end %>    




                    </ul>
                </li>       



                <li class="dropdown full-width">
                    <a href="#" class="dropdown-toggle" 
                    data-toggle="dropdown">
                        Info
                    </a>
                    <ul class="dropdown-menu" role="menu">
                        <li><%= link_to "About", pages_about_path %></li>

                        <li><%= link_to "Stockists", stockists_path %></li> 

                        <li class="divider"></li>
                        <li><a href="#">Location</a></li>
                        <li class="divider"></li>
                        <li><%= link_to "Help", pages_help_path %></li> 
                    </ul>
                </li>          
            <% if current_user && current_user.admin? %>
             <li class="dropdown full-width">
                    <a href="#" class="dropdown-toggle" 
                    data-toggle="dropdown">
                        Admin
                    </a>

                    <ul class="dropdown-menu" role="menu">
                        <li><%= link_to "Products", products_path %></li>
                        <li><%= link_to "Categories", categories_path %></li>
                        <li><%= link_to "Designers", designers_path %></li>
                        <li><%= link_to "Stockists", stockists_path %></li>

                    </ul>

                </li>   

                <% else %> 

            <% end %>

                <% if user_signed_in? %>

                <li class="dropdown full-width">
                   <p class="navbar-link"> Logged in as <%= current_user.name %>.</p>
                   </li>
                   <li>
                    <%= link_to "Logout", destroy_user_session_path, method: :delete %> </li>    
                <% else %>
                <li class="dropdown full-width">

                    <%= link_to "Login", new_user_session_path, class: "navbar-link" %>
                <% end %>        


                </li>     

                <%= render 'cart' %>    

                <%= render 'search' %> 

            </ul>
        </div>
       </div> 
    </div>

    <script src="js/bootstrap.min.js" type="text/javascript"></script>
<script>
  /**
 * Listen to scroll to change header opacity class
 */
function checkScroll(){
    var startY = $('.navbar').height() * 2; //The point where the navbar changes in px

    if($(window).scrollTop() > startY){
        $('.navbar').addClass("scrolled");
    }else{
        $('.navbar').removeClass("scrolled");
    }
}

if($('.navbar').length > 0){
    $(window).on("scroll load resize", function(){
        checkScroll();
    });
}
</script>
<script>
    $('.dropdown').hover(
        function() {
            $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeIn();
        }, 
        function() {
            $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut();
        }
    );

    $('.dropdown-menu').hover(
        function() {
            $(this).stop(true, true);
        },
        function() {
            $(this).stop(true, true).delay(200).fadeOut();
        }
    );
</script>

这是_cart.html.erb

<% if @cart %>

 <li class="dropdown full-width ">
     <a href="#" class="dropdown-toggle" 
                    data-toggle="dropdown">
             <% if @cart.empty? %>
                        Cart 0
                 <% else %>

                 <%= link_to "Cart #{pluralize @cart.count, "item"}", cart_path %>
             <% end %>
     </a>

 </li>

<% end %>

某种方式_cart.html.erb仅在products/search.html.erb中可见。我不知道为什么它只显示在我的页面上。

这是products/search.html.erb

的代码
<div class="container">
<div class="row product_top">
<table class="table table-hover">
  <thead>
    <tr>
      <th>Name</th>
      <th>Description</th>
      <th>Designer</th>
      <th>Price</th>
      <th>Stock</th>
      <th>Image</th>

    </tr>
  </thead>

<tbody>
  <% @products.each do |product| %>

      <tr>
        <td><%= link_to product.name, product %></td>
        <td><%= product.description %></td>
        <td><%= product.designer.designer_name %></td>

        <td><%= number_to_currency product.price %></td>
        <td><%= product.stock_quantity %></td>

        <td><%= image_tag product.image.thumb %></td>


  <% end %>
  </tr>
</tbody>
</table>
</div>
</div>

EDITED

_cart.html.erb_navbar.html.erb都存储在views/application

这是carts_controller.rb

class CartsController < ApplicationController
    before_filter :initialize_cart

    def add
        @cart.add_item params[:id]
        session["cart"] = @cart.serialize
        product = Product.find params[:id]
        redirect_to :back, notice: "Added #{product.name} to cart."
    end

    def show

    end

    def checkout
        @order_form = OrderForm.new user: User.new
        @client_token = Braintree::ClientToken.generate
    end
end

这里是application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :find_categories
  before_action :find_designers


  def initialize_cart
    @cart = Cart.build_from_hash session
  end

  def find_categories
    @categories = Category.all
  end

  def find_designers
    @designers = Designer.all
  end

  def authenticate_admin!
    authenticate_user!

    unless current_user.admin?
      redirect_to root_path, alert: "You are not allowed to perform that operation."
    end
  end
end

products_controller.rb

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
  before_filter :initialize_cart
  before_action :authenticate_admin!, only: [ :new, :edit, :update, :create, :destroy ]
  # GET /products
  # GET /products.json
  def index
    @products = Product.all
  end

  def search

    @products = Product.search(params[:query]).order("created_at DESC")
    @categories = Category.joins(:products).where(:products => {:id => @products.map{|x| x.id }}).distinct

  end

  # GET /products/1
  # GET /products/1.json
  def show

  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit

  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:name, :description, :price, :image, :category_id, :stock_quantity, :designer_id, :query)
    end
end

我整个上午都在试图弄明白这一点,没有运气。这里的任何人都可以看看这个并通知我吗?

提前谢谢 d

0 个答案:

没有答案