图像作为rails 4 App中类别的链接

时间:2016-06-14 10:48:19

标签: ruby-on-rails ruby loops

在我的views/pages/index.html.erb我有这个循环,它会显示每个类别中最新上传的图片。

<div class="container-fluid">


    <% @products.each_slice(3) do |products_group| %>
    <div class="row">
      <% products_group.each do |category, products| %>

            <% products.each_with_index do |product, index| %>
                <% if index == 0 %>
                    <div class="col-lg-4 col-sm-6 col-xs-12 center-block " >

                    <%= image_tag product.image.url(:medium), class: "img-responsive" %>
            <div class="caption">
                <p><%= product.category.name %></p>
             </div> 
            <% end %>
            <% end %>
            </div> 
        <% end %>
        </div>
    <% end %>

</div>

我一直在尝试将这行代码添加到'image_tag'部分

<%=link_to image_tag product.image.url(:medium), category_path (@category.products), class: "img-responsive" %>

因此,用户可以通过点击views/pages/index.html.erb

中的图片转到每个类别

它给了我这个错误syntax error, unexpected ( arg, expecting keyword_do or '{' or '(' ...e.url(:medium), category_path (@category.products), class: "... ... ^ /Users/dadi/Documents/Vefir/stores/brainstore/app/views/pages/index.html.erb:25: syntax error, unexpected ',', expecting ')' ...gory_path (@category.products), class: "img-responsive" );@o... ... ^

我一直在尝试调整代码以摆脱错误,但我找不到合适的方法来做到这一点。

我不确定如何做到这一点,这里的任何人都可以引导我走向正确的道路吗?

这是我的categories_controller.rb

class CategoriesController < ApplicationController
 before_action :set_category, only: [:show, :edit, :update, :destroy]

 # GET /categories
 # GET /categories.json
  def index
   @categories = Category.all
  end

  # GET /categories/1
  # GET /categories/1.json
 def show
    @products = @category.products
  end

 # GET /categories/new
 def new
   @category = Category.new
 end

 # GET /categories/1/edit
  def edit
 end

 # POST /categories
 # POST /categories.json
   def create
    @category = Category.new(category_params)

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

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

# DELETE /categories/1
# DELETE /categories/1.json
 def destroy
   @category.destroy
    respond_to do |format|
  format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
    format.json { head :no_content }
  end
 end

 private
   # Use callbacks to share common setup or constraints between actions.
   def set_category
    @category = Category.includes(:products).find(params[:id])
   end

  # Never trust parameters from the scary internet, only allow the white list through.
   def category_params
     params.require(:category).permit(:name)
   end
  end

这是pages_controller.rb

 class PagesController < ApplicationController
  def index
   @products = Product.all.order(created_at: :desc).group_by(&:category_id)

  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

 # 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)
   end
end

这是我的routes.rb

Rails.application.routes.draw do
 get 'pages/index'

 get 'pages/about'

 get 'pages/location'

 get 'pages/stockists'

 devise_for :users
 resources :categories
 resources :categories
 resources :category_names
 resources :products


 resource :cart, only: [:show] do
   post "add", path: "add/:id", on: :member
   get :checkout
 end

 resources :orders, only: [ :index, :show, :create, :update ] do
   member do
     get :new_payment
     post :pay
   end
 end


 root 'pages#index'

end

2 个答案:

答案 0 :(得分:1)

执行此操作的最佳方法是将link_to与块一起使用。以下是使用link_to

以块格式编写的代码
<%= link_to category_path(@category.products), class: "img-responsive" do %>
  <%= image_tag product.image.url(:medium) %>
<% end %>

了解有关link_to的更多信息。

答案 1 :(得分:1)

尝试将image_tag括在括号中:

<%=link_to image_tag(product.image.url(:medium)), category_path (@category.products), class: "img-responsive" %>

或使用块:

<%= link_to category_path (@category.products), class: "img-responsive" do %>
  <%= image_tag product.image.url(:medium) %>
<% end %>