Ruby on rails 无法访问Rails中其控制器内的模型方法

Ruby on rails 无法访问Rails中其控制器内的模型方法,ruby-on-rails,model,controller,methodaccessexception,Ruby On Rails,Model,Controller,Methodaccessexception,在我的Rails应用程序中,我有一个控制器tickets\u controller.rb和model ticket.rb。要创建票证,我有以下表格 <%= form_for(@ticket) do |f| %> <% if @ticket.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@ticket.errors.count, "error"

在我的Rails应用程序中,我有一个控制器tickets\u controller.rb和model ticket.rb。要创建票证,我有以下表格

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

禁止保存此票据:












我想在表单加载(ticket/new)上创建一个唯一的随机参考号,它应该附加到参考号文本字段中。在创建新的参考号时,它应该检查tickets表是否存在重复。所以我有下面的模型

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
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
classticket:票证历史
有一张:优先票
属于:用户
创建前:生成令牌
受保护的
def生成_num
self.token=循环do
随机令牌=随机(100000000)
除非票证存在,否则中断随机令牌(:ref\u no=>随机令牌)
结束
结束
结束

tickets\u controller.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
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
class ticketcontroller@tickets}
结束
结束
def秀
@ticket=ticket.find(参数[:id])
回应待办事项|格式|
format.html#show.html.erb
format.json{render:json=>@ticket}
结束
结束
def新
@票子
@参考号=票证生成号
@categories=Category.all
@状态=TicketStatus.first
@优先级=TicketPriority.all
回应待办事项|格式|
format.html#new.html.erb
format.json{render:json=>@ticket}
结束
结束
定义编辑
@ticket=ticket.find(参数[:id])
结束
def创建
@票证=票证。新(参数[:票证])
回应待办事项|格式|
如果@ticket.save
format.html{redirect_to@ticket,:notice=>“ticket已成功创建”。}
format.json{render:json=>@ticket,:status=>:created,:location=>@ticket}
其他的
format.html{render:action=>“new”}
format.json{render:json=>@ticket.errors,:status=>:unprocessable_entity}
结束
结束
结束
def更新
@ticket=ticket.find(参数[:id])
回应待办事项|格式|
if@ticket.update_属性(参数[:ticket])
format.html{redirect_to@ticket,:notice=>“ticket已成功更新”。}
format.json{head:no_content}
其他的
format.html{render:action=>“edit”}
format.json{render:json=>@ticket.errors,:status=>:unprocessable_entity}
结束
结束
结束
def销毁
@ticket=ticket.find(参数[:id])
@销毁
回应待办事项|格式|
format.html{redirect_to tickets_url}
format.json{head:no_content}
结束
结束
结束
当我运行我的应用程序时,我得到以下错误。有人能帮忙吗

 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'
ticketcontroller中的命名错误#新增
未定义的方法“generate_num”#
Rails.root:/home/local/Rajesh/ticket\u系统
应用程序跟踪|框架跟踪|完整跟踪
app/controllers/tickets\u controller.rb:27:in'new'

您已经定义并实例化了方法,并且正在使用类调用它

使用对象调用方法

@ticket.generate_num

将模型方法
generate_num
更改为
self.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

只需使用token而不是self.token。而random(100000000)在我这边不起作用。如果您也面临同样的问题,请使用兰特(100000000)。