[Ruby on Rails] When parameter id acquisition does not work

Target

Get id information. I hope it will be helpful for the solution procedure when the parameter related problems do not work.

Development environment

ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina google chrome

Premise

This time I stumbled on getting the parameter id, so I will write it as a memorandum.

Status

--Posting function has been implemented --Try to implement the delete function --Description of controller, routing, and view --destro becomes nil.

We will solve these.

Doubt controller

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.

Doubt view

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.

Doubt routes

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.

Summary

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

[Ruby on Rails] When parameter id acquisition does not work
[Ruby on Rails] credential does not work in production environment even though I saved production.key
A memo when the incoming number acquisition using Broadcast Receiver does not work on Android 9/10
[Ruby on Rails] Search function (not selected)
When the Ruby on Rails terminal rolls back
When @Transactional of Spring Boot does not work
Final confirmation when ruby version does not switch
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Sidekiq-limit_fetch does not work
Note: Memory Analyzer does not work on Mac OS
[Ruby on Rails] Change URL id to column name
Correspondence when Ruby version does not switch in rbenv
Ruby on Rails Tutorial Troublesome notes when running on Windows
When the server does not start with rails s
[Railways] Railways does not work when the RubyMine project root is other than the Rails root directory.
[Ruby on Rails] If there are duplicate records when registering data, do not register
[Ruby on rails] When executing the heroku command, bash: heroku: command not found is displayed. [Rails tutorial]
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby version does not switch
[Rails] Couldn't find with'id'=: id does not execute update action
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
What to check when rails db: migration does not pass
(Ruby on Rails6) Display of the database that got the id of the database
Ruby on rails learning record-2020.10.07 ②
Ruby on Rails record search, create if not find_or_create_by method
[Rails] Solving the problem that session timeout does not work
Commentary on partial! --Ruby on Rails
Docker does not work when DOCKER_HOST is specified in WSL2
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Correspondence when Java application exed by launch4j does not work on windows with openJDK installed
What to do when Rails on Docker does not reflect controller changes in the browser
Ruby on Rails Basic Memorandum
[Rails 5] [Turbolinks] What to do when JS does not work due to page transition or browser back
Spring should be suspected when Rails commands do not work properly
Build a development environment where Ruby on Rails breakpoints work on Windows
Breakpoint doesn't work when running Rails Minitest in Ruby Test Explorer
[Rails] What to do when rails s does not respond or does not stop
[Beginner] When rails s doesn't work
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function