Assume I have a model like this:
class Item < ApplicationRecord
end
And in items#new
or items#edit
, in app/views/items/_form.html.erb
:
<%= f.submit %>
This will generate a submit button named "Create Item" or "Update Item" depending on the route.
Now I want to use an alternative name for the generated button text (and only here!), like "Create Article" and "Update Article", while keeping the name Item
everywhere else in the code, so I can still use items_url
, ItemsHelper::some_method
etc.
Using <%= f.submit "Text" %>
is not an option since it won't keep the difference between Create %{model}
and Update %{model}
when the same form is rendered in different views.
I have made the following attempts without success:
# 1 - no difference observed
class Item < ApplicationRecord
def display_name
"Object"
end
end
# 2 - undefined method `name' for "Artlcle":String
class Item < ApplicationRecord
def model_name
"Object"
end
end
# 3 - undefined method `object_path'
class Item < ApplicationRecord
def model_name
ActiveModel::Name.new Item, nil, 'Object'
end
end
This isn't a good solution, either, as it defeats Rails' DRY principle.
<% if determine_controller %>
<%= f.submit "Some text" %>
<% else %>
<%= f.submit "Some other text" %>
<% end %>
How do I achieve this goal? Is it possible without rails-i18n
?
Additional question: Does the solution in your answer also work on Rails 5?