creating a form to delete a resource - ruby-on-rails

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.

Related

Rails Nested routes undefined method

I keep getting an
undefined method 'orders_path' for #<#<Class:0x007faefee50a88>:0x007faefee414e8>
when navigating to my new order path url /users/1/orders/new
Been stuck on this, not sure what the deal is.
Routes:
devise_for :users, :path => 'accounts'
resources :users, only: [:index, :show] do
resources :orders
end
root index:
<%= link_to "Create an Order", new_user_order_path(current_user.id) %>
form:
<%= form_for([#user, #order]) do |f| %>
<%= f.hidden_field :user_id %>
<div class="form-group">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, class: "form-control" %>
</div>
<% end %>
new:
<h1>New Order</h1>
<%= render 'form' %>
<%= link_to 'Back', user_orders_path(#user) %>
When you write this:
<%= form_for(#order) do |f| %>
Rails is looking for orders_path, but you don't really have such a route as you have defined your orders resource nested under the users resource. So, you have to pass both: #user and #order to the form like this:
<%= form_for([#user, #order]) do |f| %>
# your code goes here
<% end %>
If you write the form this way, then Rails will look for this path: user_orders_path which will match your defined route and will work and you won't get this error anymore:
undefined method 'orders_path' for #<#<Class:0x007faefee50a88>:0x007faefee414e8>
So, this will fix your current issue. But, there is another issue in your new.html.erb file where you currently have this:
<%= link_to 'Back', user_orders_path %>
You have to pass the #user as an argument to this method:
<%= link_to 'Back', user_orders_path(#user) %>
otherwise, it will fail again.
Hope this helps to solve your problems.
Since orders is nested on top of users, the form_for declaration will need to include both the user, and the order objects.
<%= form_for([#user, #order]) do |f| %>
Your new template will also need to have a reference to the given user.
<%= link_to 'Back', user_orders_path(#user) %>

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?

How to implement a destroy confirmation form with simple_form?

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

form_tag routing missing required keys: [:id]

I have a custom method buy inside the order controller
I've declared a special route with the following: which is sell_order_path
resources :orders do
post 'sell', on: :member
end
And here's my form tag
<%= form_tag(sell_order_path, :params => params.merge(:id => 5, :stock_symbol =>'test') ) do %>
<%= label_tag :stock_name, 'Buy Quantity' %>
<%= text_field_tag :stock_name, params[:quantity] %>
<%= submit_tag "Buy", class: "btn" %>
<% end %>
When I click on the submit button, it says
No route matches {:controller=>"orders", :action=>"sell"} missing required keys: [:id]
Any idea why? I need to pass in a total of 3 parameter to my orders#sell
id
stock_symbol
quantity
Try this:
<%= form_tag(sell_order_path(5)) do %>
<%= hidden_field_tag :stock_symbol, 'test' %>
<%= label_tag :stock_name, 'Buy Quantity' %>
<%= text_field_tag :stock_name, params[:quantity] %>
<%= submit_tag "Buy", class: "btn" %>
<% end %>
The error occurs because you need to pass appropriate object id to your url helper if it routes to resource member action. Also, I moved stock_symbol to hidden field inside your form.

link submit to another page

I have this form:
in header of my website:
<% form_tag request.path, :method => 'get' do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search User", :name => nil %>
</p>
<% end %>
controller:
def index
#title = "All users"
#users = User.paginate(:page => params[:page])
#users1 = User.simple_search(params[:query]).all
end
model:
acts_as_simply_searchable :columns => [:name, :email]
view
<%= will_paginate %>
<ul class="users">
<%= render #users1 %>
</ul>
<%= will_paginate %>
displays a user
I want to link the submit button to index.html.erb (i have assigned a path in routes.rb). So that the user can look at the search results
You don't do this at the submit button, but at the form_tag URL (the first parameter). It would be something like this:
<% form_tag users_path, :method => 'get' do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search User", :name => nil %>
</p>
<% end %>
Simply change request.path to the desired path.
I think you are missing the '=' sign and string interpolating the 'request.path'
This Worked for me:
<%= form_tag "#{request.path}", method: "get" %>
...the rest of your code ...
<% end %>

Resources