Get id information. I hope it will be helpful for the solution procedure when the parameter related problems do not work.
ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina google chrome
This time I stumbled on getting the parameter id, so I will write it as a memorandum.
--Posting function has been implemented --Try to implement the delete function --Description of controller, routing, and view --destro becomes nil.
We will solve these.
app/controllers/img_searchs_controller.rb
def destroy
# binding.pry
@img_searchs = current_customer.img_searchs
@img_search = @img_searchs.find_by(id: params[:id])
unless @img_search
redirect_to new_img_search_path
end
if @img_search.destroy
redirect_to request.referer
else
render :new
end
end
Write and check binding.pry, @img_search = @ img_searchs.find_by (id: params [: id]) becomes nil. The description itself seems to be okay, but I can't pass the id. When I check params, id ='new'. (* I will explain later, but if I could confirm this well, the solution was quick) → There is a possibility that the description of view is incorrect.
app/views
<% @img_searchs.each do |img_search| %>
...
<%= link_to "Delete", new_img_search_path(img_search), method: :delete %>
<% end %>
The variables themselves match in each statement, so there seems to be no mistake in description. However, when I used the destroy method so far, img_search_path(img_search), method: :delete Since there was no new like this, it felt a little strange. → Check the routing.
config/routes.rb
resources :img_searchs, only: [:create, :destroy, :new]
Matching. .. .. Just in case, do the following as well.
Terminal
$ rails routes
new_img_search GET /img_searchs/new(.:format)
public/img_searchs#new
DELETE /img_searchs/:id(.:format)
public/img_searchs#destroy
After all it is suitable. .. .. Even if I check the view again, the description itself is still correct. But when you hover over the delete link on the screen. .. .. (In google chrome, the URL of the link destination is displayed at the bottom left)
3000/img_searchs/new.15
Not in the form of / img_searchs /: id!
At this time, I had no idea why new was included.
However, I thought that the discomfort I mentioned earlier should be resolved,
I decided to rewrite the routing, though it's not very clean.
#### **`config/routes.rb`**
```rb
resources :img_searchs, only: [:create, :new]
delete 'img_searchs/:id' => 'img_searchs#destroy'
And when you run it again
Terminal
$ rails routes
new_img_search GET /img_searchs/new(.:format)
public/img_searchs#new
DELETE /img_searchs/:id(.:format)
public/img_searchs#destroy
The result was exactly the same. So, I decided to specify the path with as.
config/routes.rb
resources :img_searchs, only: [:create, :new]
delete 'img_searchs/:id' => 'img_searchs#destroy', as: 'img_search'
And when I execute it again, an error occurs as shown below. The path name has already been used as the content. And that.
Terminal
$ rails routes
rails aborted!
ArgumentError: Invalid route name, already in use: 'img_search'
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
...
That's stupid! When I think about it, I was using as:'img_search' in the URL of another page. This is the cause of this time. Therefore, change the path name as shown below.
config/routes.rb
resources :img_searchs, only: [:create, :new]
delete 'img_searchs/:id' => 'img_searchs#destroy', as: 'img_search_destroy'
Then change the view pathname.
app/views
<% @img_searchs.each do |img_search| %>
...
<%= link_to "Delete", img_search_destroy_path(img_search), method: :delete %>
<% end %>
It worked fine.
This time I needed to fix it quickly, so Instead of changing the existing as:'img_search' Changed the newly added as:'img_search' to as:'img_search_destroy'.
Another factor that has happened this time is It happened because I had forgotten the priorities of routes.rb myself, so I hope it helps resolve those who have the same error.
By the way, the priority order of routes.rb is the one described above.
There are many ways to resolve errors, so it seems necessary to accumulate them step by step.
Recommended Posts