简体   繁体   English

在用户和嵌套路线后编辑link_to

[英]Edit link_to behind user and nested routes

I'm trying to construct a link for a object that's nested behind a user and shopmain routes: user>shompmain>shopaddress 我正在尝试为嵌套在用户和shopmain路线后面的对象构造链接:user> shompmain> shopaddress

I can't get the show/edit/delete links working. 我无法显示/编辑/删除链接。

<%= 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" %>

I get this error that reads something like. 我收到类似这样的错误。 It seems like I'm missing the id for the shopaddress for some reason. 似乎由于某种原因我缺少商店地址的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]

My routes file readslike 我的路线文件看起来像

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

My shopaddresses controller looks like: 我的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

The full index view (per comments request) 完整索引视图(根据评论请求)

<!-- 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>

I'm surprised you said your show link works as it clearly cannot work. 我很惊讶您说您的演出链接有效,因为它显然无法正常工作。 :) Your route helpers expect three parameters, not two - you are missing user id. :)您的路线助手需要三个参数,而不是两个-您缺少用户ID。 So it should be: 因此应该是:

<%= link_to 'Show', user_shopmain_shopaddresses_path(current_user, shopaddress.shopmain, shopaddress), :class => "btn btn-primary btn-sm" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM