Eric
Eric

Reputation: 793

Missing Template error if f.collection option is not selected

In my form, if a user leaves a required field blank and tries to create the post, the correct message pops up that they need to fill in that required field first.

But if the user doesn't select an option from the drop down f.collection_select menu and tries to submit the form, it doesn't let the user know that they need to choose an option but insteadi get a missing template error.

There is no error however if all the fields are filled in and post goes to the show page as intended, so i'm confused why it's bringing up a missing template error if any of the required f.collection options are not selected.

    div class="panel-body">

        <%= simple_form_for(@job, html: {multipart: true}) do |f| %>
        <%=f.collection_select :category_id,Category.all, :id ,:name, {:prompt=> "Select Category" ,:required=> true} %>
        <%=f.collection_select :city_id, City.all, :id ,:name,:prompt=> "Select City" %>


        <div class="form-group mb-3">
        <br>
        <%= f.input :title,required: true,label: "Your Business Name (required)" %>
        <%= f.input :description,required: true,label: "Tell Us About Your Service (required)" %>
        </div>

    <%= button_tag(type: "submit", class: "btn btn-primary") do %>
    <span><i class="glyphicon glyphicon-wrench"></i> List Service</span>
        <% end %> 

Missing Template error message

 Missing template jobs/New, application/New with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}

enter image description here

Upvotes: 1

Views: 108

Answers (1)

Dimitrius Lachi
Dimitrius Lachi

Reputation: 1287

Check what the action does in your controller, may you are redirecting you user for a path with no have template, this error is the correct.

Otherwise you can check if your form is sending the correct HTTP method, look eht example:

The path:

POST http://url.com/resources 

Will send you user to #create method (usually)

The same path GET http://url.com/resouces Will send you user to a #index, needs a template to render

OBS: I'am new in the platform, can't comment yet, try to leave a description of your error.

UPDATE

The error (Missing Template) is because yout controller is trying to render new and you don't have a jobs.new.html.erb file.

For solve the required change your collection to this:

<%= f.collection_select(:city_id, City.all, :id, :name, {:prompt => 'Choose a city'}, {:required => true}) %> 

Upvotes: 1

Related Questions