I'm a beginner with Rails and my current project requires me to load a specific element on the same page without affecting other elements. To achieve this, I created a new route in routes.rb as follows:
get "/sell_used_car/edit", to:"sell_used_car#edit", as: :sell_used_car_edit
The view of the page located at "views/sell_used_car/new.html.erb" looks like this:
<%= link_to "Change Email", sell_used_car_edit_path, remote: true %>
<div id = "content"></div>
In the sell_used_car_controller.rb file, I have included the following code:
def edit
respond_to do |format|
# format.html{}
format.js
end
end
When I uncommented the line format.html{}, I encountered an error stating: "SellUsedCarController#edit is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []"
If I kept that line commented out, I received an error saying: "ActionController::UnknownFormat"
I've made sure that the files edit.js.erb and _edit.html.erb are placed correctly. Here's how they look:
edit.js.erb
$('#content').html("<%= escape_javascript(render :partial => 'edit')%>");
_edit.html.erb
<%= form_with do |form| %>
<div class="d-flex align-items-center justify-content-center flex-column">
<div class="mb-3">
<%= form.label :Enter_Your_New_Email%>
<%= form.text_field :email, placeholder: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2cacbc8f2d3d0d19cd1dddf">[email protected]</a>", class: "form-control"%>
</div>
</div>
<%end%>
I understand that including the line format.html{} causes an error because it cannot find the .html.erb file even though it should render to a partial file according to my edit.js.erb. Despite only using the code in the controller like this:
def edit
respond_to do |format|
# format.html{}
format.js {render :edit}
end
end
The same error persists as "ActionController::UnknownFormat". I'm really stuck here.
Appreciate any help in advance.