我正在尝试为嵌套在用户和shopmain路线后面的对象构建链接:user> shompmain> shopaddress
我无法让show / edit / delete链接正常工作。
<%= link_to 'Show', user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), :class => "btn btn-primary btn-sm" %>
<%= link_to 'Edit', edit_user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), :class => "btn btn-primary btn-sm" %>
<%= link_to 'Delete', user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-danger btn-sm" %>
我得到的错误就是这样的。由于某种原因,我似乎错过了shopaddress的id。
No route matches {:action=>"show", :controller=>"shop/shopaddresses", :id=>nil, :shopmain_id=>#<Shop::Shopaddress id: 2, shopadrsline1: "Civic Place", shopadrsline2: "Box 888", shopadrscity: "Toronto", shopadrsprovince: "ON", shopadrspostalcode: "N0N1V0", shopmain_id: 2, created_at: "2016-01-17 03:38:47", updated_at: "2016-01-17 03:38:47">, :user_id=>#<Shop::Shopmain id: 2, shopname: "Eastwood", shoptagline: "Testing", user_id: 1, created_at: "2016-01-17 03:30:01", updated_at: "2016-01-17 03:30:01">} missing required keys: [:id]
我的路线文件读起来像
Rails.application.routes.draw do
# Devise Authetication
devise_for :users
devise_for :admins
# Website Pages
root 'website/page#index'
# Backend Shop Module
resources :users do
scope module: "shop" do
resources :shopmains do
resources :shopdetails
resources :shopaddresses
resources :shophours
end
end
end
end
我的shopaddresses控制器看起来像:
class Shop::ShopaddressesController < ApplicationController
# Security and Layouts
before_filter :authenticate_user!
layout 'layouts/shop/application.html.erb'
before_action :set_shopaddress, only: [:show, :edit, :update, :destroy]
# Shop Address Index
def index
shopmain = Shop::Shopmain.find(params[:shopmain_id])
@shopaddresses = shopmain.shopaddresses
end
# Shop Address Single View
def show
shopmain = Shop::Shopmain.find(params[:shopmain_id])
@shopmain = shopmain.shopaddresses.find(params[:id])
end
# New Shop Address
def new
shopmain = Shop::Shopmain.find(params[:shopmain_id])
@shopaddress = shopmain.shopaddresses.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @shopmain }
end
end
# Edit Shop Address
def edit
shopmain = Shop::Shopmain.find(params[:shopmain_id])
@shopaddress = shopmain.shopaddresses.find(params[:id])
end
# Create a New Shop Address
def create
@shopmain = Shop::Shopmain.find(params[:shopmain_id])
@shopaddress = @shopmain.shopaddresses.create(shopaddress_params)
respond_to do |format|
if @shopaddress.save
format.html { redirect_to user_shopmain_shopaddresses_path, notice: 'Comment for Shopmain was Successfully Created.' }
format.json { render action: 'show', status: :created, location: @shopaddress }
else
format.html { render action: 'new' }
format.json { render json: @shopaddress.errors, status: :unprocessable_entity }
end
end
end
# Update Shop Address
def update
respond_to do |format|
if @shopaddress.update(shopaddress_params)
format.html { redirect_to user_shopmain_shopaddresses_path, notice: 'Comment for Shopmain was Successfully Updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @shopaddress.errors, status: :unprocessable_entity }
end
end
end
# Delete Shop Address
def destroy
@shopmain = Shop::Shopmain.find(params[:shopmain_id])
@shopaddress = @shopmain.shopaddresses.find(params[:id])
@shopaddress.destroy
respond_to do |format|
format.html { redirect_to user_shopmain_shopaddresses_path, notice: 'Comment for Shopmain was Successfully Deleted.' }
format.json { head :no_content }
end
end
private
# Common Constrainsts
def set_shopaddress
@shopaddress = Shop::Shopaddress.find(params[:id])
end
# Strong Params Whitelist
def shopaddress_params
params.require(:shop_shopaddress).permit(:shopadrsline1, :shopadrsline2, :shopadrscity, :shopadrsprovince, :shopadrspostalcode, :shopmain_id)
end
end
完整的索引视图(根据评论请求)
<!-- Information Card -->
<p id="notice"><%= notice %></p>
<div class="wrapper wrapper-content">
<div class="panel">
<div class="panel-heading">
<h4 class="panel-title">Shop Listing Index</h4>
</div>
<div class="panel-body">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Address Line 1</th>
<th>Address Line 2</th>
<th>City</th>
<th>Province</th>
<th>Code</th>
<th></th>
</tr>
</thead>
<tbody>
<% @shopaddresses.each do |shopaddress| %>
<tr>
<td><%= shopaddress.shopadrsline1 %></td>
<td><%= shopaddress.shopadrsline2 %></td>
<td><%= shopaddress.shopadrscity %></td>
<td><%= shopaddress.shopadrsprovince %></td>
<td><%= shopaddress.shopadrspostalcode %></td>
<td>
<div class="btn-group" role="group" aria-label="Member Contact Group">
<%= link_to 'Show', user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), :class => "btn btn-primary btn-sm" %>
<%= link_to 'Edit', edit_user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), :class => "btn btn-primary btn-sm" %>
<%= link_to 'Delete', user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-danger btn-sm" %>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'New Address', new_user_shopmain_shopaddress_path, :class => "btn btn-primary btn-sm" %>
</div>
</div>
</div>
答案 0 :(得分:1)
我很惊讶你说你的节目链接有效,因为它显然无法正常工作。 :)您的路线助手需要三个参数,而不是两个 - 您缺少用户ID。所以它应该是:
<%= link_to 'Show', user_shopmain_shopaddresses_path(current_user, shopaddress.shopmain, shopaddress), :class => "btn btn-primary btn-sm" %>