Issues: when I clink on a link_to action, the app calls another controller causing a missing id error.
I have a menu that display buttons for creating new objects that convert into navigation links when the object is created by the user.
the behavior is using the object.exists? helper.
<div class="nav-wrapper">
<!-- start menu -->
<ul class="nav flex-column">
<li class="list-group-item"><%= link_to "Personal information", edit_user_registration_path %></li>
<li class="list-group-item"><%= link_to "Settings", notification_settings_path %></li>
<li class="list-group-item"><%= link_to "Payments", payment_method_path %></li>
<% if Prequalification.exists? %>
<li class="list-group-item"><%= link_to "Edit prequalification",edit_prequalification_path(@user, @prequalification) %></li>
<% end %>
<% if Company.exists? %>
<li class="list-group-item"><%= link_to "Edit company data",edit_company_path(@user, @company) %></li>
<% end %>
</ul>
<ul class="nav flex-column mt-5">
<% if !Company.exists? %>
<a class="btn btn-primary btn-block mt-1" href="companies/new">Add a company</a>
<% end %>
<% if !Prequalification.exists? %>
<a class="btn btn-primary btn-block mt-1" href="prequalifications/new">Prequalify for loan or rents</a>
<% end %>
</ul>
</div>
Thanks to this post:
https://stackoverflow.com/questions/13186722/what-is-the-difference-between-using-exists-and-present-in-ruby
I can create a new company and have access to the edit link, but when I try to create subsequently a prequalification file, then I get:
ActionController::UrlGenerationError in Prequalifications#new
No route matches {:action=>"edit", :controller=>"companies", :format=>nil, :id=>nil, :locale=>:en}, possible unmatched constraints: [:id]
The controllers for companies and prequalifications are separated. I do not understand why the error happens. Especially as if I delete the reference link_to the company model before actioning the prequalification then it seems to work as intented.
my routes are very straightforward:
resources :prequalifications
resources :companies
my controller is also quite simple:
def new
@company = Company.new
end
def create
@company = current_user.build_company(company_params
end
def edit
@user = current_user
@company = Company.find_by(id: [params[:id], params[:company_id]])
end
The controller for the other model look very similar.
Where could that behavior could come from ?