How to delete an individual newsletter signup inside a .each loop - ruby-on-rails

I am trying to implement a delete button for each newsletter signup on my Users Show view in an admin section.
What I'm trying now:
<% #news_subs&.each do |news| %>
<div class="vert-flip bot-drop">
<div class="wellington bot-drop sub-well flip-card-inner">
<div class="flip-card-front">
<p class="align-left left hype"><%= news.name %></p>
<p class="align-right right"><%= news.verified %></p>
<div style="clear: both;"></div>
</div>
<div class="flip-card-back">
<p class="newsletter-email"><%= news.email %></p>
<%= link_to "Remove", news, method: :delete,
data: { confirm: "Are you sure you want to..." },
class: "btn btn-xs btn-danger newsletter-remove" %>
</div>
</div>
</div>
<% end %>
But I am getting the following error:
Routing Error
No route matches [DELETE] "/newsletter.2"
I have tried using: <%= link_to "Remove", Newsletter.find(params[:id]), method: :delete, but that just throws the same error as well.
How can I delete the individual news_sub and not navigate away from the page?
Using Ruby 3 and Rails 6.1
Update:
Rails Routes:
newsletter_index GET /newsletter(.:format) newsletter#index
POST /newsletter(.:format) newsletter#create
new_newsletter GET /newsletter/new(.:format) newsletter#new
edit_newsletter GET /newsletter/:id/edit(.:format) newsletter#edit
GET /newsletter/:id(.:format) newsletter#show
PATCH /newsletter/:id(.:format) newsletter#update
PUT /newsletter/:id(.:format) newsletter#update
DELETE /newsletter/:id(.:format) newsletter#destroy
edit_news_verification GET /news_verification/:id/edit(.:format) news_verification#edit
Routes.rb:
get 'newsletter', to: 'newsletter#newsWelcome', as: 'newsWelcome'
post 'newsletter', to: 'newsletter#create'
...
resources :newsletter
resources :news_verification, only: [:edit]

You can fix this by specifying the route rather than just passing the object to your link_to method. Instead of passing news specify the path you need, eg news_sub_path(news).
This should work:
<%= link_to "Remove", news_sub_path(news), method: :delete,
data: { confirm: "Are you sure you want to..." },
class: "btn btn-xs btn-danger newsletter-remove" %>

Related

Create a button to delete a specific record in Rails

I am trying to create a button to delete badges. Except that I do not want to delete the badge, but to delete the subscription to a badge if i can say.
<% #subscribeds.each do |badge| %>
<span class="badge badge-secondary">
<%= link_to 'Delete', controller: 'listdedistribution',id: Listdedistribution.find_by(group_id: badge.id, user_id: current_user.id, origine: "Self-registered").id, action: :destroy, method: :delete, :class => "fas fa-trash-alt fa-lg" %>
<%= badge.name %>
</span>
<% end %>
I get the following error:
ActionView::Template::Error (No route matches {:action=>"destroy", :controller=>"listdedistribution", :id=>18458, :locale=>:en, :method=>:delete}):
Does anybody know how to target a specific records and delete it please?
Thank you
Just check your routes
resources :listdedistribution # in routes
DELETE /listdedistribution/:id(.:format) listdedistribution#destroy

No route matches [GET] "/recipes/1/like" I want a POST request

