Rails delete link url took wrong page - ruby-on-rails

If I delete a post, it takes me to post/index.html.erb, but I want it to redirect to the profile/index.html.erb page.
<% current_user.posts.each do |post| %>
<%= post.post_name %> </p>
<%= link_to "view", post, class: "btn btn-default" %>
<%= link_to "Delete", post_path(post), method: :delete,
data: { confirm: "Are you sure?" },
class: "btn btn-default" %>
<% end %>

You're looking at the wrong code. Your view has nothing to do with this. Fix/change the redirection in your PostsController#destroy.

Related

Trying to adapt code form one of my views to be rendered in a partial

I have a partial that takes a column from a database a iterates over it and displays all the urls on the web page. I want to add the lines that I have written in my app/views/topics/show.html.erb page that allow the user to edit, delete, or like the link.
These are the three lines I want to adapt to be added to the partial, the first two are buttons to carry out the edit and delete function and the third is a link to a partial with the like code.
<%= link_to "Edit bookmark", edit_topic_bookmark_path(bookmark.topic, bookmark), class: 'btn btn-primary btn-xs'%><br>
<%= link_to "Delete bookmark", [bookmark.topic, bookmark], method: :delete, class: 'btn btn-danger btn-xs', data: { confirm: 'Are you sure you want to delete this bookmark?' } %>
<%= render partial: 'likes/likes', locals: { bookmark: bookmark } %>
this is the partial I would like to adapt it to located in app/views/bookmarks/_bookmarksandlikes.html.erb
<div>
<h3>
<%y=1%>
<% mark.each do |x| %>
<%=y%>)<%= link_to x.url, x.url%>
<%y=y+1%>
<% end %>
</h3>
</div>
and it is being called from app/views/users/show.html.erb with these two lines.
<%= render partial: 'bookmarks/bookmarksandlikes', locals: { mark: #bookmarks} %>
<%= render partial: 'bookmarks/bookmarksandlikes', locals: { mark: #liked_bookmarks} %>
here is how I have tried to insert the code into the partial
<div>
<h3>
<%y=1%>
<% mark.each do |x| %>
<%=y%>)<%= link_to x.url, x.url%>
<%y=y+1%>
<%= link_to "Edit bookmark", edit_topic_bookmark_path(x.topic, x), class: 'btn btn-primary btn-xs'%><br>
<%= link_to "Delete bookmark", [x.topic, x], method: :delete, class: 'btn btn-danger btn-xs', data: { confirm: 'Are you sure you want to delete this bookmark?' } %>
<%= render partial: 'likes/likes', locals: { x: x } %>
<%end%>
</h3>
when I run the code as is I get an error page that says "undefined local variable or method 'bookmark' for #<#<Class:0x007fdfb1b39c80>:0x007fdfb1988d50> Did you mean? bookmark_url or #bookmarks
and says there is a problem on line 3 of the partial with the code for liking a page in app/views/likes/_likes.html.erb
<% if policy(Like.new).create? %>
<div>
<% if like = current_user.liked(bookmark) %>
<%= link_to [bookmark, like], class: 'btn btn-danger', method: :delete do %>
<i class="glyphicon glyphicon-star-empty"></i> Unlike
<% end %>
<% else %>
<%= link_to [bookmark, Like.new], class: 'btn btn-primary', method: :post do %>
<i class="glyphicon glyphicon-star"></i> Like
<% end %>
<% end %>
</div>
<% end %>
side note at this point in working through this problem, it appears that only the third line for the partial I am trying to insert is giving me problems, the edit and delete buttons are rendering nicely.
Even if you don't know the answer I am quite keen to hear any thoughts on what I am not considering in this question or just your thoughts, also if you need more information please just let me know and I will post more.
Thanks for looking at my question.
I figured out my problem, I could not use the partial because the partial app/views/likes/_likes.html.erb was taking in a different variable than was being passed by app/views/bookmarks/_bookmarksandlikes.html.erb so I just put the needed code into app/views/bookmarks/_bookmarksandlikes.html.erb and adapted it like so.
<div class=row>
<h3>
<%y=1%>
<% mark.each do |x| %>
<%=y%>)<%= link_to x.url, x.url%><br>
<%y=y+1%>
<%= link_to "Edit bookmark", edit_topic_bookmark_path(x.topic, x), class: 'btn btn-primary btn-xs'%><br>
<%= link_to "Delete bookmark", [x.topic, x], method: :delete, class: 'btn btn-danger btn-xs', data: { confirm: 'Are you sure you want to delete this bookmark?' } %><br>
<% if like = current_user.liked(x) %>
<%= link_to [x, like], class: 'btn btn-danger', method: :delete do %>
<i class="glyphicon glyphicon-star-empty"></i> Unlike
<% end %>
<% else %>
<%= link_to [x, Like.new], class: 'btn btn-primary', method: :post do %>
<i class="glyphicon glyphicon-star"></i> Like
<% end %>
<br>
<% end %>
<%end%>
</h3>
</div>

