rails命名约定和表的问题

时间:2016-10-20 13:29:05

标签: ruby-on-rails ruby-on-rails-5

我遇到了使用命名约定的rails的问题。

我有一个数据库,我无法重命名表格,因此 inflector 的名称不是复数形式。

今天我想为表“wishlist__c”创建模型和控制器,问题在这里。我通过复制产品模型,控制器....然后更改名称然后自己创建文件尝试了3次,我仍然遇到问题,然后使用 rails g scaffold wishlist__c

我尝试访问网址时出现的第一个错误:8080 / wishlist__c / index:

  

路由错误   未初始化的常量WishlistCController

存在

wishlist__c_controller.rb 。经过多次测试后我注意到双'__'是rails中的一个问题。我将它重命名为wishlist_c_controller,并与模型相同。错误消息更改为

- 解决方案:我忘记将文件夹wishlist__c重命名为viewslist文件夹中的wishlist_c

谢谢大家! -

  

WishlistCController#show中的ActiveRecord :: RecordNotFound   无法找到带有'id'= index

的WishlistC

此下的代码显示来自wishlist_c_controller.rb:

def set_wishlist__c
  @wishlist__c = ::WishlistC.find(params[:id])
end

如何解决它。我需要将我的应用程序链接到此表

编辑: 型号 wishlist_c.rb

class WishlistC < ApplicationRecord
    self.table_name = "wishlist__c"
end

wishlist_c_controller

class WishlistCController < ApplicationController
  before_action :set_wishlist__c, only: [:show, :edit, :update, :destroy]

  # GET /wishlist__c
  # GET /wishlist__c.json
  def index
    @wishlist__c = WishlistC.all
  end

  # GET /wishlist__c/1
  # GET /wishlist__c/1.json
  def show
  end

  # GET /wishlist__c/new
  def new
    @wishlist__c = WishlistC.new
  end

  # GET /wishlist__c/1/edit
  def edit
  end

  # POST /wishlist__c
  # POST /wishlist__c.json
  def create
    @wishlist__c = WishlistC.new(wishlist__c_params)

    respond_to do |format|
      if @wishlist__c.save
        format.html { redirect_to @wishlist__c, notice: 'Wishlist  c was successfully created.' }
        format.json { render :show, status: :created, location: @wishlist__c }
      else
        format.html { render :new }
        format.json { render json: @wishlist__c.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /wishlist__c/1
  # PATCH/PUT /wishlist__c/1.json
  def update
    respond_to do |format|
      if @wishlist__c.update(wishlist__c_params)
        format.html { redirect_to @wishlist__c, notice: 'Wishlist  c was successfully updated.' }
        format.json { render :show, status: :ok, location: @wishlist__c }
      else
        format.html { render :edit }
        format.json { render json: @wishlist__c.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /wishlist__c/1
  # DELETE /wishlist__c/1.json
  def destroy
    @wishlist__c.destroy
    respond_to do |format|
      format.html { redirect_to wishlist__c_index_url, notice: 'Wishlist  c was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_wishlist__c
      @wishlist__c = WishlistC.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def wishlist__c_params
      params.fetch(:wishlist__c, {})
    end
end

1 个答案:

答案 0 :(得分:0)

Rails基于控制器和模型创建RESTful路由。其中一条路线是get wishlist__c/:id,它被映射到WishlistCController的动作show。因此,当您点击网址wishlist__c/index时,需要index作为ID。

如果要呈现index页面,请创建路径get wishlist__c/index并将其映射到控制器的index方法。要使上述工作正常,您必须点击网址url:8080/wishlist__c/1,其中1是您的愿望清单ID。将其替换为id表的wishlist__c列的值。

查看控制器,您已经有一条路由get wishlist__c/映射到控制器的index方法。因此url:8080/wishlist__c/应该为您的模型呈现索引页。

相关问题