我有application.html.erb一个侧边栏,并链接应该去/品牌/ [brand_id] /优惠券导轨 - 使用的link_to上application.html.erb嵌套资源没有得到ID
我用brand_coupons_path(@brand),但我得到一个错误,说'没有路线匹配{:action =>“index”,:brand_id => nil,:controller =>“coupons”}缺少必需的键:[: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
,设置链接的/brands.[id]代码 - 需要它去/品牌/ [ID] /优惠券 – 2015-02-10 03:25:41
这工作, 谢谢 – 2015-02-10 03:32:57