gpt4 book ai didi

ruby-on-rails - Ruby on Rails : Error messages not displaying since changing order of a list

转载 作者:行者123 更新时间:2023-12-03 08:08:09 25 4
gpt4 key购买 nike

我最近请求帮助重新排序复选框列表Ruby on Rails: re-order checkbox tag

我找到了一个很好的答案,然后离开并更改了代码。由此,

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

项目 Controller Action 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

最佳答案

在 Controller 的create方法中,或者处理提交的操作的名称是什么,请确保在呈现@all_technols View 之前再次填充new,否则会出现此错误!

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

else

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

关于ruby-on-rails - Ruby on Rails : Error messages not displaying since changing order of a list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13417742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com