Rails 4:在更新时传递父对象的嵌套属性

时间:2015-03-21 19:10:27

标签: ruby-on-rails ruby-on-rails-4 nested-attributes

所以,我试图更新嵌套对象,但出于某种原因,父控制器的apps_params并不满意,因为app不是&#39传递给它。看看下面,让我知道是否应该发布更多代码。

控制台错误如下:

Started PUT "/apps/1" for 127.0.0.1 at 2015-03-21 14:00:12 -0500
Processing by AppsController#update as JSON
  Parameters: {"authenticity_token"=>"0Z/SxhhEO6OA6eiShd+1PZI0DQ9QK1I7G8wqoInz+vM=", "@app"=>{"element"=>{"@app"=>{"el
ent.name"=>"hgfd"}}}, "id"=>"1"}
  App Load (1.0ms)  SELECT  "apps".* FROM "apps"  WHERE "apps"."id" = ? LIMIT 1  [["id", 1]]
Completed 400 Bad Request in 2ms

ActionController::ParameterMissing (param is missing or the value is empty: app):
  app/controllers/apps_controller.rb:73:in `app_params'
  app/controllers/apps_controller.rb:45:in `block in update'
  app/controllers/apps_controller.rb:44:in `update'

apps_controller.rb中引用的行:

  def update
    respond_to do |format|
      if @app.update(app_params)
        format.html { redirect_to @app, notice: 'App was successfully updated.' }
        format.json { render :show, status: :ok, location: @app }
      else
        format.html { render :edit }
        format.json { render json: @app.errors, status: :unprocessable_entity }
      end
    end
  end

private
def app_params
  params.require(:app).permit(:name, :infivewords, :description, elements_attributes: [:name, :description])
end

我的Apps展示View

<% @app.elements.each do |element| %>
  <tr>
        <td>    
              <span class="rest-in-place" data-url="<%= url_for @app %>" data-object="@app[element]" data-attribute="@app[element.name]" data-placeholder="Enter description">
                <%= element.name %>
        </td>
        <td><h2><%= element.description %></h2></td>

我已经无休止地试图弄清楚View,试图弄清楚为什么app_id没有通过,但我感到茫然,并且似乎无法找到我在任何地方寻找的信息。

感谢任何帮助。谢谢。

编辑:我应该在我的路线中添加它,我有两个独立的资源块。一个元素在应用程序内部,另一个元素在元素内部。我回到办公桌后,我会发布代码。

EDIT2 - 应用表单视图:

<%= form_for(@app) do |f| %>
  <% if @app.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@app.errors.count, "error") %> prohibited this app from being saved:</h2>

      <ul>
      <% @app.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :infivewords %><br>
    <%= f.text_field :infivewords %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

1 个答案:

答案 0 :(得分:1)

我对rest_in_place不熟悉,但我会仔细检查这一行的数据属性

<span class="rest-in-place" data-url="<%= url_for @app %>" data-object="@app[element]" data-attribute="@app[element.name]" data-placeholder="Enter description">

也许更像是

<span class="rest-in-place" data-url="<%= url_for @app %>" data-object="app[elements]" data-attribute="name" data-placeholder="Enter description">