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.
Related
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.
So my form partial is loaded in my div id="secondary", which is hidden on first page load.
When the user hits a button with a class called toggleSidebar, then the _form.html.erb is shown.
I have overridden the partial to display a new form (even if update is pressed) when a user is not logged in like this:
<%= simple_form_for(Post.new, html: {class: 'form-horizontal' }) do |f| %>
As opposed to the regular version that looks like this, and is included in an if statement on this same partial:
<% if current_user and current_user.has_any_role? :editor, :admin %>
<%= simple_form_for(#post, html: {class: 'form-horizontal' }) do |f| %>
The real issue is in my view, when someone goes to Update, this is what happens when the user is logged out:
<%= link_to "Update", "#", class: "togglesidebar" %>
This is perfect, it executes the CSS and shows the empty form partial perfectly.
However, when a user is logged in, I want it to send the parameter parent_id: #post with the execution of the sidebar being toggled.
This is how it looks with a normal new_post_path view (i.e. the non-sidebar new post view):
<% if current_user %>
<%= link_to "Update", new_post_path(parent_id: #post) %>
<% end %>
This is what my PostController#New looks like:
def new
#post = Post.new(parent_id: params[:parent_id])
end
How do I either pass the params in the regular non new_post_path version, or tackle this another way?
You could probably use a helper method.
Just browse to the 'helper' directory under 'app' folder and create a file similar to [name]_helper.rb
In this file create a module by [name]Helper and declare your helper method in this module.
This module is automatically required by rails.
A small example might help you.
The code in the link_helper.rb under app/helper directory
module LinkHelper
def populate_link(link1, link2, parameter)
if current_user
public_send(link2, parameter)
else
link1
end
end
end
The code in views is
<%= link_to 'update', populate_link('#', 'new_requirement_path',parameter: 33) %>
I'm a bit confused by the question, but I think you may be just need to use a hidden field to pass the parent_id param back?
e.g./
<%= simple_form_for(Post.new, html: {class: 'form-horizontal' }) do |f| %>
<%= f.hidden_field :parent_id, { value: #post.try(:id) } %>
<% end %>
HTH?
I am also a bit confused, but the following railscast might help you. It shows how to embed data in an html-tag. You can probably do it the same way.
railscast-> passing data to javascript
Out of the possibilities there I'd recommend the data-attribute:
<%= simple_form_for,(Post.new, html: {class: 'form-horizontal' }, **data: {post_id: #post.id}**) do |f| %>
<% end %>
I have a selection box on my page, and when I click the submit button I want to take the selection choice to the server as either a post or get variable (I don't think it matters). How do I link this form:
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
to this button:
<%= button_tag(link_to("Get Rates", store_rates_path))%>
You only need to provide the path to the form_for method, to link it to the rates action of your stores controller:
<%= form_tag(store_rates_path, method: "get") do %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select((1980..2014).to_a)) %>
<%= button_tag "Get Rates" %>
<% end %>
In your rates action you can then retrieve the :year parameter passed as follows:
def rates
#year = params[:year]
end
You also need to define the route in your routes.rb file as follows, if you haven't yet:
get 'stores/rate', to: 'stores#rate', as: 'store_rates'
IMPORTANT
Just note that if the rates belong to a specific store, meaning the url is something like stores/1/rate then the above get must be stores/:id/rate, which also means you need to pass the store.id to the store_rates_path in your form: store_rates_path(#store)
You can use rails submit_tag helper
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= submit_tag "Get Rates" %>
<% end %>
OR
If you want to use a link or button to submit your form parameters then you can use some js magic to achieve it:
<%= form_tag store_rates_path, id: "store-form", method: 'get' %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= link_to "Get Rates", "#", id: "store-form-btn" %>
<% end %>
$(document).on("click","#store-form-btn",function(e){
e.preventDefault();
$("#store-form").submit();
});
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
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.