2013-05-10 96 views
0

在我的Rails應用程序中,我有一個控制器tickets_controller.rb和model ticket.rb。爲了創建一票,我有以下形式,無法在Rails中的控制器中訪問模型方法

<%= form_for(@ticket) do |f| %> 
    <% if @ticket.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@ticket.errors.count, "error") %> prohibited this ticket from being saved:</h2> 

     <ul> 
     <% @ticket.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <%= f.label :ref_no, "Reference Number"%><br/> 
    <%= f.text_field :ref_no%><br /> 

    <%= f.label :category, "Type of Request"%><br/> 
    <%= f.text_field :category_id %><br /> 

    <%= f.label :issue, "Issue"%><br/> 
    <%= f.text_area :issue%><br /> 

    <%= f.label :ticket_priority, "Priority level"%><br/> 
    <%= f.text_field :ticket_priority_id %><br /> 

    <%= f.label :ticket_status, "Current Status"%><br/> 
    <%= f.text_field :ticket_status_id %><br /> 

    <%= f.label :project, "Project"%><br/> 
    <%= f.text_field :project_id %><br /> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

我要創建在Form_Load(門票/新)唯一的隨機參考數,它應該被追加到參考號碼文本字段。在創建新的參考號碼時,應該檢查票據表是否有重複。所以,我有以下的模型,

ticket.rb

class Ticket < ActiveRecord::Base 

    attr_accessible :issue, :ticket_status_id, :ticket_priority_id, :ref_no, :category_id, :project_id 
    has_many :ticket_statuses , :through => :ticket_histories 
    has_one :ticket_priority 
    belongs_to :user 

    before_create :generate_token 

    protected 


    def generate_num 
    self.token = loop do 
     random_token = random(1000000000) 
     break random_token unless Ticket.exists?(:ref_no => random_token) 
    end 
    end 

end 

tickets_controller.rb

class TicketsController < ApplicationController 
    before_filter :authenticate_user! 
    #load_and_authorize_resource 

    def index 
    @tickets = Ticket.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render :json => @tickets } 
    end 
    end 


    def show 
    @ticket = Ticket.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render :json => @ticket } 
    end 
    end 


    def new 
    @ticket = Ticket.new 

    @ref_no = Ticket.generate_num 

    @categories = Category.all 
    @status = TicketStatus.first 
    @priorities = TicketPriority.all 
    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render :json => @ticket } 
    end 
    end 


    def edit 
    @ticket = Ticket.find(params[:id]) 
    end 


    def create 
    @ticket = Ticket.new(params[:ticket]) 
    respond_to do |format| 
     if @ticket.save 
     format.html { redirect_to @ticket, :notice => 'Ticket was successfully created.' } 
     format.json { render :json => @ticket, :status => :created, :location => @ticket } 
     else 
     format.html { render :action => "new" } 
     format.json { render :json => @ticket.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    @ticket = Ticket.find(params[:id]) 

    respond_to do |format| 
     if @ticket.update_attributes(params[:ticket]) 
     format.html { redirect_to @ticket, :notice => 'Ticket was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render :action => "edit" } 
     format.json { render :json => @ticket.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 


    def destroy 
    @ticket = Ticket.find(params[:id]) 
    @ticket.destroy 

    respond_to do |format| 
     format.html { redirect_to tickets_url } 
     format.json { head :no_content } 
    end 
    end 
end 

當我運行我的應用程序,我得到了跟隨錯誤。誰能幫忙?

NoMethodError in TicketsController#new 

undefined method `generate_num' for #<Class:0x7f5cdc1f21c0> 

Rails.root: /home/local/Rajesh/ticket_system 
Application Trace | Framework Trace | Full Trace 

app/controllers/tickets_controller.rb:27:in `new' 

回答

0

您已經定義和實例方法和你正在使用的對象

@ticket.generate_num 
1

更改模型方法generate_numself.generate_num使用

調用方法調用它。

def self.generate_num 
    token = loop do 
     random_token = random(1000000000) 
     break random_token unless Ticket.exists?(:ref_no => random_token) 
    end 
end 
+0

self.token有錯誤。未定義的方法「令牌=」。這是什麼? – 2013-05-10 05:54:40

+0

只是使用標記而不是self.token。隨機(1000000000)在我的結尾不起作用。如果你也面臨同樣的問題,那麼使用rand(1000000000)。 – Deepika 2013-05-10 10:30:18

相關問題