Rails - NoMethodError(未定义方法'permit'代表“reps”:string)

时间:2017-01-14 19:34:22

标签: html ruby-on-rails

所以我试图在Rails 5中从单个表单创建多个对象。这在rails 4.2中运行得非常好,我不确定是什么导致错误(我想它与之有关)视图)。

升级到rails 5之后我遇到的另一个问题是现在我的表单只提交了最后一条记录。我使用本演练作为参考:http://vicfriedman.github.io/blog/2015/07/18/create-multiple-objects-from-single-form-in-rails/

This is the input和 这是我发送的参数。

Parameters: {"utf8"=>"√", "authenticity_token"=>"3j8A0oNW3sM5VIi11ZEHwpwlAhvY3
vml1kcLHfWdptySKSeJokRmBXObh/HEFBwG4/QC8PdfX7NKbkrGjOfI6Q==", "exercise_id"=>"16
5", "exercise_sets"=>{"reps"=>"1", "weight"=>"1"}, "exerciseName"=>"imsick", "co
mmit"=>"Save changes"}

错误

NoMethodError (undefined method 'permit' for "reps":string)

ExerciseSetsController:

def create
    if current_trainer
        savedMirror = current_trainer.exercises.find_by_name(params[:exerciseName])

        params['exercise_sets'].each do |set|
            #set[:timeFormat] = savedMirror.exercise_sets[0].timeFormat
            #set[:weightFormat] = savedMirror.exercise_sets[0].weightFormat
            #set[:distanceFormat] = savedMirror.exercise_sets[0].distanceFormat
            ExerciseSet.create(exercise_set_params(set))
        end

        flash[:notice] = "Sets added"
        redirect_to (:back)
    end
end

private 
def exercise_set_params(e_params)
  e_params.permit(:id, :reps, :weight, :weightFormat, :time, :timeFormat, :distance, :distanceFormat, :exercise_id, :_destroy)
end

查看:

<%savedMirror = current_trainer.exercises.find_by_name(e.name)%>
<%= form_tag  url_for(controller: "exercise_sets", action: "create") do%>
    <ul class="list-group">
        <% e.sets.times do |i|%>
        <%= fields_for "exercise_sets[]", @exerciseSet do |f| %>
        <li class="list-group-item">
            <label>Set: <%=i+1%></label>
                <%#= f.number_field :user_id, value: e.user_id, style:"visibility:hidden; margin:-15px;"%>
                <%= hidden_field_tag(:exercise_id, e.id) %>

              <div class = "row">
                <% if savedMirror.exercise_sets[0].reps == 1%>
                  <div class="col-sm-3">
                    <%= f.label :reps, "Reps" %><br>
                    <%= f.text_field :reps, class: "form-control"%>
                  </div>
                <%end%>
                <% if savedMirror.exercise_sets[0].weight == 1%>
                  <div class="col-sm-3">
                    <%= f.label :weight, "Weight" %><br>
                      <%= f.text_field :weight, class: "form-control"%>
                  </div>
                <%end%>
                <% if savedMirror.exercise_sets[0].time == 1%>
                  <div class="col-sm-3">
                    <%= f.label :time, "Time" %><br>
                      <%= f.text_field :time, class: "form-control"%>
                  </div>
                <%end%>
                <% if savedMirror.exercise_sets[0].distance == 1%>
                  <div class="col-sm-3">
                    <%= f.label :distance, "Distance" %><br>
                    <%= f.text_field :distance, class: "form-control"%>
                  </div>
                <%end%>
              </div>
        </li>
        <%end%>
        <%end%>
        <br>
        <%= hidden_field_tag(:exerciseName, e.name) %>
        <%=submit_tag%>
    </ul>
    <% end %>
<%end%>

非常感谢任何帮助,谢谢!

4 个答案:

答案 0 :(得分:0)

您需要添加.require(:exercise)(我假设您的模型的名称是练习)才能使用强参数。

如果您未通过该模型,则需要使您的应用能够提供错误。因为您列入白名单的参数是为该模型定义的。

只需替换

def exercise_set_params(e_params)
  e_params.permit(:id, :reps, :weight, :weightFormat, :time, :timeFormat, :distance, :distanceFormat, :exercise_id, :_destroy)
end

<强>与

def exercise_set_params(e_params)
  e_params.require(:exercise).permit(:id, :reps, :weight, :weightFormat, :time, :timeFormat, :distance, :distanceFormat, :exercise_id, :_destroy)
end

希望它有效!

答案 1 :(得分:0)

看似:reps:weight ,作为嵌套参数传递

"exercise_sets"=>{"reps"=>"1", "weight"=>"1"}

所以我很想尝试这个:

e_params.permit(:id, exercise_sets: [:reps, :weight], :weightFormat, :time, :timeFormat, :distance, :distanceFormat, :exercise_id, :_destroy)

注意嵌套:

exercise_sets: [:reps, :weight]

也许会这样做。

答案 2 :(得分:0)

解决了问题:

我没有将值作为哈希值传递,而是将它们作为数组传递。我通过将各个字段更改为:

来修复它
<input type="text" name="exercise_sets[][reps]" class="form-control" />

<input type="text" name="exercise_sets[][weight]" class="form-control" />

......等等。

答案 3 :(得分:0)

在我的情况下,我遇到了同样的问题,即在参数中使用axios发送ajax的数据,解决方案是在“数据”中添加了值,而不是“参数”中

axios({
        method:'patch',
        url:'/clients/'+ item +'/update.json',
        //params:{'client': {'info': info}},
        data:{authenticity_token:token,client:{info: info}},
        responseType:'json'
    })