Rails-在application.html.erb上使用link_to来获取嵌套资源而不获取ID

柯蒂斯·埃利奥特(Kurtis Elliott)

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

我使用了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
乔丹·艾伦

您正在将未定义的@brand变量传递给路由助手,而不是block变量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" %>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

避免在rails应用着陆页上使用application.html.erb发出Flash通知

来自分类Dev

Rails渲染来自application.html.erb的实例变量

来自分类Dev

在application.html.erb中定义Rails变量的位置

来自分类Dev

在application.html.erb中定义Rails变量的位置

来自分类Dev

Rails aways呈现application.html.erb而不是想要的视图

来自分类Dev

Rails actionmailer使用link_to来转义HTML

来自分类Dev

可以查看无法在Rails中的ruby中扩展“ application / application.html.erb”吗?

来自分类Dev

骨干Rails:如何在application.html.erb中添加HTML的javascript

来自分类Dev

环绕HTML与Rails link_to

来自分类Dev

如何在Rails 6中的application.html.erb中包含一个url?

来自分类Dev

Rails直接从视图文件提供页面,而无需合并到application.html.erb

来自分类Dev

需要帮助来获取application.html.erb文件以进行呈现

来自分类Dev

如何从 rails_admin 上的 select 标签获取 (html) id?

来自分类Dev

Rails中的HTML实体link_to

来自分类Dev

如何在Rails 4中的application.html.erb中添加一个样式表

来自分类Dev

Ruby on Rails-application.html.erb中的标头div,是否在除首页之外的所有页面中呈现?

来自分类Dev

HTML erb中的Rails循环

来自分类Dev

rails:嵌套资源 show.html.erb 错误:未定义的方法... nil:nilclass

来自分类Dev

多个Rails Application docker up不工作

来自分类Dev

Rails edit.html.erb ArgumentError

来自分类Dev

在ERB内插HTML属性以修复Rails?

来自分类Dev

Rails预编译index.html.erb

来自分类Dev

如何获取以.html结尾的URL(使用Rails 5)

来自分类Dev

application.html.erb仍未呈现

来自分类Dev

使用jQuery.html方法将Div设置为具有跨度的Rails link_to

来自分类Dev

Rails 4:有什么方法可以修复/使用不带“ link_to”的常规html href链接?

来自分类Dev

在 show.html.erb 上编辑评论,而不是使用 Ruby on Rails 重定向到新的 edit.html.erb 页面

来自分类Dev

在rails的列表元素上使用link_to

来自分类Dev

Ruby on Rails:在html.erb文件中以html显示图片

Related 相关文章

  1. 1

    避免在rails应用着陆页上使用application.html.erb发出Flash通知

  2. 2

    Rails渲染来自application.html.erb的实例变量

  3. 3

    在application.html.erb中定义Rails变量的位置

  4. 4

    在application.html.erb中定义Rails变量的位置

  5. 5

    Rails aways呈现application.html.erb而不是想要的视图

  6. 6

    Rails actionmailer使用link_to来转义HTML

  7. 7

    可以查看无法在Rails中的ruby中扩展“ application / application.html.erb”吗?

  8. 8

    骨干Rails:如何在application.html.erb中添加HTML的javascript

  9. 9

    环绕HTML与Rails link_to

  10. 10

    如何在Rails 6中的application.html.erb中包含一个url?

  11. 11

    Rails直接从视图文件提供页面,而无需合并到application.html.erb

  12. 12

    需要帮助来获取application.html.erb文件以进行呈现

  13. 13

    如何从 rails_admin 上的 select 标签获取 (html) id?

  14. 14

    Rails中的HTML实体link_to

  15. 15

    如何在Rails 4中的application.html.erb中添加一个样式表

  16. 16

    Ruby on Rails-application.html.erb中的标头div,是否在除首页之外的所有页面中呈现?

  17. 17

    HTML erb中的Rails循环

  18. 18

    rails:嵌套资源 show.html.erb 错误:未定义的方法... nil:nilclass

  19. 19

    多个Rails Application docker up不工作

  20. 20

    Rails edit.html.erb ArgumentError

  21. 21

    在ERB内插HTML属性以修复Rails?

  22. 22

    Rails预编译index.html.erb

  23. 23

    如何获取以.html结尾的URL(使用Rails 5)

  24. 24

    application.html.erb仍未呈现

  25. 25

    使用jQuery.html方法将Div设置为具有跨度的Rails link_to

  26. 26

    Rails 4:有什么方法可以修复/使用不带“ link_to”的常规html href链接?

  27. 27

    在 show.html.erb 上编辑评论,而不是使用 Ruby on Rails 重定向到新的 edit.html.erb 页面

  28. 28

    在rails的列表元素上使用link_to

  29. 29

    Ruby on Rails:在html.erb文件中以html显示图片

热门标签

归档