【问题标题】:Nested Form Rails UPDATING嵌套 Form Rails 更新
【发布时间】:2015-08-31 03:52:56
【问题描述】:

我希望他们是好的,需要用用户的个人资料和他们各自的汽车做嵌套表格。

每个用户都有自己的个人资料:

 class PerfilController < ApplicationController
      before_filter :authenticate_user!

   def index
   end

   def show
     @usuario = current_user
     @usuario.perfil ||= Perfil.new
     @perfil = @usuario.perfil
   end

  def update
     @usuario = current_user
     @perfil = @usuario.perfil
     respond_to do |format|
          if @usuario.perfil.update_attributes(perfil_params)
              format.html {redirect_to @usuario, notice: "Datos Actualizados" }
         else
              format.html {render action: "show"}
         end
      end
   end

   private
    def perfil_params
       params.require(:perfil).permit(:nombre, :apellido, :rut)

 end
 end

我希望该基金确保每次用户发布汽车时,您都可以更新您的个人资料。副嵌套表格

 <%= simple_form_for(@auto) do |f| %>
   <%= f.error_notification %>

   <%=f.fields_for @perfil do |perfil_builder|%>
         <p>
         <%= perfil_builder.label :nombre %><br/>
         <%= perfil_builder.text_field :nombre %>
         <%= perfil_builder.label :apellido %><br/>
         <%= perfil_builder.text_field :apellido %>
         <%= perfil_builder.label :rut %><br/>
         <%= perfil_builder.text_field :rut %>
         </p>
     <%end%>

   <div class="form-inputs">
      <%= f.association :region %>
      <%= f.association :ciudad %>
      <%= f.association :marca %>
      <%= f.input :modelo %>
      <%= f.input :version %>
      <%= f.input :año %>
      <%= f.input :patente %>
      <%= f.association :tipo_transmision %>
      <%= f.association :combustible %>
      <%= f.association :tipo_vehiculo %>
      <%= f.association :carroceria %>
 </div>

   <div class="form-actions">
      <%= f.button :submit, :id => 'submit' %>
   </div>
 <% end %>

问题是当你想要更新的时候不会改变用户数据但是如果你发布了文章。控制台中的错误是:Unpermitted parameter: perfil

最好的朋友。

【问题讨论】:

  • 您可以在帖子中添加自动控制器吗?汽车模型呢?

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 nested-forms


【解决方案1】:

您正在向自动控制器发送请求,因此出现Unpermitted parameter: perfil 错误并且更新失败。

您可以将其添加到您的 Perfil 控制器中:

has_many: :autos
accept_nested_attributes_for: :autos

然后在视图中像这样切换它:

<%= simple_form_for(@perfil) do |f| %>
    <%= f.error_notification %>
    <%= f.label :nombre %><br/>
    <%= f.text_field :nombre %>
    ...
    <%=f.fields_for :auto do |auto_builder|%>
        <p>
        <%= auto_builder.association :region %><br/>
    ...

查看此页面了解更多信息:http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

【讨论】:

    猜你喜欢
    • 2012-11-16
    • 2016-05-29
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多