Tips
# .zip
[A,B].zip([hoge,fuga]) #=> [[A,hoge], [B,fuga]]
[A,B].zip([hoge,fuga]).to_h #=> { A=>hoge, B=>fuga}
Thinreports_Editor用 の 転置配列. 引数はどちらも配列状態でもらう。key部分は、可変長引数で羅列で第2引数でもらう。
# .transpose
def replaceArray(valAry, *keyAry)
newAry = [keyAry,valAry].transpose
h = Hash[*newAry.flatten] #[A,B],[hoge,fuga] => [A=>hoge, B=>fuga]
end
<% # .erb
ryuijikou2_3_data= ["装置","使用機器","管理"]
ryuijikou2_3_data.each do |v|
p params[:ryuijikou2_3]
%>
<%= f.radio_button :ryuijikou2_3, "#{v}" %>
<label for="ccnumber"><%= v %></label>
<% end %>
# pdf Thinreports_Editor
ryuijikou_data = {"ryuijikou2_3a"=>"装置","ryuijikou2_3b"=>"使用機器","ryuijikou2_3c"=>"管理"}
ryuijikou_data.each do |k,v|
if v == report["ryuijikou2_3"]
page.item(k)
else
page.item(k).hide
end
end
check debug
#controller
def hoge
render plain: report.errors.inspect
end
「Rails5 バリデーションエラー URL変わる」は仕様? 回答参照
Reloadするとエラーになる時の解決方法
解決: JavaScriptの力技
# view
<% if post.errors.any? %>
<script type="text/javascript">
$(document).ready(function() {
history.pushState('', '', location.href + '/new')
});
</script>
# パラメーターが変動する場合: ?id=2 など
<script type="text/javascript">
$(document).ready(function() {
history.pushState('', '', location.href + '/new' + document.referrer.split("new")[1] )
});
</script>
<% end %>
render
# controller
render :edit
# view
<% if @hoge.errors.messages[:end_date].present? %>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<%= @hoge.errors.messages[:end_date][0] %>
</div>
<% end %>
if URL changes and loses the /edit. when it reload ERROR.
http://localhost:3000/preferences/1/edit
http://localhost:3000/preferences/1
redirect_to !保存されたり、APIにpatchは投げるみたい。。
# controller v1
def update
..
flash[:notice] = "error!!"
redirect_to :action => :edit
end
# controller v2
def update
report = Hoge.new(hoge_params)
..
flash[:notice] = report.errors.messages[:end_date][0]
redirect_to action: :edit
end
#view
<%= flash[:notice] %>
#bootstrap version1
<% if flash[:notice] %>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<%= flash[:notice] %>
</div>
<% end %>
#v2
<% if @foo.errors.messages[:end_date].present? %>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<%= @foo.errors.messages[:end_date][0] %>
</div>
<% end %>