Passing comments_id to controller in Rails app - ruby-on-rails

I am unable to edit/delete comments in a basic Rails MVC, but the trouble I'm having is that the comments_controller looks for the user_id and not the comments_id, because the user_id is the foreign key. My assumption was that Comment.find(params[:id]) would lead to comments_id, but this is npot the case.
This is the last part of my comments_controller:
def edit
#comment = Comment.find(params[:id])
#user = current_user
end
def update
#comment = Comment.find(params[:id])
#user = current_user
#comment.update(comment_params)
redirect_to #comment
end
def destroy
#user = current_user
#comment = Comment.find(params[:id])
#comment.destroy
redirect_to comments_path
end
private
def comment_params
params.require(:comment).permit(:user_id, :location, :title, :body)
end
The comments in the views that I'm trying to edit/delete look like this:
<% #user.comments.each do |w| %>
<tr>
<td>Location:<%= w.location %></td>
<td>Title:<%= w.title %></td>
<td>Body:<%= w.body %></td>
<td><%= link_to 'Edit', edit_comment_path %></td>
<td><%= link_to 'Destroy', comment_path,
method: :delete,
data: { confirm: 'Are you sure?' } %></td><br>
</tr>
<% end %>
Thanks for any advice offered :-)

When you edit/destroy your comment, you need to pass the actual comment in the link_to helper. Something like link_to 'Destroy', w, method: :delete, data: { confirm: 'Are you sure?' } will do.
Similarly with edit: link_to 'Edit', edit_comment_path(w)

Related

Delete post redirect to the post

I'm currently trying to delete a post through the link
<%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' }
The problem is that when i click on it, instead of destroying the post, it's redirect me to the show view of the post.
Here is my index.html.erb
<table>
<tr>
<th>titre</th>
<th>description</th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.description %></td>
<td><%= link_to 'Show', post_path(post)%> </td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<br>
And here is my controller posts.controller
class PostsController < ApplicationController
before_action :set_post, except: [:create, :index, :new]
def index
#posts = Post.all
end
def show
end
def new
#post = Post.new
end
def edit
end
def create
#post = Post.new(post_params)
#post.save
redirect_to :action => "index"
end
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Lime was successfully updated.' }
else
format.html { render :edit }
end
end
end
def destroy
#post.destroy
redirect_to root_path, :notice => "Vous n'ĂȘtes plus un batard"
end
private #private has nos end
#Must be set to update later
def set_post
#post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :description)
end
end
Thx for helping
Make sure you have //= require jquery and //= require jquery_ujs included in your application.js And include application.js included into view/layout/application.html.erb

Error searching for :all

I am currently trying to create a search method. I have a database all setup; however, I am running into the errors:
ActiveRecord::RecordNotFound in ArticlesController#index
and:
Couldn't find Article with 'id'=all
Here is the pertinent code:
Articles_controller.rb
class ArticlesController < ApplicationController
def index
#articles = Article.all
#articles = Article.search(params[:id])
end
def show
#article = Article.find(params[:search])
end
def new
#article = Article.new
end
def edit
#article = Article.find(params[:id])
end
def create
#article = Article.new(params.require(:article).permit(:title, :text))
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
article.rb
class Article < ActiveRecord::Base
validates :title, presence: true,
length: { minimum: 5 }
def self.search(search)
if search
#article = Article.find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
#article = Article.find(:all)
end
end
end
index.rb
<h1>Listing articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<%= form_tag articles_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</table>
Sorry for all the code to look through. The errors I am getting are from running localhost:3000/articles, where I receive these error messages from the server. I should note that I am still very new to both Ruby and Ruby on Rails; however, I aim to learn and find seeing proper code helps me quite significantly (I am dyslexic and tend to be a visual learner).
I truly appreciate your help, thanks in advance.
I think find can not take :all. the documentation says
"Using the find method, you can retrieve the object corresponding to the specified primary key that matches any supplied options. I think this is enough
Article.where('name LIKE ?', "%#{search}%")
or if you find all the articles
Article.all
Why do you have #articles = Article.search(params[:id]) in the index method?
Also, the stack trace will tell you exactly on which line the error occurs

Ruby getting started guide: uninitialized constant PostsController::Posts

