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.
Related
I've tried a lot to delete a record from my DB, but I have failed. Instead of deleting the selected value it is inserting a new empty record into my db. Please help me out.
My Controller is:
class CatvaluesController < ApplicationController
...
def destroy
#catvalue = Catvalue.find(params[:id])
#catvalue.destroy
redirect_to catvalues_path
end
....
end
and my form is:
<%= form_for(#catvalue) do |f| %>
<%= f.collection_select :id, #catvalues, :id, :v_name, {}, class: 'drop-down'%>
<%= f.submit 'Destroy', :confirm => 'Are you sure you want to destroy your account?' %>
<% end %>
form_for by default takes post method
<%= form_for #catvalue, :method => :delete do |f| %>
Okay, so I'm now adding url to the form helper, try this one!
<%= form_for #catvalue, :url => "/catvalues/#{#catvalue.id}",:method => :delete do |f| %>
You are submitting a POST request, it will call the create action on your controller hence your empty model.
You have to use the delete http method to call the destroy action on your controller :
<%= form_for(#catvalue, :method => :delete) do |f| %>
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.
hy, i'm trying to update a single field in a user resouce. The field to update is 'locale', it is a string type.
i have triend with a form and it works:
<%= form_for current_user do |f| %>
<%= f.select :locale, [['En', 'en'], ['It', 'it']] %>
<%= f.submit %>
<% end %>
now i'm trying to update the same field using a link or a button:
<%= current_user.locale = 'fr' %>
<%= button_to "update" ,current_user,method: :patch %>
<%= button_to "update" ,user_path(current_user),method: :patch %>
but none of this work.
the problem is the request, infact the web server doesn't recive the current_user parameters:
{"_method"=>"patch",
"authenticity_token"=>"7X6QdD4DGxsXaETT86/8Ut4xyuOICaxirs4IjmZl7jY=",
"locale"=>"en",
"id"=>"1"}
There is no current_users parameters.
i have tried with the link_to but the problem is the same:
<%= link_to "update", current_user ,method: :patch %>
I have no idea. Can you help me?
The fact that you don't get the parameter sent back to server is the expected one since you don't use a form.
In order to pass the parameter back you have to:
use a form (as you did) or
pass the parameter in the url of the button: button_to 'update', user_path(param_name: 'param_value')
In the second case, you will have to search for the appropriate parameter in your action, ex params[:param_name]
I feel like I'm abusing form_for (and simple_form_for) to update a single hidden attribute of a record. Here is an example:
<%= simple_form_for :present, url: present_path(list_item), method: 'put' do |f| %>
<%= f.hidden_field :ordered, value: "1" %>
<%= f.button :submit, "ordered", class: "btn btn-mini" %>
<% end %>
Essentially this presents a single button that a user can press to mark a present as 'ordered'. On the back end, it updates the attribute :ordered to the value 1 for the current list_item.
I feel like this is a cheat because its not a proper form per se. I'd much rather have a single link_to which would when clicked updated the attribute.
I imagine the link_to would need to be method: aware to update the attribute.
Is it possible to replace my simple_form_for with a single link_to?
it is, use
link_to 'Ordered', present_path(list_item, ordered: 1), method: :put
Try using:
link_to 'Ordered', present_path(list_item, present: {ordered: 1}), method: :put
<%= text_area_tag :body_html, "", :class =>"required mceEditor", :id=>"txaEditableContent" %>
This is my code. I want this text_area_tag to be assigned to an object(object is note in my case) just like we use "form_for object" so that the :body_html goes straight into params[:note] on submit. How can I do this?
You have got to set first an instance of Note in the controller corresponding action:
#note = Note.new
Then, in your view put this form:
<%= form_for #note do |f| %>
<%= f.text_area :body_html, :class => "required mceEditor", :id => "txaEditableContent" %>
<%= f.submit "Submit" %>
<% end %>
Now when you click the submit button you should get the right post request as you needed.