Rails: correct redirection but wrong url in address bar after creating in DB

In my rails application, I have a create button as such

def create @client = Client.find(params[:client_id]) @inventory = @client.inventories.create(params[:inventory]) redirect_to client_path(@client) end 

which, when creating the inventory (as part of the client, for example, the has_many client’s inventory, the inventory belongs to the clients), the inventory is added to the client in the database and redirected to localhost: 3000 / client / (regardless of the Client ID)

However, I have a problem with my program, because although it does the correct redirection, the address in the address bar after clicking create is localhost: 3000 / client / 1 / inventory / 1 ... and I only want to be local: 3000 / client / 1 /. If I'm actually trying to access localhost: 3000 / client / 1 / inventoryories / 1, this gives me an error because I don't have a show for inventory.

How does this possibly make the correct redirect, but the wrong URL is displayed in my browser? By the way, this is in my route.rb, which does not seem to me a problem.

  resources :clients do resources :inventories end 

Why is my application behaving like this? Any taker? :]

EDIT

When I pick up rake routes, I see it. This The paths of creation and destruction seem to be wrong. How can I change them?

+4
source share
2 answers

A workaround to this problem is that the inventory controller and adding

  def index @client = Client.find(params[:client_id]) redirect_to client_path(@client) end 

It seems like this was the best way to handle the back button by clicking on these errors, it looks like you cannot control the address displayed in the address bar. At least we don’t know how ..: P

+1
source

In this case, use:

 redirect_to client_path(@client.id) 
0
source

Source: https://habr.com/ru/post/1416483/


All Articles