2015-05-28 42 views
0

我一直在開始學習rails和ruby。我的編碼和應用程​​序,一個用戶有很多購買和銷售。我一直在做所有的教程和這裏尋找答案,許多在SOF但都沒有成功,所以這是我的問題:在SellsController#聲明路線和關係後,未定義的方法'類'

NoMethodError爲#

創建 未定義的方法'賣」我的用戶是西班牙語的「usuario」。

我知道我在某處丟失了某些東西,因爲我已經在買東西的時候做了這件事,而且效果很好。這些都是我的文件

Usuario.rb(型號)

class Usuario < ActiveRecord::Base 
# Include default devise modules. Others available are: 
# :confirmable, :lockable, :timeoutable and :omniauthable 
devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 
has_many :sells 
has_many :buys 
end 

有 「HAS_MANY」

Sell.rb(型號)

class Sell < ActiveRecord::Base 
    belongs_to :usuario 
    after_create :set_estado 

    private 

    def set_estado 
    self.estado = true 
    end 

end 

這種方法是因爲我試圖設置「estado」(狀態)爲真。使用

塞爾斯控制器

class SellsController < ApplicationController 
    before_action :set_sell, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_usuario! 

    # GET /sells 
    # GET /sells.json 
    def index 
    @sells = Sell.all 
    end 

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

    # GET /sells/new 
    def new 
    @sell = Sell.new 
    end 

    # GET /sells/1/edit 
    def edit 
    end 

    # POST /sells 
    # POST /sells.json 
    def create 
    @sell = current_usuario.sell.new(sell_params) 
    respond_to do |format| 
     if @sell.save 
     format.html { redirect_to @sell, notice: 'La venta ha sido creada con exito' } 
     format.json { render :show, status: :created, location: @sell } 
     else 
     format.html { render :new } 
     format.json { render json: @sell.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /sells/1 
    # PATCH/PUT /sells/1.json 
    def update 
    respond_to do |format| 
     if @sell.update(sell_params) 
     format.html { redirect_to @sell, notice: 'La venta ha sido modificada con exito.' } 
     format.json { render :show, status: :ok, location: @sell } 
     else 
     format.html { render :edit } 
     format.json { render json: @sell.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /sells/1 
    # DELETE /sells/1.json 
    def destroy 
    @sell.destroy 
    respond_to do |format| 
     format.html { redirect_to sells_url, notice: 'La venta ha sido eliminada con exito.' } 
     format.json { head :no_content } 
    end 
    end 

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

    # Never trust parameters from the scary internet, only allow the white list through. 
    def sell_params 
     params.require(:sell).permit(:usuario_id, :fecha_compra, :peso_final, :monto, :estado, :cantidad) 
    end 
end 

林支架和寶石設計一種用於驗證。

我不知道我在想念,因爲我在購買幾乎同樣的事情,它的工作,但對於銷售不工作:(

Routes.db

Rails.application.routes.draw do 

    devise_for :usuarios 
    devise_for :users 
    resources :usuarios do 
    resources :buys 
    resources :sells 
    end 

    get 'welcome/index' 
    post 'welcome/index' 
    post 'users/index' 
    resources :buys 
    resources :users 
    resources :sells 

end 

這是我在軌道上的第一個應用程序,所以我不知道我現在缺少什麼。我使用設計,Rails 4.2.1和Postgre作爲數據庫

回答

0

如果您的關聯是has_many,Rails將使用模型的複數形式作爲關聯名稱。因此,而不是

current_usario.sell.new

嘗試:

current_usario.sells.build

如果你只希望每個用戶一個Sell,使用has_one協會代替。

P.S.你可能會考慮使用「銷售」而不是「銷售」,而「購買」而不是「購買」。

+0

謝謝你。這正是問題所在。我會接受這個建議!我想我需要提高我的英語水平:) –

相關問題