I have a nested resource, comments like so....
resources :microposts do
member do
get :upvote, :undo_upvote
end
member do
get :follow, :unfollow
end
resources :responses do
member do
get :upvote, :undo_upvote
end
resources :comments
end
end
I have a delete button on the comments index page....
<div class = "Comment" id="comment-<%= comment.id %>">
<%= link_to comment_avatar_for(comment.user), comment.user %>
<span class="Commentator">
<%= comment.user.full_name %>
</span>
<span class="content">
<%= comment.body %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(comment.created_at) %> ago.
</span>
<span class="timestamp">
<% if current_user?(comment.user) %>
<%= link_to "delete", comment, method: :delete, data: { confirm: "You sure?" }, :class => "btn btn-default btn-xs delete" %>
<% end %>
</span>
</div>
And I am getting this error when I load the page
undefined method `comment_path' for #<# <Class:0x007f8936876e70>:0x007f8931857020>
I am uncertain why this isn't working - after all I have the correct instance of 'comment'. If someone could just point me in the right direction I would be grateful.
Rails makes assumptions.
Because you have an instance of Comment it assumes you're going to be using comment_path, but you don't have that as per your routes so you need to set the correct path:
<%= link_to "delete", micropost_response_comment_path(#micropost, #response, comment), method: :delete, data: { confirm: "You sure?" }, :class => "btn btn-default btn-xs delete" %>
I might have the path wrong, but hopefully you get the idea.
Related
I am refactoring my application so that users are now managed in a dedicated Administration namespace. I did the following:
routes.rb
namespace :administration do
get 'users', to: "/administration/users#index"
resources :users
resources :groups
end
The Users and Groups controller are now in the app/controllers/administration folder. They are defined as Administration::UsersController and Administration::GroupsController. The models are not namespaced.
The related links are now based on administration_users_path and administration_groups_path.
I use some partials for hamonised layout. One of them diplays Edit and Delete buttons, where this_object represents the instance passed to the locals:
_object_actions.erb
<% if this_object.is_active %>
<%= link_to [ :edit, this_object], method: :get, class: "mat-flat-button mat-button-base mat-primary" do%>
<span class="fa fa-edit"></span>
<%= t("Edit") %>
<% end %>
<%= link_to this_object, data: { confirm: t("Sure") }, method: :delete, class: "mat-flat-button mat-button-base mat-warn" do%>
<span class="fa fa-trash"></span>
<%= t("Destroy") %>
<% end %>
<% else %>
<%= link_to [ :activate, this_object], method: :post, class: "mat-stroked-button mat-button-base" do%>
<span class="fa fa-trash-restore"></span>
<%= t("Recall") %>
<% end %>
<% end %>
But coming to to Users and Groups, the path to actions is not properly built. I would need to build the URL such as /administration/users/1/edit, but it actually is /users/1/edit.
How can I make sure to build the correct URL for this purpose?
Thanks to this post Rails using link to with namespaced routes, I undestood that it is possible to add more parameters to the link_to method to build the actual link.
To call namespaced controller methods, I added the following to the links:
<%# Check if the parent is a module defined as Namespace. If not (Object) then display default link %>
<% namespace = controller.class.parent.name == 'Object' ?
nil :
controller.class.parent.name.downcase %>
<% if this_object.is_active %>
<%= link_to [ :edit, namespace, this_object],
method: :get,
class: "mat-flat-button mat-button-base mat-primary" do %>
<span class="fa fa-edit"></span>
<%= t("Edit") %>
<% end %>
<%= link_to [namespace, this_object],
data: { confirm: t("Sure") },
method: :delete,
class: "mat-flat-button mat-button-base mat-warn" do %>
<span class="fa fa-trash"></span>
<%= t("Destroy") %>
<% end %>
<% else %>
<%= link_to [ :activate, namespace, this_object],
method: :post,
class: "mat-stroked-button mat-button-base" do %>
<span class="fa fa-trash-restore"></span>
<%= t("Recall") %>
<% end %>
<% end %>
You can specify the desired route for each link_to
<%= link_to "Edit", edit_administration_user_path(this_object) %>
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>
I am trying to replace the word "like" with a heart icon.
Basically, I am trying to add <i class="glyphicon glyphicon-heart"></i> to the following:
<%= link_to 'Like', like_post_path(post), method: :post %>
Try something like
<%= link_to like_post_path(post), method: :post do %>
<i class="glyphicon glyphicon-heart"></i>
<% end %>
This is for you
<%= link_to '<i class="glyphicon glyphicon-heart"></i> Like'.html_safe, like_post_path(post), method: :post %>
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%>
I have a dozen different Views/Controllers for administrator. In every view I have got something like this:
<%= link_to edit_topic_path(topic) do %>
<span class="glyphicon glyphicon-pencil"></span>
<% end %> |
<%= link_to topic, method: :delete, confirm: "Are you sure?" do %>
<span class="glyphicon glyphicon-trash"></span>
<% end %>
Now my question is, How can I DRY this so I just pass some vars or object to a method and generate the above code for different views.
Thank you.
You should crate a partial
_controls.html.erb
<%= link_to path do %>
<span class="glyphicon glyphicon-pencil"></span>
<% end %> |
<%= link_to model, method: :delete, confirm: "Are you sure?" do %>
<span class="glyphicon glyphicon-trash"></span>
<% end %>
Then you can call it as follows:
<%= render :partial => "controls", :locals => {:path => edit_topic_path(topic), :model => topic} %>