If statement in Rails helper file? - ruby-on-rails

I have this code in a helper file:
def admin_post_edit_destroy(post)
if current_user.admin?
link_to 'Edit', edit_post_path(post)
link_to "delete", post, method: :delete, data: { confirm: "You sure" }
end
end
I got it to return the delete.
I was just wondering what do I need add to make it display the edit link in addition to the delete link? Do I type && or + or something else?

The problem you are having is that a ruby method automatically returns only the value of the last item calculated, so you are only getting the delete link back. Try adding the two links together like this:
def admin_post_edit_destroy(post)
if current_user.admin?
link_to('Edit', edit_post_path(post)) + link_to('Delete', post, method: :delete, data: { confirm: "You sure" })
end
end
You could also return two values from the helper method:
def admin_post_edit_destroy(post)
if current_user.admin?
return link_to('Edit', edit_post_path(post)), link_to('Delete', post, method: :delete, data: { confirm: "You sure" })
end
end
Then you can call the method like this to get the two return values in the two variables (edit_link and delete_link):
edit_link, delete_link = admin_post_edit_destroy(#post)

Related

Destroy method in rails and Mongoid

I am trying to delete a particular "work" from an array of "works" which is embedded in user.
In my work.html.erb file in views:-
<% #works.each do |f| %>
<%= link_to 'Destroy', profiles_destroy_path(f), data: {:confirm => 'Are you sure?'}, :method => :delete %>
<% end %>
And in my controller:-
def destroy
#work = current_user.works.find(params[:id])
#work.destroy
respond_to do |format|
format.html { redirect_to root_url }
end
end
I am getting following error:-
Mongoid::Errors::InvalidFind at /profiles/destroy.56fa4d2f498b5908a002e2e8
P.S. - I am new to rails.
Please check params[:id] is not nil. There's no :id at session start and you get the Mongoid::Errors::InvalidFind exception above.
delete action doesn't have path by default..
<%= link_to 'Destroy', profiles_path(f), data: {confirm: 'Are you sure?'}, method: :delete %>
That will call the destroy action from your controller.
And make sure params[:id] is not nil.
PS: use rails 4 annotation (avoid using rockets (=>))
There may be an error in Juan's answer.
I believe you want:
<%= link_to 'Destroy', profile_path(f), method: :delete, data: {confirm: 'Are you sure?'} %>
Note: the singular "profile_path" instead of "profiles_path"
This is because the destroy action uses the same path as the show action, but uses a DELETE method instead of a GET. (Which you can observe by running "rake routes" in your terminal and comparing the two routes.)

Upon clicking delete, rails refreshes page but does nothing

I am writing a rails app for planning school assignments.
I have been experiencing a problem upon clicking the delete button on the web page it just refreshes the page but doesn't delete anything from the page.
Here is my destroy action from assignment_controller along with assignment_params.
def destroy
#assignment = Assignment.find(params[:id])
#assignment.destroy
redirect_to assignments_path
end
private
def assignment_params
params.require(:assignment).permit(:name)
end
And this is my index.html.erb
<h1>Listing of Assignments</h1>
<% #assignment.each do |x| %>
<h2><%= x.name %></h2>
<%= link_to "Delete", assignments_path(#assignment),
method: :delete,
data: { confirm: "Are you sure?" } %>
<% end %>
I'm not sure if the problem is with the route but here is my routes.rb just in case.
Rails.application.routes.draw do
devise_for :users
resources :assignments
root 'home#index'
end
Thank you!
Instead of:
<%= link_to "Delete", assignments_path(#assignment),
method: :delete,
data: { confirm: "Are you sure?" } %>
Try this:
<%= link_to "Delete", assignment_path(x),
method: :delete,
data: { confirm: "Are you sure?" } %>
You must pass the x not #assignment because you have been passed it in a loop. Also, change: assignments_path to assignment_path.
You have to change your link for delete to below
<%= link_to "Delete", x, method: :delete, data: { confirm: "Are you sure?" } %>

Can't get syntax for deleting a commentable comment in Rails 4

Here's the line from my routes config file:
DELETE /posts/:post_id/comments/:id(.:format) posts/comments#destroy
I want to delete a single comment but I can't get the syntax right. Here's what I tried:
<% if current_user == comment.user %>
<span class="edit-delete-line"><p><small><%= link_to "Delete", post_comments_path([#commentable, comment]), method: :delete, data: { confirm: "Are you sure?" } %> </span>
</small></p>
<% end %>
That gives me a routing error. I also tried:
<% if current_user == comment.user %>
<span class="edit-delete-line"><p><small><%= link_to "Delete", post_comments_path(params([:post_id], [:comment_id])), method: :delete, data: { confirm: "Are you sure?" } %> </span>
</small></p>
<% end %>
I got "wrong number of argument" error this time. I know this should be simple, right???
You're trying to delete a single comment, so you need to use post_comment_path(#commentable, comment), not plural post_comments_path, which points at the index. If you have your controller set up to also accept the unnested resource (just /comments/:id), you can just use comment_path directly.
Check out the Rails routing guide for more details.
<%= content_tag :span, link_to("Delete", [#commentable, comment], method: :delete, data: { confirm: "Are you sure?" }), class: "edit-delete-line" if current_user == comment.user %>
This should get it working for you.
To give you a simple synopsis, you have to realize that the Rails routes are basically helper methods which are generated when you define your respective routes.
The route helpers themselves don't do anything except give you a dynamic way to call specific routes. For example, instead of "/posts/<%= #post.id %>", you can call posts_path(#post).
--
The Rails routes work very simply -- they take arguments like any other helper method.
Thus, if you call a route which requires specific values to be passed (for example post_comments_path(post_id, comment_id), you have to pass the respective values to the helper.
Therefore, you can call the following:
post_comments_path(#commentable, comment), method: :delete
... or if you're using link_to, you should be able to pass the respective data objects that you require:
link_to "Destroy", [#commentable, comment], method: :delete

Index pagination with links to own method

actually i use the following code in my _user.html.erb:
<%= link_to "Delete User", user, method: :delete, data: { confirm: "Are you sure?" } %>
For every user i get a link to delete him.
In my controller the delete-method calls the destroy-method:
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted."
redirect_to users_url
end
This all works fine.
What i want now is another link where a call a custom method.
How can i call a custom method?
What i already tried was to add a method with the name test and then i called method: :test but this doesn't work:
No route matches [POST] "/users/2"
Thanks for any idea
Question heading is wrong, pagination means something else.
First configure your routes like
resources :controller_name do
member do
delete 'test'
end
end
Your route will be set ..
test_controller_name DELETE /controller_name/:id/test(.:format) controller_name#test
Call the action from link_to and pass the object id .
<%= link_to 'Destroy', test_controller_path(:id => #object.id), method: :delete, data: { confirm: 'Are you sure?' } %>

ROR delete action not working

I am beginner to ROR and I am following this tutorial http://guides.rubyonrails.org/getting_started.html.
So according to this tutorial I want to delete one post. But its not working it showing this error The action 'destroy' could not be found for PostsController
My post controller delete method looks like
def destroy
#post = Post.find(params[:id])
logger.debug "***********************: #{#post.id}"
#post.destroy
redirect_to posts_path
end
In routes I mentioned resource resources :posts but still it is giving error for destroy action. Am I doing something wrong. Need Help.
Did you mention the method as delete in your views?
If you are using Rails 4, you should do:
<%=link_to 'Destroy', post_path(post), method: :delete, data: { confirm: 'Are you sure?' } %>
In Rails 3:
<%=link_to 'Destroy', post_path(post), method: :delete, confirm: 'Are you sure?' %>

Resources