2013-03-16 43 views
0

我已經開始使用新工具處理舊項目。這一次 - Ruby上的Rails。我設法取得了一些進展,現在我想改進我的代碼中的一個元素。如何將數據庫操作從控制器移動到模型

整個項目大約有完整歷史搜索所有跟蹤的錯誤的bug跟蹤。現在我正處於用戶正在進入錯誤的階段。每個bug都屬於屬於項目的表。

唯一的問題 - 現在 - 是表名的自動完成功能,當我使用全新的名稱(與已經存在於數據庫中它的工作就好了表,填寫錯誤錄入的table_id)。

的觀點負責輸入(或從現有選擇)部分表看起來像這樣:

<div class="control-group"> 
    <%= f.label :table_name, :class => 'control-label' %> 
    <div class="controls"> 
    <%= f.autocomplete_field :table_name, autocomplete_table_name_bugs_path %> 
    </div> 
</div> 

沒有什麼不尋常。這一個去模型(Bug.rb)

class Bug < ActiveRecord::Base 
    attr_accessible :bugid, :fixdate, :fixnote, :fixstate_id, :milestone, :newtarget, :notes, :oldtarget, :project_id, :bugreason, :reason_id, :regressiondate, :regressionstate_id, :source, :stringid, :table_id 

    belongs_to :project 
    belongs_to :table 
    belongs_to :reason 
    belongs_to :state 

    def table_name 
    table.name if table 
    end 

    #def table_name=(name) 
    # self.table = Table.find_or_create_by_name(name) unless name.blank? 
    #end 

end 

現在沒有驗證,你可以看到。 table_name =(name)註釋,因爲它顯然沒有在我的代碼中做任何事情。

這是用蟲子控制器(bugs_controller.rb)

def create 
    if params[:bug][:table_id].nil? 
    if Table.find_or_create_by_name(params[:bug][:table_name]) 
     params[:bug][:table_id] = Table.find_by_name(params[:bug][:table_name]).id 
     params[:bug].delete :table_name 
    end 

    end 

    @bug = Bug.new(params[:bug]) 

    respond_to do |format| 
    if @bug.save 
     format.html { redirect_to bugs_path, notice: 'Bug was successfully created.' } 
     format.json { render json: @bug, status: :created, location: @bug } 
    else 
     format.html { render action: "new" } 
     format.json { render json: @bug.errors, status: :unprocessable_entity } 
    end 
    end 
end 

工作,我把這裏只是部分負責保存新的錯誤,我將設法處理更新的一部分,當我做這部分的權利。

我想提高什麼是第一部分。現在它不僅負責將table_name更改爲table_id,還負責創建新表(如果它不存在)。我知道這部分應該由模型來處理,但我不知道該怎麼做,可以使用一些幫助。

另一部分,如順便說一句。是我的下拉菜單,用戶可以選擇活動項目。它的部分處理:

<% @projects.each do |project| %> 
    <% if project.id == session[:current_project].to_i %> 
     <li class="disabled"><%= link_to project.name, '#' %></li> 
    <% else %> 
     <li><%= link_to project.name, choose_project_path(project.id) %></li> 
    <% end %> 
<% end %> 

但它工作正常,只有從項目控制器使用。如何 - 通過本書 - 我可以從其他控制器處理這個問題?確切地說,我希望它在整個項目中以相同的方式工作。

對於現在,我在每個控制器處理它的片段,但我敢肯定,回報率神仙不滿意我爲。

before_filter :projects 

    def projects 
    @projects = Project.all 
    end 

它應該如何正確完成? :)

+0

現在我看到,模型中的'before_create'可能可以完成這項工作......但仍然,建議感謝:) – 2013-03-16 18:17:07

+0

以某種方式找出它。將張貼回答tommorow - 它沒有before_create,但所有的邏輯從控制器移到模型 - 與發佈的代碼幾乎相同。還剩下一個問題,但我會把它作爲單獨的問題發佈。 – 2013-03-17 18:54:37

回答

0

所以我設法從控制器移到邏輯模型。此外,它保存了我需要的所有數據 - table_id在創建新表時將表和表project_id(存儲在會話中)隱藏到表表中。

所以形式的一部分渲染沒有改變:現在

<div class="control-group"> 
    <%= f.label :table_name, :class => 'control-label' %> 
    <div class="controls"> 
    <%= f.autocomplete_field :table_name, autocomplete_table_name_bugs_path %> 
    </div> 
</div> 

模式是這樣的:

class Bug < ActiveRecord::Base 
    attr_accessible :table_name, :bugid, :fixdate, :fixnote, :fixstate_id, :milestone, :newtarget, :notes, :oldtarget, :project_id, :bugreason, :reason_id, :regressiondate, :regressionstate_id, :source, :stringid, :table_id 

    belongs_to :project 
    belongs_to :table 
    belongs_to :reason 
    belongs_to :state 

    def table_name 
    table.name if table 
    end 

    def table_name=(name) 
    self.table = Table.find_or_create_by_name(name, project_id: self.project_id) unless name.blank? 
    end 

end 

所以,唯一的變化是在取消table_name的=方法和添加PROJECT_ID創造新表(缺少project_id是它之前沒有工作的原因)。

控制器看起來是這樣的:

def create 

    @bug = Bug.new(params[:bug]) 
    @bug.project_id = session[:current_project] 

    respond_to do |format| 
     if @bug.save 
     format.html { redirect_to bugs_path, notice: 'Bug was successfully created.' } 
     format.json { render json: @bug, status: :created, location: @bug } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @bug.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

它的工作般的魅力。儘管如此,我仍然有問題,但會將它作爲單獨的問題發佈。

相關問題