NoMethodError: undefined method `stringify_keys' for "/auctions/2/comments/9":String Rails [duplicate]

I have this edit button:
<%= link_to edit_income_path(trans), class: "btn btn-default" do %>
<i class="glyphicon glyphicon-pencil"></i>
<% end %>
And I want a delete button to look the same with this icon: glyphicon glyphicon-trash, but I can't find the right syntax to make it work and look the same. My delete button now:
<%= button_to "delete", trans, :method=> :delete, :remote=> true %>
Is this what you are looking for??
<%= button_to trans, method: :delete, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-trash"></span>
<% end %>
try this...for example with link_to
<%= link_to (‘<i class=“fa fa-thumbs-up fa-lg”> </i>’).html_safe, vote_path(#image), :method=> :delete, :remote=> true%>

Rails pass radio button params

Hey guys I am a newbie so go easy on me.
I am trying to figure out how to pass to the controller the radio button choice. It is NOT part of the model. Here is the code:
<%= radio_button_tag(:remove, "yes") %>
<%= label_tag(:yes_delete, "yes, delete the order") %>
<%= radio_button_tag(:remove, "no") %>
<%= label_tag(:no_dont_delete, "No, do not delete the order") %>
<h4 class="row"><td><%= link_to 'Delete', payment_path(:id => #payment.id), method: :delete, data: { confirm: 'Are you sure?' } %></td></h4>
When I click delete all here is what the params look like:
Parameters: "authenticity_token"=>"lfFsfN04ajhIpYI13bwhmGtLtQIdRUkaClPsBBP12SSZwDEnsIslOqH3yptGvWUF620bEYhPBgbD7sLYV2cA7Q==", "id"=>"175"}
Thank for the help.
form_for can handle that easily.
<%= form_for #payment, method: :delete do |f| %>
<%= radio_button_tag(:remove, "yes") %>
<%= label_tag(:yes_delete, "yes, delete the order") %>
<%= radio_button_tag(:remove, "no") %>
<%= label_tag(:no_dont_delete, "No, do not delete the order") %>
<h4 class="row"><td><%= f.submit 'Delete', data: { confirm: 'Are you sure?' } %></td></h4>
<% end %>
The post parameters received by the server:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"KoCb84m88ptGUPbY9eofUMjpGLGmpKfUwBxB2t+Fv+QABr6lWnWpsEuJQ/quOJN3g6Zm+DZy2JTdVokjMQxLvQ==", "remove"=>"yes", "commit"=>"Delete", "id"=>"1"}

Passing HTML from serverside to datatables

I am changing my Datatables to load content from my Rails app. In one of the cells I have a big chunk of HTML, and since when using the server-side approach the cells are printed with Javascript, I would need to pass the HTML from Rails.
So, here is how it looks one of the cells with datatables WITHOUT server side fetching:
<td>
<div class='order-actions-container'>
<div class='order-action'>
<%= link_to 'Show pages', admin_order_pages_path(order.id), :class => 'btn btn-primary' %>
</div>
<% if order.status.name == 'reviewed' %>
<div class='order-action'>
<%= form_tag(admin_order_set_completed_status_path(order), :method => 'patch' ) do %>
<%= submit_tag 'Complete order', class: 'btn btn-success', data: { confirm: 'Are you sure you want to complete this order?' } %>
<% end %>
</div>
<% end %>
<% if order.status.name == 'queued' %>
<div class='order-action'>
<%= form_tag(admin_order_process_order_path(order), :method => 'post' ) do %>
<%= submit_tag 'Process order', class: 'btn btn-success', data: { confirm: 'Are you sure you want to process this order?' } %>
<% end %>
</div>
<% end %>
<% if order.status.name != 'processing' %>
<div class='order-action'>
<%= form_tag(admin_order_path(order), :method => 'delete' ) do %>
<%= submit_tag 'Delete order', class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this order?' } %>
<% end %>
</div>
<% end %>
</div>
</td>
Now, since all the data will come back from Rails, I would need to pass that HTML via a JSON. Isn't that breaking the MVC? How do you deal in these situations?
If you are wanting to use json with ajax for datatables you can use JBuilder or rabl to create a json document that can be easily reused.
Also there is not a need to write out form_tags wrapped around submit_tags to call different methods, with rails you can use link_to and specify the :method to accomplish the same thing.
<td>
<div class='order-actions-container'>
<div class='order-action'>
<%= link_to 'Show pages', admin_order_pages_path(order), :class => 'btn btn-primary' %>
</div>
<% if order.status.name == 'reviewed' %>
<div class='order-action'>
<%= link_to "Complete order", admin_order_set_completed_status_path(order), method: :patch, class: 'btn btn-success', data: { confirm: 'Are you sure you want to complete this order?' }%>
</div>
<% end %>
<% if order.status.name == 'queued' %>
<div class='order-action'>
<%= link_to "Process order", admin_order_process_order_path(order), method: :post, class: 'btn btn-success', data: { confirm: 'Are you sure you want to process this order?' }%>
</div>
<% end %>
<% if order.status.name != 'processing' %>
<div class='order-action'>
<%= link_to "Delete order", admin_order_path(order), method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this order?' }%>
</div>
<% end %>
</div>
</td>

Adding a class to link_to block

I have a following code which displays a 'delete' link:
<%= link_to :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
But for some reason ROR is not adding some_class to a tag. Have you any idea what can i do to fix it ? Thanks in advance.
You need to add the URL as the first parameter, then the html options, e.g.:
<%= link_to resource_path(#resource), :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
I actually found this to be a working solution with Rails 4.2
<%= link_to(resource_path(#resource), class: "project-card clearfix") do %>
<h1>Your html here</h1>
<% end %>
If you need to pass a controller and action, like edit and destroy, do it as follow:
<%= link_to url_for(controller: controller_name, action: :edit, id: item.id), class: "btn btn-link btn-warning btn-just-icon edit" do %>
<i class="material-icons">edit</i>
<% end %>
<%= link_to url_for(controller: controller_name, action: :destroy, id: item.id), method: :delete, data: { confirm: t('common.confirm') }, class: 'btn btn-link btn-danger btn-just-icon remove' do %>
<i class="material-icons">close</i>
<% end %>
The link_to docs:
link_to(body, url, html_options = {})
So you'd want
<%= link_to <span>Delete</span>, '/someurl', :class=>'some_class', :method=>:delete, .... %>

Resources