Ruby on Rails submit button - ruby-on-rails

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.

Related

Changing submit button text value per form

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 %>

display text best_in_place gem

I'm using the best_in_place gem for in place editing. I have this around a link that I want editable, but what's displayed to the user should be "Edit Link".I want to be able to edit the link when this field is clicked on, but I want what's displayed to the user (the anchor text) to be "edit link". I tried using "display_with" and "value" but neither has worked.
It's currently displaying the value of the database field which is the URL: https://plus.google.com/+SteveQuatrani/posts
<%= best_in_place c, :google_maps_url, value: "Edit Link" %>
Is there a way to do this? Thank you in advance for your help!
display_with requires a helper or proc, so try giving it one:
<%= best_in_place c, :google_maps_url, display_with: Proc.new {"Edit Link"} %>

Auto click a link in ruby on rails

Hi I am working in ruby on rails,
I want to auto click on
<%= link_to 'Referesh', :onclick => 'window.opener.location.reload(true)' %>
when I submit the form by
<%= f.submit 'Save' %>
My question is how I can perform auto clicking on link_to when I submit the form by f.submit.
Both are in the same form.
I am not sure why/what you want to do.. the question is not clear. However, you can override the onsubmit() and do something there.
If you're using JQuery, you can trigger the click event on a link with something like this:
$("a#my_link").trigger('click');
If you're using Prototype, you should include event.simulate.js
I've used this several times and it works like a charm. It allows you to manually trigger native events, such as click or hover:
$('a#my_link').simulate('click');

How can I use radio buttons properly in Rails

I am doing my first project using Ruby on Rails and need to display a set of radio buttons. It should behave exactly like a selection list. For usability reasons, I need it in a radio buttons format.
In my project, I use the collection select which also allows me to display on the edit page as follows:
select('project','project_type_id',#project_types.collect{|project_type|[project_type.name,project_type.id]}) <br>
I need something exactly like this, (especially the ability to display the selected value in the edit page) but using radio buttons.
I did a Google search and read the entire Rails guides on radio buttons but I can't find the answer.
How can I do this?
I suppose you can do it like this in your view
<% #project_types.each do |project_type| %>
<%= radio_button("project", "project_type", project_type.name) %> #assuming you have a name attribute on project_type
<% end %>
If you want a particular radio button to be checked then you can pass the checked option like so
<%= radio_button("project", "project_type", project_type.name, {:checked => true}) %>

how to use variable name with image tag

my controller uses code like this:
if params[:commit] == "Submit"
this used to work fine when I just had buttons. however, now I am using images as buttons like below:
<%= image_submit_tag 'butons/Add-08.png', :class => 'image-button-submit' %>
How can I pass the commit variable with value Submit along with this image_submit_tag?
KandadaBoggu is right, but that will (in most browsers) give you a params['commit.x'] and params['commit.y'] rather than just params['commit'].
If there's only the one button on the form (and you're posting to the same action that renders the form) you can do if request.post? instead, but that's only going to work if there's just one button, ie: only submit, not submit and cancel.
As per the image_submit_tag documentation you can pass any HTML options. I haven't tested this but the following code should work.
<%= image_submit_tag 'butons/Add-08.png', :name =>"commit", :value =>"Submit" %>
I would try passing it as a hidden field inside of the form you're submitting.
hidden_field_tag "commit", "Submit"

Resources