undefined method error with delete statement - ruby-on-rails

For my projects I have the following relevant code
routes:
resources :lists do
resources :items
end
I now included a loop on list/show page in which I want to show the item and provide the users with the possibility to delete the item.
So i got code like this:
<% #items.each do |item|%>
<p>
Item: <%= item.name %>
<%= time_ago_in_words(item.created_at) %> ago.
</p>
<%= link_to "Delete", [#list.item, item], method: :delete %>
<% end %>
But when I try to run it I get the error:
undefined method 'item' for #<List:0x007fba7be6fe28>
While I did define the variables in my controller:
Items-controller:
def destroy
#list = current_user.list
#item = #list.items
end
Could anyone explain whats causing this error?

you have a typo in your controller - it should be #items = #list.items that's why the iteration doesn't work properly.
edit: after formatting your original question, I see that the error was raised on #list object, so you have to fix path to delete action:
<%= link_to "Delete", [#list, item], method: :delete %>
you build the path by providing the parent object (#list) and then the object itself (item) - Rails will translate it to list_item_path.

change your link_to
= link_to("Delete", lists_items_url(#list, #item), method: :delete)
You have defined resources :lists and resources :items.
Basically what you want to do is send a DELETE request to the collection with the IDs to fetch and delete the items.
Run a rake routes to check the exact naming, but I think it's written like that to be sure. Although you don't seem to be needing the IDs based on your controller's action, you still need to supply them for the helper.

You're using #items.each and in your controller have only #item.

Related

Why is my RoR link pointing to the wrong controller action?

I have the following in my projects_controller.rb:
def destroy
#project = Project.find_by_slug(params[:id])
#project.destroy
redirect_to projects_url
end
And I have the following in my routes.rb file:
delete "projects/:id", to: "projects#destroy", as: "destroy_project"
I have the following link (inside the show.html.erb file):
<%= link_to destroy_project_path(#project), method: :delete, class: "btn-gradient btn-red" do %>
<span>Delete Project</span>
<% end %>
Upon clicking the button, the page reloads. The show action is called upon clicking the button. I've added console logs in each method, and it is clear that the destroy action is never called.
Can anyone point me in the right direction?
the link_to helper receives 2 different sets of hashes for the options.
The first set is for things like the http method, and the second for the html attributes (class, id and so on)
The way you wrote it, you probably have method=delete in your query params, which is wrong. You have to explicitly enclose the method: :delete within its own options hash:
<%= link_to destroy_project_path(#project), { method: :delete }, class: "btn-gradient btn-red" do %>
<span>Delete Project</span>
<% end %>
If you use rails 7 with turbo framework you can try below for buttons.
<%= button_to "Delete this project", destroy_project_path(#project), method: :delete %>
Or you can try below for links.
<%= link_to destroy_project_path(#project.id) , data: { turbo_method: :delete } do %>
<span>Delete Project</span>
<% end %>
the problem that you are having is with the JS. there is a problem where something is not loading properly.
For testing try to remove your JS and use something like this
<%= javascript_include_tag "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.js" %>
When you get the delete method working, then you can debug where in your JS there is a problem.
at this time there is not sufficient info on your question to be able to know what it is.

Link_to path in each do loop not working

In this Rails app, Users write Stories. A Story may be part of a Collection. Collections belong to the User who created it.
I am trying to show a single Story with links to the Collections it is part of. The collection.name part works but I can't get the collection_path right. Thanks for your help.
stories/show.html.erb
<% #story.collections.each do |collection| %>
<%= link_to collection.name, collection_path %>
<% end %>
rake routes for collections
user_collections GET /users/:user_id/collections(.:format) collections#index
POST /users/:user_id/collections(.:format) collections#create
new_user_collection GET /users/:user_id/collections/new(.:format) collections#new
edit_user_collection GET /users/:user_id/collections/:id/edit(.:format) collections#edit
user_collection GET /users/:user_id/collections/:id(.:format) collections#show
routes.rb
resources :users do
resources :collections
Solved it by using the following with the help of Sebastián Palma who answered this earlier.
<% #story.collections.each do |collection| %>
<%= link_to collection.name, user_collection_path(collection.user, collection), class: 'btn btn-lake' %>
<% end %>

No routes matches DELETE for removing a CarrierWave multiple file uploader

Using a member route like this similar to one I have used for deleting tags:
resources 'factory' do
resources 'tags' do
member do
delete :remove
end
end
resources 'project_files' do
member do
delete :remove
end
end
end
I (should) invoke in the projects controller:
class ProjectFilesController < ApplicationController
def remove
#factory = Factory.find(params[:fabmoment_id])
#project_file = ProjectFile.find(params[:id])
authorize #factory, :project_file?
#project_file.remove!
head :ok
end
end
Which I can't check for correctness (yet). The partial _project_files looks like:
<li class="project_file">
<% if policy(factory).project_file? %>
<%= link_to "<span></span>".html_safe,
remove_factory_project_file_path(factory, project_file), method: :delete, remote: true,
class: "remove",
title: "remove" %>
<% end %>
<%= link_to File.basename(project_file.url), project_file.url %>
(<%= number_to_human_size(project_file.file.size) %>)
</li>
Which is invoked by passing locales with a render method in the factory#show view. (Could also add an index.)
Like this it is mostly equal to the tags remove action though and I like to keep things consequent and simple.
The error I receive from my spec:
No route matches [DELETE] "/factories/1/project_files/%2Fuploads%2Ffactory%2Fproject_files%2F1%2Fthe_puritan_1st_floor.stl/remove"
My Factory model only contains mount_uploaders :project_files, ProjectFileUploader
Any ideas on how to make this work?

rails link_to with block, controller options and html post

I am trying to use link_to in rails 4 with controller and html options and a do..end block. I have seen similar posts but have not been able to use any of the answers successfully.
Working code without a do..end block:
<%= link_to 'recommend', { controller: 'recommendations', id: offer.id }, method: :post %>
When I try to use some embedded ruby to add extra information to the link, I cannot get it to work:
<%= link_to( { controller: 'recommendations', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The code compiles but in the rendered, the link that is generated is the following:
<a controller="recommendations" id="38">
<p>Some Html</p>0
</a>
Any help would be appreciated. I think that it is a small problem with the syntax but I have tried all manner of brackets, spaces etc that I could think of without luck.
UPDATE: I have tried the following code without success:
<%= link_to( { controller: 'recommendations', action: 'create', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The HTML output is:
<a action="create" controller="recommendations" id="39">
<p>Some Html</p>0
</a>
This might not be important but as a side note, the create action doesn't have a helper function for links. When I run the
rake routes
command I get the following
...
recommendations GET /recommendations(.:format) recommendations#index
POST /recommendations(.:format) recommendations#create
new_recommendation GET /recommendations/new(.:format) recommendations#new
...
In my opinion this isn't a problem but it is a reason why code such as:
link_to create_recommendation_path
won't work. Finally, the intention of the link is to act as a 'like' button. It creates a recommendation and then displays the current page again. Once again, thanks for the help in advance.
The reason link_to create_recommendation_path doesn't work is because there is no named route for create_recommendation_path, only for recommendations_path. You can see the named routes in your routes list (which you have in your post above). The left most column that comes out of routes shows the named routes. Notice that recommendations#create doesn't have an entry in the list.
You could probably get the path you want with
<%= link_to recommendations_path(:offer_id => offer.id), :method => :post do %>
html stuff
<% end %>
This should post to a path that looks like
/recommendations?offer_id=<the offer id>
(except the post data will be in the headers not on the URL)
This will work if the create method going to do something like
Recommendation.create(params)
and the only parameter you need to create a new Recommendation is an offer_id
What I don't understand is why you're trying to POST with a link? Does creating a recommendation only require an offer id?
In your link_to you're only specifying a controller, you need to also specify the action otherwise it doesn't know where to route it to. Either use:
<%= link_to({ controller: 'recommendations', action: 'show', id: offer.id }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
Or
<%= link_to({ show_recommendations_path(id: offer.id) }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>

Link_to doesn't work when using acts-as-taggable-on with custom method

Or rather I don't know how to specify the route for it.
I have my controller setup us:
def tags
#clients = current_user.clients.find_tagged_with(params[:tag])
end
and my views
Tags:
<% for tag in #client.tags %>
<%= link_to tag.name, clients_path(:view =>'tag', :tag => tag.name) %>
<% end %>
Only problem is that the link (clients_path) goes back to index and not 'all.' I know it has to do with changing the clients_path to somehow tell it to use 'all'. But I don't know how.
Any help?
Thanks
You can check your routes using rake routes.
I'm not sure what you mean by 'all' but if this is a custom method added to routes, then you should be able to use all_clients_path instead of clients_path.

Resources