I've done the ruby getting started guide a few times over and I always end up with the same result.
Guids
NameError in PostsController#index
uninitialized constant PostsController::Posts
Extracted source (around line #21):
19
20
21
22
23
24
def index
#posts = Posts.all
end
def edit
Rails.root: C:/RailsTesting/blog
Application Trace | Framework Trace | Full Trace
app/controllers/posts_controller.rb:21:in `index'
Request
Out of frustration/desperation, I copied all of the files from the actual code supplied by the tutorial with no avail, please help.
Here's my index
<h1>Hello, Rails!</h1>
<%= link_to "My Blog", controller: "posts" %>
<%= link_to 'New post', new_post_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th></th>
<th></th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post_path(post),
method: :delete, data: { confirm: 'Are you sure?' } %>
</tr>
...
Here is my posts_controller
class PostsController < ApplicationController
def new
#post = Post.new
end
def create
#post = Post.new(params[:post].permit(:title, :text))
if #post.save
redirect_to #post
else
render 'new'
end
end
def show
#post = Post.find(params[:id])
end
def index
#posts = Posts.all
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(params[:post].permit(:title, :text))
redirect_to #post
else
render 'edit'
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Your index method has the Post class written in plural. Change it to Post.all
Modify your index action as:
def index
#posts = Post.all
end
Here Post is the model from where you are get all posts.

undefined method `destroy' for nil:NilClass can't find the id of my data

I have a table that users can add data to, I've also included a button for users to delete a row of data:
<tbody>
<% #jobs.each do |job| %>
<tr>
<td><%= link_to job.job_title, job.url, :target => "_blank" %></td>
<td><%= job.company %></td>
<td><%= job.location %></td>
<td><%= link_to "", list_url(job), method: :delete, class: "fa fa-trash-o" %></td>
</tr>
<% end %>
</tbody>
However when the delete button is pressed I get a "undefined method `destroy' for nil:NilClass" error. In my server logs it says that the ID is null and I can't seem to grab the id of that particular row of data.
I've looked at other similar Stackoverflow questions to the one I am asking but the solutions found there haven't worked for me.
For reference here are my routes that relate to adding, showing and deleting data:
get '/jobs' => 'search#list', as: 'list'
post '/jobs' => 'search#create'
delete '/jobs' => 'search#destroy'
And my controller:
def new
#job = Job.new
end
def create
#job = Job.new(job_params)
if #job.save
redirect_to list_url, notice: "You've successfully added the job to the list"
else
render 'search', notice: "Something went wrong, please try again"
end
end
def list
#jobs = Job.all
#job = Job.find_by_id(params[:id])
end
def destroy
#job = Job.find_by_id(params[:id])
#job.destroy
redirect_to list_url, notice: "Job destroyed"
end
def job_params
params.require(:job).permit(:job_title, :company, :location, :url)
end
In your delete link, you want to link to the object itself, not the URL to the object. So change
<td><%= link_to "", list_url(job), method: :delete, class: "fa fa-trash-o" %></td>
to
<td><%= link_to "", job, method: :delete, class: "fa fa-trash-o" %></td>
On top of that, you'll need the appropriate CRUD routes defined, so I would recommend the following in your routes.rb file:
resources :jobs, controller: 'search'
Don't think you want to link to list_url if you're trying to specify a particular instance of job. Maybe something like
<td><%= link_to "", job_path(job), method: :delete, class: "fa fa-trash-o" %></td>

How to delete nested record

My girl model has comments with using gem 'acts_as_commentable'
When I access example.com/girls/show/1
It shows ID#1 girl's profile.
All the posted comments are shown in the bottom of this page.
For each comment row, I want to add delete button to delete a comment.
If it should pass the parameter to girls_controller.rb's comment_destroy action.
How action part and view should be??
It keeps undefined local variable or method `girls' error with codes below.
"girls/show.html.erb" view should be something like this. Just a part.
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Body</th>
<th>Subject</th>
<th>Delete</th>
</tr>
<% #all_comments.each do |comment| %>
<tr>
<td><%= comment.id %></td>
<td><%= comment.title %></td>
<td><%= comment.body %></td>
<td><%= comment.subject %></td>
<td><%= button_to 'comment_destroy', girls, confirm: 'Are you sure?', :disable_with => 'deleting...', method: :delete %></td>
</tr>
<% end %>
</table>
girls_controller.rb's comment_destroy action should be something like this
def comment_destroy
#comment = comment.find(params[:id])
#comment.destroy
respond_to do |format|
format.html { redirect_to girls_url }
format.json { head :ok }
end
redirect_to :controller => 'girls', :action => 'show', :id => params[:girls][:id]
flash[:notice] = "comment deleted!"
end
It looks like you have comments nested under a girl, and you want to delete the comment.
Routes
resources :girls do
resources :comments, only: [:create, :destroy]
end
Then, you have a comments controller that handles your creation and destroy.
<%= button_to 'comment_destroy', [#girl, comment], confirm: 'Are you sure?', :disable_with => 'deleting...', method: :delete %>
The destroy method in your comments controller:
def destroy
#girl = Girl.find(params[:girl_id])
#comment = #girl.comments.find(params[:id])
if #comment.destroy
redirect_to #girl, notice: "Comment Removed"
else
redirect_to #girl, error: "We could not remove the comment"
end
end
end
UPDATE -- based on user's request to use a non-restful solution
Routes:
resources :girls do
member do
delete :delete_comment, to: "girls#delete_comment", as: "delete_comment"
end
end
controller
def delete_comment
#girl = Girl.find(params[:id])
#comment = #girl.comments.find(params[:comment_id])
if #comment.destroy
redirect_to #girl, notice: "Comment Removed"
else
redirect_to #girl, error: "We could not remove the comment"
end
end
View link
<%= button_to 'comment_destroy', delete_comment_path(#girl, comment_id: comment.id), confirm: 'Are you sure?', :disable_with => 'deleting...', method: :delete %>
Final note: I really don't like this solution. You should have a Comments controller and go with my first solution.

Resources