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