When I try to use button_to for link smthing - rails add to that button tag and my design is broke. How i can fix that? For example:
<%= f.submit %>
<%= button_to 'Back', resumes_path %>
I know that i can use somthing like link_to, but in this situation i need to use button_to. Thanks.
Please create any class name or div id and write button css.
<%= link_to 'Back', resumes_path , :class => "button_css" %>
and write style,
.button_css
{
// your button style...
}
Edit : Try this
<% content_tag :button :type => :submit do %>Button<% end %>
This will create a button, you can then add onclick event to redirect the page.
Related
In my app I want to set a search form with two buttons. Each one of those buttons should send request to separate controller. Something like this:
<%= form_tag(products_path, method: :get) do %>
Search Field <%= text_field_tag :q %>
<br>
<%= submit_tag 'First controller' %>
<%= submit_tag 'Second Controller' %>
<% end %>
Is this even possible? Or rails just impose on developer an "one form - one controller" way?
Use JavaScript to change the form URL based on the button clicked.
<%= form_tag(first_controller_path,id: 'search-form', method: :get) do %>
Search Field <%= text_field_tag :q %>
<br>
<button type='submit' id="form-submit-button">First Controller</button>
<button type='button' id="second-controller-button">Second Controller</button>
<% end %>
<script>
$(function(){
$("#second-controller-button").on("click",function(e){
e.preventDefault();
var form = document.getElementById('search-form');
form.action = '<%= second_controller_path %>' ;
form.submit(); // Or you could also try document.getElementById("form-submit-button").click();
})
});
</script>
HTML5 added some attributes to INPUT and BUTTON elements. One of them is formaction so you can set the action triggered by each button independantly and it overrides the default form action.
<%= form_tag(products_path, method: :get) do %>
Search Field <%= text_field_tag :q %>
<br>
<%= submit_tag 'First controller' #triggers the default action %>
<%= submit_tag 'Second Controller', formaction: another_path %>
<% end %>
https://www.w3schools.com/tags/att_input_formaction.asp
You already defined form_tag with products_path through which the controller method is already defined.
So answering your question, you can't pass send requests to two different controllers with one form.
If you want to pass some status with buttons try adding some attributes to the buttons and differentiate them inside the controller.
I have a method like this:
gamer.rb
def approve_gamer(type)
where(type: type).last.update(status: 'approved')
end
and I want to make a button for each type to call approve_gamer('type').
Should it be button_to [:approve_gamer, gamer] or link_to ... class: "btn btn-default"? How do I pass the type parameter there?
I'd use the button_to helper since I wouldn't call data-changing methods through GET.
button_to creates a form around the button and sends the data through POST.
More to read here: http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
You can also set the form yourself:
<%= form_for gamer, :url => { :action => 'approve' } do |f| %>
<%= f.hidden_field('type', :value => type_goes_here) %>
<%= button_tag 'Approve', class: 'btn btn-default' %>
<% end %>
That method you call should be in your gamers_controller though.
Also you could simply call the edit method for gamer and set the parameters.
And if you call the method approveyou have to set the route, too.
Here's my form:
<%= form_for #asset do |f| %>
<%= f.check_box :remove_picture %>
<%= f.submit "Remove" %>
<% end %>
How could I just make this one button that does :remove_picture and submit? Thanks
Check out the API dock for the form_for helper:
http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
You can force the form to use the full array of HTTP verbs by setting
:method => (:get|:post|:put|:delete)
So your code might look like
<%= form_for(#asset, html: { method: :delete }) do |f| %>
<%= f.submit "Remove" %>
<% end %>
You could change the checkbox to a hidden field on the form...
If it were me, I'd look at something like button_to and handle this via AJAX on the controller. This way the button would call a controller action, say remove_picture and return a JS response which could update your view.
Example:
button_to([remove_picture, #asset], { method: :delete })
Note: method: :delete may not be needed - depends on your routes.
I've been toying with using icons instead of standard buttons. I like the look of it and occasionally a icon is clearer than a labelled button.
While I found it easy to implement for link_to calls;
<%= link_to raw('<i class="icon-exclamation-sign"></i>'), '#' %>
I am struggling to achieve the same result when nested with form_for (or simple_form_for). Is there a way to use an icon for a submit or input when working with the form?
Here are some example forms from my code using the standard buttons:
<%= simple_form_for :present, url: present_path(list_item), method: 'put' do |f| %>
<%= f.hidden_field :purchased, value: "1" %>
<%= f.submit "owned" %>
<% end %>
<%= form_for :present, action: 'new', url: presents_path do |f| %>
<%= f.hidden_field :purchased, value: '0' %>
<%= f.submit "List", class: "btn btn-mini" %>
<% end %>
I've tried a few methods but none of yielded just the icon acting as a button - most render the button with either the raw HTML as its contents or the icon within the button.
The end result I'd like to be able to implement is just the icon acting as a click-able item (similar to the link_to result).
you probably want to look at image_submit_tag http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-image_submit_tag
How I can submit form with link on correct rails 3 format?
Thanks.
<%= form_for #post do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<p><%= f.submit %></p>
<% end %>
My code sample.
For people who came here via Google, I have an improvement on Zequez's answer. Instead of the method that he gives, add this method to the application helper instead:
def link_to_submit(*args, &block)
link_to_function (block_given? ? capture(&block) : args[0]), "$(this).closest('form').submit()", args.extract_options!
end
Then, as Zequez stated, for simple links you can just do this in your view:
<%= link_to_submit 'Submit Form' %>
...and for more complicated buttons you can pass HTML options and a block to be used inside the link. If you use Twitter Bootstrap, for example, this lets you add CSS classes, formatting and icons:
<%= link_to_submit( class: 'btn btn-primary' ) do %>
<strong>Submit</strong> the Form <i class="icon-arrow-right"></i>
<% end %>
The JQuery code will work as long as the link is a child of the form (that is, as long as link_to_submit is called from somewhere within the form_for block).
"Correct" is a tricky word in this context ;) . One could ask why you're not just taking a button element and make it look like a link?
Anyways — you can't achieve this with plain HTML (at least not to my knowledge). With a Javascript framework like e.g. jQuery you could simply do something like this:
$('a').click(function(){
$('form').submit();
return false;
});
Rails 2.3.x had a link_to_remote helper which let's you specify a :submit parameter (= DOM element's ID, default is the parent form). So you were be able to write:
link_to_remote 'submit', :url => {…}, :submit => "my_form"
But with Rails 3's push to UJS, this helper is gone.
You can add the following to the application helper:
def link_to_submit(text)
link_to_function text, "$(this).closest('form').submit()"
end
Then inside your view files you can just call
link_to_submit 'Submit Form'
And the link must be child of the form.
With jquery, this one-liner will work fine for a simple form.
<%= link_to t("translate.submit"), "#", class: "make it beautiful", :onclick=>"$('form').submit()" %>
Of course you don't really have to use jquery, just finding the dom element for your form will work fine as well.
<%= link_to t("translate.submit"), "#", class: "make it beautiful", :onclick=>"document.getElementById('your_form_id').submit()" %>
This way you don't use any ajax, just plain form submit.
In Rails 3, the link_to_remote helper is gone, but it's replaced with
link_to 'submit me', url_for(#post), {:remote => true, :class => 'submit_me'}
In your case, you likely want your form to do the AJAX, like so:
<%= form_for #post, :remote => true do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<p><%= f.submit %></p>
<% end %>
With a companion link:
link_to 'submit me', '#', :class => 'submit_me'
Then, in an .js file included in the page body:
$('.submit_me').click(function() {
$('form').submit();
return false;
});
The idea is that anything more complicated than turning a link or form into an ajax request should be done with the jQuery callbacks, as listed here:
https://github.com/rails/jquery-ujs/wiki/ajax
And if you want to really get into interactive AJAX requests, go here for a great 2-part article on it.