How to implement a destroy confirmation form with simple_form? - ruby-on-rails

I'm using Ruby on Rails 4 with simple_form. On my delete page, I would like to display a form confirming the user's decision to destroy the resource in question. Here is the code I am using:
<h2>Delete Page</h2>
<p>Are you sure you want to delete <%= #journalEntry.title %></p>
<%= simple_form_for(#journalEntry, :action => 'destroy') do |f| %>
<%= f.button :submit %>
<% end %>
However, this is getting processed by the update action instead (my server console shows that it is being sent as a PATCH request).
I also tried amending that code to the following, but with the same result:
<%= simple_form_for(#journalEntry, :url => {:action => 'destroy', :id => #journalEntry.id}) do |f| %>
<%= f.button :submit %>
<% end %>

Add :method => :delete option with simple_form_for :
<%= simple_form_for(#journalEntry, :method => :delete) %>
<%= f.button :submit %>
<% end %>

Related

Rails - method put input not sent with params

When updating an object, we should :method => :put to override the post in the form as the following:
<%= simple_form_for #task, :url => update_task_url, :method => :put do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :description %>
</div>
<div class="form-actions">
<%= f.submit 'Update Task'%>
</div>
<% end %>
<%= link_to 'Back', tasks_path %>
In the controller before making the request, I tried to render the params to check everything is correct as follow:
render text: tasks_params
uri = URI.parse("http://localhost/tasks/public/api/tasks/"+params[:id])
response = Net::HTTP.post_form(uri, task_params)
render text: response.body
but I get the following without the _method attribute:
{"name"=>"Task#1", "description"=>"lorem ipsum"}
and as a result the request is not successful on the server side.
What am I missing here exactly?
You should post to the desired controller action like so
<%= simple_form_for #task, :url => update_task_url, :method => :post do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :description %>
</div>
<div class="form-actions">
<%= f.submit 'Update Task'%>
</div>
<% end %>
I would add a debugger break point in the update action in your TasksController just to make sure you are hitting the right route.

How to use session_path with simple_form?

I'd like to adapt form_tag to simple_form
<%= form_tag sessions_path do %>
<%= label_tag :password %>
<%= password_field_tag :password %>
<% end %>
it works fine, I can log in normally.
<%= simple_form_for(sessions_path) do |f| %>
<%= f.error_notification %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
Here I get
No route matches [POST] "/sessions/new"
In my routes.rb I have following line
get 'login', :controller => 'sessions', :action => 'new'
Could someone help me fix it?
form_for (and therefore simple_form_for) assume that you have a model and are POSTing or PATCHing to its resource route.
But you don't have a model and that route is a GET route, so you need to pass in some options to simple_form to make it work.
This should do it:
simple_form_for(sessions_path), method: :get
But there's also this answer which should help (not the accepted one, the second one):
Does form_tag work with Simple_form?

correct syntax for form_for url in rails

This is probably simple, I'm still coming to terms with rails syntax. What is the right syntax to pass the address_id in the url for form_for to a modified route?
This is the form - note the "address_id parameter"
<div class="one_fourth floatcenter">
<%= form_for address, :url => edit_address_path(:id => address.id), :method => :get do |f| %>
<%= content_tag(:button, :class => 'btn btn-inverse') do %> Edit Address
<% end %>
<% end %>
</div>
And this is the route I've configured:
get "edit_address/:id" => "member/addresses#edit"
Id is not being passed to the controller for some reason...
form_for address should be enough if address is a persisted object, but if it's not enough, then form_for address, url: edit_address_path(address) is what you want.
This is very simple. In place of url, you put your post method route:
<%= form_for(#post, url: super_posts_path) do |f| %>
...
<% end %>
You also call by action
<%= form_for #friend,:url=> { action: "create_friend"} do |f|%><br>
<%= f.label :u_from %>
<%= f.text_field :u_from %>
<%= f.label :u_to %>
<%= f.text_field :u_to %>
<%= f.submit%>
<% end %>

The Mysterious Order-Dependent Checkbox State Array Controller Transmission Anomaly

Here's all of the relevant view code:
<%= form_for(:object) do |form| %>
<p id="batch_ops">
<b>Objects</b>
<%= submit_tag "Do This" %>
<%= submit_tag "Do That",
:confirm => "Really do that?" %>
<p id="capture" float="left">
<%= button_to_function "Operation_1", \
"Element.remove('batch_input_area'); Element.show('get_op1_params')" %>
<%= text_field_tag "op1_save_path", "_op1_full_dir_path_" %>
<%= button_to_function "Operation_2", \
"Element.remove('batch_input_area'); Element.show('get_op2_params')" %>
<%= text_field_tag "op2_save_path", "_op2_full_dir_path_" %>
</p>
<div id="batch_input_area" style="display:none;">
</div>
</p>
<ul id="object_list">
<% #objects.each do |c| %>
<li id="current_object" style="horizontal-align: left" width="auto"><b>
<%= hidden_field_tag('seen_objects[]', c.id) %>
<%= check_box_tag 'chkd_objects[]', c.id, c.object_checked? %>
<%= c.object_address %>
<% if c.data1? %>
<%= c.data1 %>
<% else %>
<%= "_____________" %>
<% end %>
<% end %>
</ul>
<div id="get_op1_params" style="display:none;">
<%= form_tag(
:action => 'index',
:remote => true,
:html => {:id => 'op1_form'}
) do %>
Options: <%= text_field :op1params, :op1_params, :value => 'freq=30 run=1 stop=0' %>
<%= submit_tag 'Op1 Run', :confirm => 'Run Operation_1 test on checked objects?' %>
<%= submit_tag 'Op1 Status' %>
<%= submit_tag 'Op1 Stop', :confirm => 'Stop Operation_1 test on checked objects?' %>
<%= link_to "Cancel", {:action => "index"} %>
<% end %>
</div>
<div id="get_op2_params" style="display:none;">
<%= form_tag(
:action => 'index',
:remote => true,
:html => {:id => 'op2_form'}
) do %>
Options: <%= text_field :op2params, :op2_params, :value => 'freq=30 run=1 stop=0' %>
<%= submit_tag 'Op2 Run', :confirm => 'Run Operation_2 test on checked objects?' %>
<%= submit_tag 'Op2 Status' %>
<%= submit_tag 'Op2 Stop', :confirm => 'Stop Operation_2 test on checked objects?' %>
<%= link_to "Cancel", {:action => "index"} %>
<% end %>
</div>
Now here's the twist: Over on the controller, the Operation_1 handler gets the checkbox presence array seen_objects[] and the activated elements of the checkbox state array chkd_objects[] while the Operation_2 handler does not. If I reverse the order of the get_op1_params and get_op2_params form_tag divs shown last above then the Operation_2 handler gets these checkbox arrays but the Operation_1 handler does not.
My question is simply: Why?
I've long since lost count of the different things I've tried to get around this, which included additional hidden_field_tags within the form_tags. Ultimately I gave up and used a far less elegant submit_tag with a text_field_tag in place of the Operation_2 button_to_function and its associated form_tag but still do not understand why I could not make both of these operations work using button_to_function as illustrated above.
If someone can get this to work, please post your solution here. Any suggestion short of a working solution I've probably already seen and tried.

creating a form to delete a resource

I have a subscriber resource (mailinglist) and want to make a unsubscribe form. I created a remove view with a form
<%= form_for(Subscriber.new, :action => :delete) do %>
email: <%= text_field_tag :mail %>
<%= submit_tag "Sign out" %>
<% end %>
I try to call the delete method of the controller but instead the edit action gets called.
The problem is that the RESTful routes to the destroy action needs an :id of the resource to be deleted and since you use Subscriber.new as a source for creating the form, it cannot create an appropriate url to post to.
You can go around this by using routes like this:
<% form_for(:subscriber, :url => subscriber_path("email"), :html => {:method => :delete}) do %>
email: <%= text_field_tag :mail %>
<%= submit_tag "Sign out" %>
<% end %>
Note that you have to edit subscriber_path to your own routing but by using "email" as an identifier you make sure that no faulthy :id is being passed to the controller and you can use the email to find the correct model to destroy as I think was what you wanted to do.
<%= form_for #subscriber, :method => :delete do %>
email: <%= text_field_tag :mail %>
<%= submit_tag "Sign out" %>
<% end %>
This should do the trick.

Resources