I am really new in web development. I just started some weeks ago reading the ruby on rails tutorial and before it I only knew a bit of C+. So the question is probably easy for you but I have already spent 2 days looking for an answer which I didn't find. What I have is a form_for with a data field which appear/disappear depending on the radio button clicked. This works fine, when I submit the form the data are saved in the database correctly! The problem is that I am not redirected at all. In the shell it is written
render patient/show.html.erb within layouts/application Completed 200 ok
BUT in the browser I still see the /srtinitial_infos/new (ie the page where I fill in the form).
My code is
#new.html.erb
<%= form_for(:srtinitial_infos, url: {action: "create"},remote: true) do |f|%>
<%= javascript_include_tag "srtinitial_infos_new.js" %>
<p>
<strong>Operations </strong>
<%= f.radio_button :operation, '1', :value=>1%>
<%=label :operation_yes,'Yes'%>
<%= f.radio_button :operation, '0', :value=>0%>
<%=label :operation_no,'No'%>
<%= f.radio_button :operation, '2', :value=>2%>
<%=label :operation_ni,'No info'%>
</p>
<div id="opdate">
<p>
<strong>Date of operation </strong>
<%=f.date_select :operationdate%><br>
<p>
</div>
<p>
<strong>Date of MRI </strong>
<%=f.date_select :mridate%><br>
<p>
<%=f.submit "Save" %>
The Javascript
#javascript/srtinitial_infos_new.js.erb
$(function(){
$("#opdate").hide();
$("input:radio[name='srtinitial_infos[operation]']").change(function(){
if(this.value == 1 && this.checked){
$("#opdate").show();
}else{
$("#opdate").hide();
}
});
});
The controller
def create
@patient=Patient.find(params[:patient_id])
if @patient.srtinitial_info
render 'show'
else
@[email protected]_srtinitial_info(srtinitial_infos_params)
@srtinitial_info.author=@current_user.email
@srtinitial_info.save
redirect_to patient_path(@patient)
end
end
Since I think that the problem is in the controller since data are saved correctly, I also try to write stuff like
def create
@patient=Patient.find(params[:patient_id])
@[email protected]_srtinitial_info(srtinitial_infos_params)
@srtinitial_info.author=@current_user.email
respond_to do |format|
if @patient.srtinitial_info
format.html { render action: "show"}
else @srtinitial_info.save
format.html { redirect_to @patient, notice: 'Correctly initialized.'}
format.js {}
end
I tried similar stuff in the new controller
def new
@patient=Patient.find(params[:patient_id])
respond_to do |format|
format.html # new.html.erb
format.js
end
end
If I do not use the remote: true everything works well (apart the hide/show option obviously) I hope that the question is clear :) Cheers
remote: true
is that it won't redirect you, this is Ruby's unobtrusive javascript way of creating an AJAX form instead of a standard form. In this case your server is saying it's rendering the show because it is, that's how it's responding to the AJAX request. I'm not sure why your hide/shows would not be working withoutremote: true
however. – Clarkremote: true
, it becomes a ajax request and the block forformat.html
will never be executed. – Abhi