Rails - 在 application.html.erb 上使用 link_to 嵌套资源未获取 ID
Rails - using link_to on application.html.erb for nested resource not getting id
我在 application.html.erb 上有一个侧边栏,链接应该转到 /brands/[brand_id]/coupons
我使用了 brand_coupons_path(@brand) 但我收到一条错误消息 'No route matches {:action=>"index", :brand_id=>nil, :controller=>"coupons"} missing required keys: [: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
@brand
包含什么?我认为您应该将对象放入 @brand
变量。
像这样。 @brand = Brand.find(params[:id])
我们可以看到您的索引控制器吗?
您将未定义的 @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" %>
我在 application.html.erb 上有一个侧边栏,链接应该转到 /brands/[brand_id]/coupons
我使用了 brand_coupons_path(@brand) 但我收到一条错误消息 'No route matches {:action=>"index", :brand_id=>nil, :controller=>"coupons"} missing required keys: [: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
@brand
包含什么?我认为您应该将对象放入 @brand
变量。
像这样。 @brand = Brand.find(params[:id])
我们可以看到您的索引控制器吗?
您将未定义的 @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" %>