访问深度嵌套的params Rails
我正在控制器中编写一段代码,该代码应该以 00:00 的形式获取用户输入的数据,并将其转换为分钟,然后将其发送到模型。我使用的代码非常简单,只需在冒号处拆分 :duration 参数,然后将小时乘以 60,然后将分钟添加到该数字上。当我提交 for 时,当它尝试对 nil 对象执行拆分时,我收到错误。这肯定意味着我没有正确访问参数,但我对如何导航到深度嵌套参数感到困惑,并且似乎无法找到有关深度嵌套参数的任何文档。
这是我的模型组织:
log_entry >>
has many
workouts >>
has many
workout_times, which has :duration as an attribute
这是我的 log_entries_controller 中的代码:
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
我已经尝试了所有我能想到的方法来编写参数部分,但我似乎无法正确执行。我对 Rails/编程很陌生,所以可能有一些完全明显的我遗漏的东西......
编辑
以下是过去参数的日志信息:
Parameters: {
"commit" => "Save",
"log_entry" => {
"date(1i)" => "2011",
"date(2i)" => "10",
"date(3i)" => "13",
"workouts_attributes" => {
"0" => {
"time_of_day" => "AM",
"summary" => "",
"workout_times_attributes" => {
"0" => {
"duration" => "2:00",
"zone" => "1",
"_destroy" => "false"
}
}
}
}
},
"authenticity_token"=>"zD6qS6jOSQ3/mRyH7RAgAzWYmwHub0+uBG1sjPVvkEY=",
"utf8"=>"\342\234\223"
}
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.
Here is my model organization:
log_entry >>
has many
workouts >>
has many
workout_times, which has :duration as an attribute
This is the code within my log_entries_controller:
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
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...
EDIT
Here is the log info of parameters past:
Parameters: {
"commit" => "Save",
"log_entry" => {
"date(1i)" => "2011",
"date(2i)" => "10",
"date(3i)" => "13",
"workouts_attributes" => {
"0" => {
"time_of_day" => "AM",
"summary" => "",
"workout_times_attributes" => {
"0" => {
"duration" => "2:00",
"zone" => "1",
"_destroy" => "false"
}
}
}
}
},
"authenticity_token"=>"zD6qS6jOSQ3/mRyH7RAgAzWYmwHub0+uBG1sjPVvkEY=",
"utf8"=>"\342\234\223"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以像这样将你想要的参数分配给你的模型:
然后在你的convert_duration方法中执行:
在Ruby中,返回方法的最后一个语句,因此这会将计算的持续时间返回到save方法,该方法将设置持续时间我们根据传递给该方法的参数创建的锻炼对象的锻炼时间对象。
You could just assign the parameter you want to your model like so:
Then in your convert_duration method do:
In Ruby the last statement of a method is returned, so this would return your calculated duration back to the save method which will set the duration on the workout_time object for the workout object we created from the parameters passed to the method.