Couldn't find with 'id' - ruby-on-rails

when i use method 'destroy' show me an error
Couldn't find Post with 'id'=#<Post::ActiveRecord_Relation:0x000000045e14c0>
method destroy
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
view
<%= link_to 'Destroy', post_path(#posts),
method: :delete,
data: { confirm: 'Are you sure?' } %>
sorry for my bad English

You have #posts variable defined as a ActiveRecord::Relation (some kind of models array, defined with #posts = Post.all or anything like that). To fix your issue, if you have link_to call inside index url, most probably you are doing something like:
<% #posts.each do |post| %>
# link_to call somewhere here
<% end %>
Then you have to change your link_to call to
<%= link_to 'Destroy', post_path(post),
method: :delete,
data: { confirm: 'Are you sure?' } %>
Notice we use variable defined in .each do |var| block, here it's post

Related

Can we attach some variables to route path in ruby on rails 6?

I'm a beginner in ruby-on-rails and I struggled for hours to do this:
There is one variable X in a html page Page1 of one controller C1 in my application. And I want to link to anther route path path1 with that variable X so that I can do something with that variable X in the controller action act1 corresponding to that route path path1.
In my situation is:
<% #courses.each do |course| %>
<tr>
<td><%= link_to 'Enroll', enroll_path, method: :post, data: { confirm: 'Are you sure?' } %></td>
</tr>
This code is in the view page html and I want to link_to enroll_path with the variable course
post '/enroll', to: 'enrollments#enroll'
This is the enroll path route
def enroll
enrollment = Enrollment.new(user_id: current_user.id, course_id: course[id])
enrollment.save
redirect_to root_url
end
This is action that corresponds enroll path and I want use the variable course in it.
I have tried to attach the variable course directly to the enroll_path, Like this:
<td><%= link_to 'Enroll', enroll_path(course), method: :post, data: { confirm: 'Are you sure?' } %></td>
but it did not work out.
How should I do to solve this?
Have you tried
<%= link_to 'Enroll', enroll_path(course_id: course.id), method: :post, data: { confirm: 'Are you sure?' } %>
I believe you will have to change the controller line to this
enrollment = Enrollment.new(user_id: current_user.id, course_id: params[:course_id])
This part (course_id: course.id) sends course_id as a param, so in the controller you can access its value by looking for params[:course_id].

Nesting under the Devise in Rails4

I have a problem with Controller Show Product. Can anyone help me out?
No route matches {:action=>"show", :controller=>"products", :id=>nil, :vendor_id=>"3"} missing required keys: [:id]
<td>
<%= link_to 'Show', vendor_product_path(current_vendor, #product) %><br>
<%= link_to 'Edit', edit_product_path(product) %><br>
<%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
my controller in product
def show
#product = Product.find(params[:vendor_id])
end
If you're trying to find all the products for a specific vendor, it would be something like:
def show
#vendor_products = Vendor.find(params[:vendor_id]).products
end
If you are just trying to find a single product by id:
def show
# Since :id is currently nil in params, this will not work
#product = Product.find(params[:id])
end
In the view, where are you getting #product from? Seems like #product is not yet saved to the database, and so it is nil in the view (in the line <%= link_to 'Show', vendor_product_path(current_vendor, #product) %>. Which view file have you shown, and what is the corresponding controller action?
In that controller action, make sure you add a line to save the product #product.save
In case the #product is valid, then try passing in the fields explicitly:
<%= link_to 'Show', vendor_product_path(:vendor_id => current_vendor.id, :id => #product) %>

New instance of a linked model, pass 'caller' id

I'm creating a simple inventory app, there is a view that lists 'items'. It has tables with these rows:
<tr>
<td><%= item.title %></td>
<td><%= item.desc %></td>
<td><%= item.value %></td>
<td><%= item.room.name %></td>
<td><%= item.user.username %></td>
<td>
<%= link_to 'View', item %>
<%= link_to 'Edit', edit_item_path(item) %>
<%= link_to 'Delete', item, method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to 'Add Comment', !?????! %>
</td>
/tr>
I have a linked model for 'comments' set up but don't know how to pass the 'item_id' to it when creating a new one.
The URL helpers actually accept the object to make a route for an association. Meaning, assuming you have a nested route for comments within items,
resources :items do
resources :comments
end
you can link_to the new_item_comments_path(item).
The method new_item_comments_path(item) makes a string URL based on the new_item_comments route, which you feed to link_to to make an HTML <a> tag.
To be clearer, in your view you would have:
<%= link_to 'View', item %>
<%= link_to 'Edit', edit_item_path(item) %>
<%= link_to 'Delete', item, method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to 'Add Comment', new_item_comments_path(item) #-> (instead of ???) %>
In this case, the item you are passing is the reference to your current item, which allows the URL helper to make a URL for it from the route.
The Rails guide for routing should be a useful read for you.
Now that's assuming your Comment controller assigns the right stuff at the right place. You seemed to have figured that out, but I'll explain for the sake of clarity (and future visitors)
class CommentsController < ApplicationController
# GET /item/:item_id/comments/new
def new
#comment = Comment.new
#item = Item.find(params[:item_id])
#comment.item = #item
# render
end
# POST /item/:item_id/comments
def create
#comment = Comment.new(params[:comment])
#item = Item.find(params[:item_id])
#comment.item = #item
# if #comment.save blah
end
end
All credit should go to #jonallard
The solution is all about routing it seems, you need to pass a url to the form that makes new comments (linked models).
to do this:
Add this (or similar depending on object names) to the page that is calling the creation:
<%= link_to 'Add Comment', new_item_comment_path(#item) %>
In both the new and the create method of the comments_controller there is a line starting #comment = Comment.new. Under that line add:
#item = Item.find(params[:item_id]) AND
#comment.item = #item
edit the top line of the comments template for to: <%= form_for(#comment, {:url => item_comments_path(#item)}) do |f| %>
Edit routes to somethings like:
resources :items do
...
resources :comments
end
and Read this: http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects!
As mentioned before all credit goes to #jonallard, his answer and his expertise.

Link for destroy returns "undefined method"

I have the*Topics* controller and in the view/topics/index.html.erb the link for destroying item:
<%= link_to 'Destroy',topic, confirm: 'Are you sure?', method: :delete %>
I tried also
<%= link_to 'Destroy', topic_path(topic), confirm: 'Are you sure?', method: :delete %>
but both returns
undefined method topic_path for Class:0x00000105056a80>:
0x00000105047328>
in routes.rb is following:
namespace :admin do
...
resources :topics
end
Where could be the problem and how to in the easily way to solve it? I was checking the other generated controllers/views by CRUD, and the setup is always the same and in all other controllers it's working well, just in this one I am getting over and over again this error.
Try this:
<%= link_to 'Destroy', admin_topic_path(topic),
confirm: 'Are you sure?', method: :delete %>
To make sure run command rake routes, in the result you should see your route.

Trouble with deleting my blog posts using the UI

I am very new to Ruby on Rails so apologies, as this may be a silly question to post here.
I have made a blog (using generate scaffold). I have a few pages to interact and edit blog posts, starting with a main page which displays all blog posts ("index"), an area to view a specific blog post ("show"), and area to edit a blog post ("edit"), and an area to create a new blog post ("new").
I've also created comments (again using generate scaffold) to be applied to relevant blog posts. Comments, and a partial form for comments appears on the "show" page.
I have been working away on the whole thing to get it working nicely, but have recently realised that delete buttons that I had on the "index" page aren't working. Instead of prompting confirmation for the delete, I'm simply taken to the "show" of the relevant post.
Here is a snippet of the index "index" page:
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= image_tag "work/thumbnails/#{post.image}", :alt => '' %></td>
<td><%= time_ago_in_words(post.created_at) %> ago (<%= post.created_at %>)</td>
<td class="zero-out"><%= link_to 'Show', post, :class => "submit-button" %></td>
<td class="zero-out"><%= link_to 'Edit', edit_post_path(post), :class => "submit-button" %></td>
<td class="zero-out"><%= link_to 'Destroy', post, confirm: 'Are you sure?', :method => :delete, :class => "submit-button" %></td>
</tr>
<% end %>
And here is the snippet of code from the posts_controller relevant to the delete:
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :ok }
end
end
I have also found that the remove comment buttons (on the "show" page alongside each comment) have stopped working with an error message:
Unknown action
The action 'show' could not be found for CommentsController
For reference the code for the "remove comment" button is:
<%= link_to 'Remove Comment', [comment.post, comment],
:confirm => 'Are you sure?',
:method => :delete %>
And the snippet of code in the comments_controller is:
def destroy
#post = Post.find(params[:post_id])
#comment = #post.comments.find(params[:id])
#comment.destroy
redirect_to post_path(#post)
end
I lack the full knowledge of how RoR works, and how these files interact with each other fully to troubleshoot the problem, so any help would be very much appreciated.
First you miss the arrow in the destroy link method call
Correct it to this.
<%= link_to 'Destroy', post, confirm: 'Are you sure?', :method => :delete, :class => "submit-button" %>
Second you don't have to find the post of a comment to delete it simply do this.
If you want to be redirected back to the post, you do it in the controller
<%= link_to 'Remove comment', comment, confirm: 'Are you sure?', :method => :delete %>
in the comment_controller
def destroy
#comment = Comment.find(params[:id])
#post = #comment.post
#comment.destroy
respond_to do |format|
format.html { redirect_to #post }
format.json { head :ok }
end
end
I've found the problem - I had customised the links to stylesheets, javascript files etc and in doing this I had left out links to the javascript files for posts and comments created by the scaffold.
After including links to these files everything worked again.

Resources