Note that there are some explanatory texts on larger screens.

plurals
  1. POaccessing deeply nested params rails
    text
    copied!<p>I am writing a piece of code in my controller that is suposed to take the data a user inputs in the form of 00:00 and convert that into minutes before sending it to the model. The code I am using is pretty simple, just split the :duration parameter at the colon and multiply hours by 60 and add the minutes to that number. When I submit the for I get an error when it tries to perform split on a nil object. This must mean I am not accessing the parameter correctly but I am confused as to how I would navigate to a deeply nested parameter and cant seem to find any documentation regarding deeply nested parameters. </p> <p>Here is my model organization:</p> <pre><code>log_entry &gt;&gt; has many workouts &gt;&gt; has many workout_times, which has :duration as an attribute </code></pre> <p>This is the code within my log_entries_controller:</p> <pre><code>def convert_duration hours, minutes = params[:log_entry][:workout][:workout_time][:duration].split(":") params[:log_entry][:workout][:workout_time][:duration] = (hours.to_i * 60 + minutes.to_i) end </code></pre> <p>I have tried all the ways I could think of to write the params part but I cants seem to get it right. I am pretty new to rails/programming so there might be somthing totally obvious I am missing...</p> <p>EDIT</p> <p>Here is the log info of parameters past: </p> <pre><code>Parameters: { "commit" =&gt; "Save", "log_entry" =&gt; { "date(1i)" =&gt; "2011", "date(2i)" =&gt; "10", "date(3i)" =&gt; "13", "workouts_attributes" =&gt; { "0" =&gt; { "time_of_day" =&gt; "AM", "summary" =&gt; "", "workout_times_attributes" =&gt; { "0" =&gt; { "duration" =&gt; "2:00", "zone" =&gt; "1", "_destroy" =&gt; "false" } } } } }, "authenticity_token"=&gt;"zD6qS6jOSQ3/mRyH7RAgAzWYmwHub0+uBG1sjPVvkEY=", "utf8"=&gt;"\342\234\223" } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload