簡體   English   中英

Ruby on Rails:自從更改列表順序以來,不顯示錯誤消息

[英]Ruby on Rails: Error messages not displaying since changing order of a list

我最近向我尋求幫助,要求對復選框列表進行重新排序, Ruby on Rails:對復選框標簽進行重新排序

我找到了一個很好的答案,然后離開並更改了代碼。 由此,

<div class="tech" STYLE="text-align: left;">
  <b>Technologies:</b>
  <style>
    .split { text-align:left; }
  </style>


  <p><ul> 

  <% for technol in Technol.all %> 
   <li class="split"> 
    <%= check_box_tag "project[technol_ids][]", technol.id,  @project.technols.include?(technol) %> 
    <%= technol.tech %> 
   </li> 

  <% end %> 
 </ul> 
 </p> 

為此,

<div class="tech" STYLE="text-align: left;">
<b>Technologies:</b>
<style>
    .split { text-align:left; }
</style>


<p><ul> 

<% @all_technols.each do |technol| %> 



<li class="split"> 
<%= check_box_tag "project[technol_ids][]", technol.id,  @project.technols.include?(technol) %> 
<%= technol.tech %> 
</li> 

<% end %>
</ul> 
</p> 

項目控制器動作new如下所示:

def new
    @project = Project.new
        @technol = Technol.new(params[:tech])

        @all_technols = Technol.order('tech ASC') 

        tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?


        @project_technol = @project.projecttechnols.build

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @project }
    end
  end

我現在注意到,如果用戶輸入一個新項目,並且在我的代碼中標記了validates_presence_ofvalidates_format_of ,則錯誤消息不會顯示,而是出現錯誤消息:

NoMethodError in Projects#create 
line #256 raised: 
undefined method `each' for nil:NilClass 

Extracted source (around line #256): 
253: 
254: <p><ul> 
255: 
256: <% @all_technols.each do |technol| %> 
257: 
258: 
259:

它一定與重新排序技術有關,但是我似乎找不到解決方法。 希望有人可以看到我要去哪里。 提前致謝。

編輯

def create  
    @project = Project.new(params[:project])
        @project.client = params[:new_client] unless params[:new_client].blank?
        @project.role = params[:new_role] unless params[:new_role].blank?
        @project.industry = params[:new_industry] unless params[:new_industry].blank?
        @project.business_div = params[:new_business_div] unless params[:new_business_div].blank?


if !params[:technols].nil?

            params[:technols][:id].each do |tech|

                if !tech.empty?

                    @project_technol = @project.projecttechnols.build(:technol_id => tech) 

                end
            end

end

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

在控制器的create方法中,或者處理提交的操作的名稱是什么,請確保在呈現new視圖之前再次填充@all_technols ,否則會出現此錯誤!

因此,在create動作中,例如:

else

  format.html { @all_technols = Technol.order('tech ASC'); render action: "new" }
  format.json { render json: @project.errors, status: :unprocessable_entity }
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM