0

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 ?

Thierry
  • 111
  • 2
  • 14

1 Answers1

0

You want to test @company.exists? and @prequalification.exists instead of Company.exists and Prequalification.exists, otherwise your are testing if the Class exists (which is always the case) and not if the actual company and prequalifcation exist.

This is why you are getting an error on the edit company link, since Company.exists is true even when @company = nil, it tries to build a link for a nil object

Vincent Rolea
  • 1,601
  • 1
  • 15
  • 26
  • Vincent. thanks. Your suggestions are solving the issue (with true and nil, can't get to make it work with x.exists?. Where I am surprised is when rails build a link by calling another controller. – Thierry Jan 07 '19 at 18:16
  • It does not call another controller. The error is generated because the url generator gets `nil` at line 11 instead of an valid `id`, and your `routes.rb` states that in order to edit a company, you need to pass an valid id. (hence the `possible unmatched constraints: [:id]`) – Vincent Rolea Jan 08 '19 at 09:53