Question

I generated a Stories scaffold and now when I'm in stories/new, the button that I click says "Create Story." How do I change the value of that to say something else, like "Create Tale"?

I've gone into the stories/new.html.erb and also the stories/edit.html.erb, but all that is there is

<%= render 'forms' %>

When I head to stories/_form.html.erb there is a

<div class="actions">
<%= f.submit class: "btn" %>
</div>

I know you can put <%= f.submit "Create Tale", class: "btn" %>, but if I do that it will say "Create Tale" for both creating and updating. How could I make is also say "Update Tale" for when I'm in stories/edit?

Was it helpful?

Solution

For giving it a name, try:

<%= f.submit class: "btn", name: "Create Tale" %>

For naming it differently depending on the action. What I normally do is pass the button-name into the form-template from the action-view. Eg in new.html.erb:

<%= render partial: 'form', button_name: 'Create Widget' %>

in edit.html.erb:

<%= render partial: 'form', button_name: "Update Widget" %>

in form:

<%= f.submit name: button_name %>

The alternative is not to put the submit buttons in the "form" partial, but to keep them in the action-views eg for "new.html.erb"

<%= form_for @widget do |f| %>
   <%= render partial: 'form' %>
   <%= f.submit name: 'Create Widget' %>
<% end %>

for "edit.html.erb"

<%= form_for @widget do |f| %>
   <%= render partial: 'form' %>
   <%= f.submit name: 'Update Widget' %>
<% end %>

NOTE: code not tested and probably buggy, but you get the drift...

OTHER TIPS

work directly on the form (partial _form)

<%= f.submit "Submit", class:"btn"%>

For Rails 5, change:

in new and edit views

and the same way. Then in form use: instead of
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top