How to deleted nested comments in Rails - ruby-on-rails

I recently did this tutorial http://www.reinteractive.net/posts/32-ruby-on-rails-3-2-blog-in-15-minutes-step-by-step, but im having trouble figuring out how to delete the comments once theyve been posted, ive tried the
method but that doesnt work as well as
it just return undefined method eror to me, what am i doing wrong
my code:
<%= div_for rep do %>
<p>
<div style="font-weight:bold; color:grey;"><%= rep.title %></div>
<div><%= rep.body %></div>
<strong style="font-size:8px;">
Posted <%= time_ago_in_words(rep.created_at) %> ago
</strong>
<br/>
<%= link_to 'Destroy', steppy_reps_path(#steppy, rep), method: :delete, data: { confirm: 'Are you sure?' } %>
</p>
<% end %>
the error:
No route matches [DELETE] "/steppies/11/reps.9"
routes.rb:
Testapp2::Application.routes.draw do
resources :steppies do
resources :reps, :only => [:create]
end
get "steppies/ask"
get "steppies/create"
get "steppy/ask"
get "steppy/create"
end
rake route output:
steppy_reps POST /steppies/:steppy_id/reps(.:format) reps#create
steppy_rep DELETE /steppies/:steppy_id/reps/:id(.:format) reps#destroy
steppies GET /steppies(.:format) steppies#index
POST /steppies(.:format) steppies#create
new_steppy GET /steppies/new(.:format) steppies#new
edit_steppy GET /steppies/:id/edit(.:format) steppies#edit
steppy GET /steppies/:id(.:format) steppies#show
PUT /steppies/:id(.:format) steppies#update
DELETE /steppies/:id(.:format) steppies#destroy
steppies_ask GET /steppies/ask(.:format) steppies#ask
teppies_create GET /steppies/create(.:format) steppies#create
steppy_ask GET /steppy/ask(.:format) steppy#ask
steppy_create GET /steppy/create(.:format) steppy#create

You need to add the action to your routes.rb file.
resources :steppies do
resources :reps, :only => [:create, :destroy]
end
Also, the form needs to use a different route.
steppy_rep_path(#steppy, rep)

Related

I have a bug in my HTML ERB file for Rails

I want to create a text field that accepts input and when you press the button next to it, it will save these changes to the database. It will be a PUT request, updating the value in the database for this specific user (not the logged in user, this is a page for admins to update values for users).
This is what the structure should be, (code to follow below):
1) Create text field and button for this in show.html.erb
2) Create a function called set_wistia_project_ID for this in users_controller.rb, set button equal to this :action
3) Create a helper function called set_project_id in user.rb that is called by set_wistia_project_ID, which actually updates the database. I decided to split it up into two functions because I noticed a lot of Rails projects tend to separate and break up functionality into the controller and model.
4) Edit my routes.rb to include some strange code, which I'll be honest, have no idea what it actually does. But without it, the entire app crashes on Heroku. So I leave it there. Found from a StackOverflow post.
My show.html.erb:
<% provide(:title, #user.name) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
<br>
<%= #user.email %>
<br>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :wistia_project_id %>
<%= f.text_field :wistia_project_id, class: 'form-control' %>
<%= button_to "Save", :action => "set_wistia_project_ID", :method => :put, :form_class => "form-control" %>
</div>
</div>
</h1>
</section>
</aside>
</div>
My users_controller.rb:
# Sets wistia_project_ID.
def set_wistia_project_ID
#user = User.find(params[:id])
#user.set_project_id
end
My user.rb:
# Sets the wistia_project_ID.
def set_project_id
self.wistia_project_ID # HOW TO SET EQUAL TO INPUT?
end
My routes.rb:
Rails.application.routes.draw do
root 'static_pages#home'
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users do
member do
put 'set_wistia_project_ID'
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
end
But the problem I am having is that when I try to go to show.html.erb, my Heroku app crashes :(
The reason:
F, [2019-06-25T19:53:20.706881 #8] FATAL -- : [b2ed08bb-de52-417f-9ba8-8ec85629dce8]
app/views/users/show.html.erb:30:
syntax error, unexpected end-of-input, expecting end
But it looks fine to me?
The syntax error is that you are missing <% end %> for the <%= form_for(#user) do |f| %> block.
To answer your question about how to access the param in the model's method - you should pass it as an argument:
#user.set_project_id(params[:wistia_project_ID])
And change the method to accept an argument. Note, I am assuming you want to actually persist this to the database, so I'm adding a save call. You could alternatively call update which assigns the new value and saves in one go.
def set_project_id(val)
self.wistia_project_ID = val # self is necessary here
save # or self.save, but the self is unnecessary here
end
However, calling save does always succesfully save the record, there is still the possibility of an error if a model-level validation failed. You can handle this using flash: https://guides.rubyonrails.org/action_controller_overview.html#flash-now
or, if you don't need to redirect, can just use an instance variable to store the errors:
#user = User.find(params[:id])
#user.set_project_id(params[:wistia_project_ID])
unless #user.valid?
#errors = #user.errors.full_messages
render :show
end
and in the show view:
<% if #errors %>
<p>THE FORM COULD NOT BE SAVED </p>
<ul id='errors'>
<% #errors.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
<% end %>
Also note, I would probably rename your set_project_id method to set_project_id! (added a bang), to signify that it mutates the input (changes its internal state).

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

No route matches [POST] - Rails destroy

I am new to RoR and still don't have enough experience on solving the different errors that may appear to me. In this case I am designing a blog where I can post articles. More specifically, my problem is related to deleting these articles.
As far as I know, writing:
resources :articles
in the routes file is an alternative for writing:
get "/articles" #index
post "/articles" #create
delete "/articles/:id" #delete
get "/articles/:id" #show
get "/articles/new" #new
get "/articles/:id/edit" #edit
patch "/articles/:id" #update
put "/articles/:id" #update
When I try to delete an article I get the following error:
No route matches [POST] "/articles/1"
The code I wrote was:
View
<% #articles.each do |art| %>
<%= art.title %>
<div>
<%= art.body %> - <%= link_to "Delete", art, method: :delete %>
</div>
<% end %>
Controller
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
It sounds like you have this in your view:
<%= art.body %> - <%= link_to "Delete", art, method: :destroy %>
But you actually need:
<%= art.body %> - <%= link_to "Delete", art, method: :delete %>
I'd advise double-checking this in your app based on your reply to a comment from #GonzaloRobaina.
It looks to me like you're missing the correct path in your code. It should work with something like this :)
<%= link_to "Delete, article_path(art), method: :delete %>

<%= posts_delete_path %> dont work

I'm programming a website with a friend, and I'm getting one error with my code. In the computer of my friend, he have the same code and don't have any error.
I get the following error:
We're sorry, but something went wrong.
Well, we're newbie in Rails and I get this error when I try to load the posts page. We think the error is on deleting a post, because if we comment the line of "posts_delete_path", everything works. For delete a post, we are using:
show.hmtl.erb:
<% #hashtag.posts.each do |p| %>
<li>Posted by: <%= p.user.name %></li>
<li>Content:</li>
<li><%= p.content %></li>
<li><a href="<%= posts_delete_path %>/<%= p.id %>" >Delete</a></li>
<% end %>
routes:
match '/signout', to: 'sessions#destroy'
match 'posts/delete/:id', to: 'posts#destroy'
resources :posts, :only => [:create, :show]
posts controller:
def destroy
#post = Post.find(params[:id])
if current_user.id == #post.user_id
#post.destroy
end
end
---- edit ----
rake routes:
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
We know that's the newbie way to delete a post, but we don't find any better solution. (If you want suggest something, we will be glad too).
<%= link_to "Delete", p, method: :delete %>
and modify the routes.rb with
resources :posts
You should use the link_to notation like this:
<%= link_to "Delete", p, method: :delete %>
Also, you should try to stop users from even seeing the delete button in the view, something like:
<% if current_user == p.user %>
<%= link_to "Delete", p, method: :delete %>
<% end %>
This will only show the delete link only to the owner of the post.
Also like Ganesh said , turn this:
match 'posts/delete/:id', to: 'posts#destroy'
resources :posts, :only => [:create, :show]
to:
resources :posts, :only => [:create, :show, :destroy]
Checkout the routing system in the rails guides: http://guides.rubyonrails.org/routing.html, and see which of the routes you will need.
looks like ur link is fine, but sounds like you did not include your js, thats why it is redirecting you to the show page when you click delete.
<%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %>
1. Check your /assets/application.js for //= require jquery and
//= require jquery_ujs
Add in application.js this code
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
2. Check your /layouts/application.html.erb for <%= javascript_include_tag "application"%>
<head>
<title>My awesome app</title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application"%>
<%= csrf_meta_tag %>
</head>
Thanks for everyone who tried help me. Now I solved my problem, but by other way.
What I do is:
In view:
<% if current_user.id == p.user_id %>
<%= button_to "Delete", { :controller => :posts, :action => 'destroy', :id => p.id }, :method => :delete %>
<% end %>
In controller:
def destroy
#post = Post.find(params[:id])
#if current_user.id == #post.user_id
#post.destroy
end
end
In routes:
resources :posts
Thanks guys!

Resources