Rails - 在application.html.erb上使用link_to使嵌套资源无法获取id

时间:2015-02-10 02:20:17

标签: ruby-on-rails navigation resources nested link-to

我在application.html.erb上有一个侧边栏,链接应该转到/ brands / [brand_id] /优惠券

我使用了brand_coupons_path(@brand),但我收到一条错误消息,说“没有路线匹配{:action =>" index",:brand_id => nil,:controller => "优惠券"}缺少必需的密钥:[:brand_id]'

resources :brands do
  resources :coupons, :sales
end


class Brand < ActiveRecord::Base
  has_many :coupons
  has_many :sales
  accepts_nested_attributes_for :coupons
  accepts_nested_attributes_for :sales
end



class Coupon < ActiveRecord::Base
    belongs_to :brand
end


<div class="sidebar">
    <ul class="sidebar-list">
        <li><a class="sidebar-header">Most Popular Brands</a></li>
        <% @brands.each do |brand| %>
            <% if current_page?(:controller => 'coupons') %>
                <li><%= link_to brand.name, brand_coupons_path(@brand), :class => "sidebar-link" %></li>
            <% else %>
                <li><%= link_to brand.name, brand_sales_path(@brand), :class => "sidebar-link" %></li>
            <% end %>
        <% end %>
    </ul>
</div>

class CouponsController < ApplicationController
          before_action :set_coupon, only: [:show, :edit, :update, :destroy]
          # before_filter :load_brand

        def new
            @coupon = Coupon.new
        end

        def index
            @coupons = Coupon.where(brand_id: params[:brand_id])
        end

        def show
        end

        def create
            @coupon = Coupon.new(coupon_params)

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

class BrandsController < ApplicationController
    before_action :set_brand, only: [:show, :edit, :update, :destroy]

    # GET /brands
    # GET /brands.json


    def index
      @brands = Brand.all
    end

    # GET /brands/1
    # GET /brands/1.json
    def show
    end

    # GET /brands/new
    def new
      @brand = Brand.new
    end

    # GET /brands/1/edit
    def edit
    end
# POST /brands
  # POST /brands.json
  def create
    @brand = Brand.new(brand_params)

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

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

  # DELETE /brands/1
  # DELETE /brands/1.json
  def destroy
    @brand.destroy
    respond_to do |format|
      format.html { redirect_to brands_url, notice: 'Brand was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

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

2 个答案:

答案 0 :(得分:1)

您将未定义的@brand变量传递给路径助手,而不是块变量brand。变化:

<%= link_to brand.name, brand_coupons_path(@brand), :class => "sidebar-link" %>
<%= link_to brand.name, brand_sales_path(@brand), :class => "sidebar-link" %>

<%= link_to brand.name, brand_coupons_path(brand), :class => "sidebar-link" %>
<%= link_to brand.name, brand_sales_path(brand), :class => "sidebar-link" %>

答案 1 :(得分:0)

@brand包含哪些内容?我认为你应该把一个对象放到@brand变量上。

喜欢这个。 @brand = Brand.find(params[:id])

我们能看到你索引的控制器吗?