Changing submit button text value per form - ruby-on-rails

I'm rendering a form on a few different pages, such as: edit, new, show. Now on the submit button it says: "update post" and "create post". I know that there is a way to add custom text to the buttons <%= f.submit "Text" %>. But because i'm rendering the form it shows that value on each of the pages. So i'm wondering if there is a way where i can add the custom text value per page, without having to copy the whole form itself.

You can use the Rails built-i I18n (internationalization) module:
# config/locales/en.yml
en:
helpers:
submit:
post:
create: "Toss me bottle to the sea!"
update: "Arg! Now she be a spellin' straight."
You need to remove the text from submit button in order for it to be translated:
<%= f.submit %>

Related

Put a relative link button in Rails Action Text

In .html.erb <%= link_to 'the listing', "years/1846", class: "btn btn-primary" %> works. How can I put this in an Action Text field and have it be a link?
It will be hard coded to a specific record as shown. years is a table, model, etc in my app. Doesn't have to be a Bootstrap button, but does need to look like a link, so will need to be styled.
You can add the raw html output of your link_to code directly to the ActionText field and it should work. Something like roster.description += ‘the listing’ etc (obviously roster and description are placeholder names for the ActiveRecord object and the ActionText field name)

Rails: change the contents of view when click the different link

Is it possible to change the contents of view when click the different link?
I have two links.
<%= link_to "Add event", xxx_path %>
<%= link_to "Add place", xxx_path %>
I'd like to display <%= f.text_field :detail %> on the view of xxx_path when I click Add event, and not to display it when I click Add place.
It would be appreciated if you could give me advice.
If you need to pass additional information along with a GET request you would use query parameters.
example:
/cities?near=london
/users/1/orders?status=pending
You can pass any hash keys with path/url helpers to generate a path/url with any arbitrary query attached.
cities_path(near: 'london')
user_orders_path(user_id: current_user.to_param, status: 'pending')
However your example makes very little sense as Event and Place most likely are different resources and should have their own routes and controllers.

Ruby on Rails submit button

How can I change submit button on form to text?
<%= f.submit "Text" %>
I know how to change text on button but I need to change a way to submitting.
You can press on link, for example Submit and submit.
You don't say what ruby version you are using. But try this
<%= submit_tag "Text" %>
Note that there is no form helper here, so no "f." in front
In order to change the submitting of a form to be triggered by a link as opposed to a submit button, you must use javascript instead of raw generated HTML only.
See similar answers on StackOverflow but a summary of a possible solution in jQuery (which comes baked into recent versions of Rails) would be as follows:
$('#submit_link').click(function(){
$('form').submit();
return false;
});
Update: This should do it using HAML notation in Rails 2.3.x:
- form_for(#object) do |f|
= f.submit 'Update Or Whatever'
You could also try "button" tag or use an image link that looks like a button.

Ruby on Rails 3, forms, ajax, nested, in-place editing, one-by-one. Best practice

Assume, that I have a complex nested form with many fields.
I want to edit its fields one-by-one in ajax way, so that every time I see form - it is in 'show' style (without fields to change information), but with possibility to switch any particular field or group of fields to 'edit' mode with it's own 'save' or 'update' button.
Solving this kind of problem, I ended up with two ways:
Extended use of Ryan Bates' complex-form-examples.
The disadvantage of this way is that every field (or group of fields) requires it's own code (i.e. javascript: 'insert_fields'), which renders corresponding 'edit' style form, so it results in page is overwhelmed by javascripts.
Second - is unified procedure of loading corresponding edit partials through ajax through special controller action (i.e. get_partial), which "render :do updates" given field's area by 'edit' form.
For given field or group of fields i have partials for 'edit' and for 'show'. When i need to switch that field to edit mode i send request (link_to ...,'/.../get_partial?partial=foo',:remote => true) with necessary params by ajax, and some controller#action renders that partial by javascript.
I think that second approach is better one, but I can't figure out how optimize it the best.
Are there any more elegant solutions to this problem?
What if you generated a normal 'edit' form (with all the nested fields, etc), and then had the javascript hide the fields and add the text of the field and an edit link next to it. So for example say your form looks like:
= form_for #foo do |f|
= f.text_field :name
and your javascript would do this to it (1):
= form_for #foo do |f|
= f.text_field :name, :class => "hide"
<the value of the field here>
= link_to "edit", "#"
then make your javascript add a click event to the edit links that, when clicked, does:
= form_for #foo do |f|
= f.text_field :name
= f.submit "Save"
then you'd need more javascript that makes the save button submit the form (ajax), and go back to (1) above

Using i18n to change text between update and create

I have a model that uses the same form for both create and update controller actions.
Nothing special there. I have the submit button changing its text based on which action using en.yml
en:
helpers:
submit:
location:
create: "Add to map"
update: "Save changes"
How would I go about changing a body of text on this form, according to the appropriate action?
Eg, if it was create,
<h1>Create new location</h1>
and if update
<h1>Update location</h1>
You could simply add the text to your en.yml file:
en:
create_new_location: 'Create new location'
update_location: 'Update location'
And then use the following in your view (create/update)
<h1><%= I18n.t(params[:action] == 'create' ? 'create_new_location' : 'update_location') %></h1>
Or, if you were using new/edit:
<h1><%= I18n.t(params[:action] == 'new' ? 'create_new_location' : 'update_location') %></h1>
You could also make your translation based on the action and embed that in the translation text, by doing something like this:
I18n.t "location.action.#{params[:action]}"
I would recommend against this though, because it's harder to tell what text you are actually translating.

Resources