問題描述
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 Jazz、Anthony Alberto)