I've never encountered this problem before. I'm getting this error.
No route matches [GET] "/recipes/1/like"
Here is my routes.rb:
Rails.application.routes.draw do
root 'pages#home'
get '/home', to: "pages#home"
resources :recipes do
member do
post 'like'
end
end
end
Here is my recipes_controller:
def like
#recipe = Recipe.create(params[:id])
Like.create(like: params[:like], chef: Chef.first, recipe: #recipe)
#flash message
flash[:success] = "Your selection was sucessful"
redirect_to :back
end
Here is my html.erb file:
<%= render 'shared/page_title', title: #recipe.name.titleize %>
<div class= "row">
<div class="col-md-4 pull-right center">
<%= gravator_for #recipe.chef, size: 200 %>
<p>
<h5>Brought to you by: <%= #recipe.chef.chefname.capitalize %></h5>
</p>
</div>
<div class= "col-xs-8 col-md-8">
<%= link_to "Edit this Recipe", edit_recipe_path(#recipe), class: "btn btn-success pull-right" %>
<h3><%= #recipe.summary.capitalize %></h3>
<div class="show_recipe">
<%= image_tag(#recipe.picture.url, size: "300x200", class: "recipe-image") if #recipe.picture? %>
</div>
<div class ="well recipe-description">
<p>
<strong> Steps:</strong>
</p>
<%= simple_format(#recipe.description) %>
<div class="pull-right">
<%= link_to like_recipe_path(#recipe, like: true), method: :post do %>
<i class="glyphicon glyphicon-thumbs-up"></i>
<% end %> &nbsp&nbsp&nbsp&nbsp
<%= link_to like_recipe_path(#recipe, like: false), :method => :post do %>
<i class="glyphicon glyphicon-thumbs-down"></i>
<% end %>
</div>
</div>
</div>
</div>
<h5><%= link_to "Return to Recipes Listings", recipes_path, class: "btn btn-warning btn-small" %></h5>
I've explicitly added the HTTP POST request to my html.erb file
%= link_to like_recipe_path(#recipe, like: true), method: :post do %>
but rails is complaining that there is no GET route request, which I never created in my routes because I need a POST request for this particular section of the web app.
Rake routes:
Prefix Verb URI Pattern Controller#Action
root GET / pages#home
home GET /home(.:format) pages#home
like_recipe POST /recipes/:id/like(.:format) recipes#like
recipes GET /recipes(.:format) recipes#index
POST /recipes(.:format) recipes#create
new_recipe GET /recipes/new(.:format) recipes#new
edit_recipe GET /recipes/:id/edit(.:format) recipes#edit
recipe GET /recipes/:id(.:format) recipes#show
PATCH /recipes/:id(.:format) recipes#update
PUT /recipes/:id(.:format) recipes#update
DELETE /recipes/:id(.:format) recipes#destroy
I am honestly lost. It seems that everything is in its right place.
Rails version:
Rails 4.2.5
I've defined the action, created the like model, nested the route under recipes, and explicitly requested for a post HTTP request in the html.erb page.
Any ideas would be great!
Cheers!
Here's the relevant code from my working project with a similar arrangement.
# /views/events/splits.html.erb:
<%= link_to "Add",
associate_splits_event_path(id: #event.id, split_ids: [split.id]),
:method => :put,
:class => 'btn btn-xs btn-success' %>
# routes.rb
resources :events do
member { put :associate_splits }
end
If it's helpful to see it in context, feel free to poke around the repo. Here's a link to the view: https://github.com/SplitTime/OpenSplitTime/blob/master/app/views/events/splits.html.erb
While rails link_to does allow links to make requests with a method other than GET, it is not the normal behavior of links and relies on Javascript to make a form which gets submitted behind the scenes. Rails uses jquery-ujs for this, so make sure you haven't disabled it.
That said, making POST requests is normal behavior for forms, so you could also try using button_to instead, and simply styling the button to look like a link if necessary.
<%= button_to "Like", like_recipe_path(#recipe), method: :delete, like: true %>
Additional Info:
https://github.com/rails/jquery-ujs
http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
Try with
<%= link_to "LIKE", like_recipe_path(#recipe) , method: :post %>
Other option:
<%= link_to '<i class="glyphicon glyphicon-thumbs-up"></i>'.html_safe , like_recipe_path(#recipe , like: 'value' ) , method: :post %>
You need to add a GET route:
resources :recipes do
member do
get 'like'
post 'like'
end
end

Rendering string with erb content on page

I found many posts with this problem, but it's seems like none of them solves my problem. I got this code which i want to render from string:
<%= button_to "/admin/#{contr_name}/#{obj.id}", method: :delete, class: 'btn btn-danger btn-resource-destroy', data: {toggle: 'tooltip'}, title: 'Delete' do %>
<%= icon('trash-o') %> <span class='sr-only'>Delete</span>
<% end %>
I have tried this:
template += "<div class='col-sm-4'>"
template += "<%= button_to \"/admin/#{contr_name}/#{obj.id}\", method: :delete, class: 'btn btn-danger btn-resource-destroy', data: {toggle: 'tooltip'}, title: 'Delete' do %>
<%= icon('trash-o') %> <span class='sr-only'>Delete</span>
<% end %>"
template += "</div>"
ERB.new(template).result(binding)
but i get syntax errors.
How i can fix this?
I would suggest to use partials instead.
First, define the partial at, for example, views/shared/_delete_button.html.erb:
<%= button_to "/admin/#{contr_name}/#{obj.id}", method: :delete, class: 'btn btn-danger btn-resource-destroy', data: {toggle: 'tooltip'}, title: 'Delete' do %>
<%= icon('trash-o') %> <span class='sr-only'>Delete</span>
<% end %>
Then, you can render the partial with the wanted parameters:
render 'shared/delete_button', contr_name: [contr_name], obj: [obj]
Replacing [contr_name] and [obj] with whatever you want those variables to be assigned to.
Or, even better, allow the partial to extract the controller's name from predefined variables, like this:
<%= button_to "/admin/#{controller.controller_name}/#{obj.id}", method: :delete, class: 'btn btn-danger btn-resource-destroy', data: {toggle: 'tooltip'}, title: 'Delete' do %>
<%= icon('trash-o') %> <span class='sr-only'>Delete</span>
<% end %>
Now you only need to supply the obj when rendering it.
render 'shared/delete_button', obj: [obj]
You could also use named routes, and do [route_name]_path(obj) instead of manually constructing the path.

Ruby on Rails: Confused about how link_to parameters for a delete action with a 'do' block

So I have a web app where a user can create and delete his own events. I am trying to link to a delete action when a user presses on a 'trashcan' icon (a font awesome icon) but am a bit stuck. I need to include the icon inside a link_to "do" block since I want the user to press it to delete his own event.
This is easy to do for a GET route, since I can just link to that view, for example, the code to edit a user's event looks like (I am using the cancan gem for authentication):
<% if can? :update, #event %>
<%= link_to edit_event_path(#event), class: 'user' do %>
<i data-toggle="tooltip" data-placement="left"
title="Edit your event" class="custom-a2">
<i class="fa fa-pencil-square-o fa-2x"> </i>
</i>
<% end %>
The above works. And the previous working link_to code to delete an event is:
<%= link_to 'Destroy', #event, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
So the above also works, but I want it so that when the user presses on a trashcan icon, it would link to the delete action of the events controller. This is confusing because this isn't a GET request, and so I need to pass the specific user and event to a link_to action without actually redirecting the user to a view. Can I please get help? My attempt is as below:
<%= link_to(#event), controller: :events, action: :delete, data: { confirm: 'Are you sure?' }, class: 'user' do %>
<i data-toggle="tooltip" data-placement="left"
title="Delete your event" class="custom-a2">
<i class="fa fa-trash fa-2x"> </i>
</i>
<% end %>
You only need to send the event to the delete action for deleting purpose. this should work:
<%= link_to #event, method: :delete, data: { confirm: 'Are you sure?' }, class: 'user' do %>
<i class="fa fa-trash fa-2x"></i>
<% end %>

Adding a Twitter Bootstrap button icon to button_to in Rails

I am working through the Agile Web Development with Rails book but I have been using Twitter Bootstrap instead of the custom styling from the book. I am having trouble adding an icon through GLyphonics to the button_to method. My code looks like this:
<%= button_to <i class="icon-search icon-white">Add To Cart</i>,
line_items_path(product_id: product),
class: "btn btn-success" %>
I have tried quite a few variations but can't seem to get it to work correctly.
I'm not sure how the OP got this to work, but Rails button_to generates an <input type='submit' /> element, which does not allow for HTML in the value field.
See also: input type="submit" Vs button tag are they interchangeable?
The best alternative in this situation is to force link_to to PUT (or POST):
<%= link_to raw("<i class=\"icon-search icon-white\">Add To Cart</i>"),
line_items_path(product_id: product),
class: "btn btn-success",
method: :put %>
You can add the icon as a child element:
<%= button_to button_path, method: :delete do %>
<span class="glyphicon glyphicon-search"></span>
<% end %>
It looks like you have an issue with your quotes:
<%= button_to raw("<i class=\"icon-search icon-white\">Add To Cart</i>"),
line_items_path(product_id: product),
class: "btn btn-success" %>
Enclose the label of the button in double quotes, escape the double quotes in your i tag, and finally, wrap everything into a raw() call to ensure the HTML is properly displayed.
Alternatively you can use html_safe:
<%= button_to "<i class=\"icon-search icon-white\">Add To Cart</i>".html_safe,
line_items_path(product_id: product),
class: "btn btn-success" %>
good point from #jordanpg: you can't have HTML in the value of a button, so his solution is more appropriate and should get the approved status.
the html_safepart remains valid though.
Using raw() or #html_safe still did not work for me.
I am using a helper method to create a button_to flag content. Ended up using the following in my helper method (path is defined beforehand):
form_tag path, :method => :post do
button_tag do
content_tag :i, 'Flag as inappropriate', :class => 'icon-flag flag_content'
end
end
I used this one and it works fine for me :
<%= link_to(line_items_path(product_id: product),
method: :put,
class: 'btn btn-success') do %>
<%= content_tag('i', nil, class: 'icon-search icon-white') %> Add To Cart
<% end %>
Hope this helps
I am using this helper:
module ApplicationHelper
def glyph(*names)
content_tag :i, nil, class: names.map{|name| "icon-#{name.to_s.gsub('_','-')}" }
end
end
Example:
glyph(:share_alt)
=> <i class="icon-share-alt"></i>
and
glyph(:lock, :white)
=> <i class="icon-lock icon-white"></i>
Using Rails 4 and Bootstrap 3, here's how to create a delete button using link_to or button_to.
Note that I'm using Haml instead of Erb.
In your view:
- #users.each do |user|
= link_to content_tag(:i, ' Delete', class: "glyphicon glyphicon-trash"),
users_path(user),
class: "btn btn-danger",
method: :delete,
data: { confirm: "Delete user #{user.username}?" }
You can also replace the content_tag part with
raw('<i class="glyphicon glyphicon-trash"> Delete</i>'),
This work for me, (and with confirm message)
<%= button_to "/home/delete?cardId="+card.id.to_s, data: { confirm:'Are you sure you want to delete?' } do %>
<i class="fa fa-times"></i>
<% end%>
this generate
<form class="button_to" method="post" action="/home/delete?cardId=15">
<button data-confirm="Are you sure you want to delete?" type="submit">
<i class="fa fa-times"></i>
</button>
</form>

Resources