【发布时间】:2025-12-28 07:15:06
【问题描述】:
我有一个 Rails 5 应用程序,它有一个基于参数显示不同表单的视图。视图首次显示时显示正确的形式
但是,当出现错误时,它总是显示相同的部分。
日记控制器
def edit
@view = params[:view]
end
def update
respond_to do |format|
if @diary.update(diary_params)
format.html { redirect_to @diary, notice: 'Diary was successfully updated.' }
format.json { render :show, status: :ok, location: @diary }
else
format.html { render :edit }
format.json { render json: @diary.errors, status: :unprocessable_entity }
end
end
end
edit.html.erb
<% if @view == 'close' %>
<%= render 'close_fields'%>
<% elsif @view == 'new'%>
<%= render 'new_fields' %>
<% else %>
<%= render 'all_fields' %>
<% end %>
目前,当出现错误时,它总是显示'all_fields' 部分。
是否可以在保留用户在字段中输入的数据的同时显示最初显示的部分,以及错误消息(目前显示在文本字段下,因为我使用的是 simple_form)?
------------更新------------
当我在:edit 中添加puts params[:view] 时,它会在控制台中返回close。
当我在:update 中添加puts params[:view] 时,它不会在控制台中返回任何内容。我还尝试将@view = "close" 更新。当我这样做时,出现错误后会显示正确的视图。
Parameters: {"utf8"=>"✓", "authenticity_token"=>"f7JoOucLkUlOyt31NW9HaJIWyXKVSu7q91kspfZYjgdF3hJneRKapnCPtgJn+R5NOC5nDhNvkB8PzzHNs9aLkg==", "diary"=>{"last_date"=>"", "notes"=>"hi, hi!", "update_type"=>"close", "view"=>"close"}, "reason_list_select2"=>"going on vacation", "season_reason_list_select2"=>"", "temp_reason_list_select2"=>"", "area"=>[{"id"=>"4", "action"=>"Not Applicable"}], "commit"=>"Close Diary", "id"=>"27"}
【问题讨论】:
标签: ruby-on-rails ruby