1
votes

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

1
The purpose of 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 without remote: true however.Clark
When you do remote: true, it becomes a ajax request and the block for format.html will never be executed.Abhi
@Clark you are right! Actually the_remote: true_ is useless in my case. In one of my versions it was useful but in this one it is not. Thanks a lot!!ToTo10

1 Answers

0
votes

You can use .js.erb files to respond to AJAX requests. In this case you need show.js.erb in the same views folder as show.html.erb. The file uses javascript to update the page without a refresh/redirect.

Assuming you have a div with id notice where you want to display the message, your show.js.erb might just look like this:

$("#notice").html("<%= flash [:notice] %>"):

If you want to redirect to another page, then you could do a client-side redirect from the controller:

window.location = "<%= patient_path @user %>";