Ruby on Rails:更改列表順序後未顯示錯誤消息 (Ruby on Rails: Error messages not displaying since changing order of a list)


問題描述

Ruby on Rails:更改列表順序後未顯示錯誤消息 (Ruby on Rails: Error messages not displaying since changing order of a list)

I recently asked for help reordering a list of check boxs, Ruby on Rails: re‑order checkbox tag

I found a good answer and went away and changed my code. From this,

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

to this,

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

with the project controller action new looking like this:

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

I have now noticed that if a user enters a new project, and a validates_presence_of or validates_format_of flags up in my code, the error messages do not show, and instead the I get an error messsage 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:

It must have something to do with reordering the technologies, but I can't seem to find a fix. Hopefully someone can see where I'm going wrong. Thanks in advance.

EDIT

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

‑‑‑‑‑

參考解法

方法 1:

In the create method of the controller, or whatever is the name of the action dealing with your submission, make sure you populate again @all_technols before rendering the new view, otherwise you'll get this error!

So in the create action for example:

else

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

(by JazzAnthony Alberto)

參考文件

  1. Ruby on Rails: Error messages not displaying since changing order of a list (CC BY‑SA 3.0/4.0)

#controller #error-handling #ruby-on-rails #ruby






相關問題

覆蓋我的控制器,我應該在哪個事件中檢查 cookie? (Overriding my controller, which event should I check for a cookie in?)

ASP.NET MVC,將模型從視圖傳遞到控制器 (ASP.NET MVC, passing Model from View to Controller)

Ruby on Rails:更改列表順序後未顯示錯誤消息 (Ruby on Rails: Error messages not displaying since changing order of a list)

Успадкоўванне кантролера ASP.NET MVC 4 (ASP.NET MVC 4 controller inheritance)

如何在 CakePHP 中創建自定義 MySQL 查詢? (How to create custom MySQL queries in CakePHP?)

Respond_to 沒有調用正確的格式 (Respond_to does not call the right format)

Rails - 尋找錯誤 ID 的 JSON 回調 (Rails - JSON callback looking for wrong id)

YIi PHP - 帶有 foreach 循環的輸出數組 (YIi PHP - Output array with a foreach loop)

使用 SQL 將方法邏輯移動到 Rails 項目中的模型 (Moving method logic with SQL to model in rails project)

MVC 為什麼要單元測試控制器 (MVC Why unit test controllers)

Jquery & Rails 3:從 js 文件運行控制器操作? (Jquery & Rails 3: Run controller action from js file?)

.net core mvc:在區域上添加具有 crud 功能的控制器未正確生成代碼和鏈接 (.net core mvc: adding controller with crud functionality on area didn't generate code and link correctly)







留言討論