4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

登録画面と編集画面で共通のviewを使用する時に、ボタンのラベルとかをアクションに応じて出し分けする

Posted at
ja.yml
ja:
  action:
    new: '登録'
    edit: '編集'
new.html.erb
<%= render 'form' %>
edit.html.erb
<%= render 'form' %>
form.html.erb
<%= t "action.#{controller.action_name}" %>
<%# アクションがnewの時は「登録」、editの時は「編集」が出力される %>

provideメソッドやlazy lookupを使うよりこっちの方がすっきり書ける。
書いてて思ったけど、共通画面に限らず一律これにすればコピペが捗るかも?

show.html.erb
<%= t "action.show" %>
↓
<%= t "action.#{controller.action_name}" %>
index.html.erb
<%= t "action.index" %>
↓
<%= t "action.#{controller.action_name}" %>
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?