嵌套表格不保存导轨4

时间:2015-09-05 00:37:50

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

我在一周前遇到了嵌套表单的问题。我有一个名为用户的模型另一个Auto和另一个Perfil。我需要在用户创建新的Auto时,您可以根据需要更改您的perfil数据

你能帮助我吗?我们将非常感激

尝试以下方法:

用户模型

 class User < ActiveRecord::Base
   has_many :autos
   has_one :perfil
     devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable
   end

自动模型

 class Auto < ActiveRecord::Base
   belongs_to :user
   belongs_to :perfil
   accepts_nested_attributes_for :perfil, allow_destroy: true
 end

Perfil模型

 class Perfil < ActiveRecord::Base
    belongs_to :user
    has_many :autos
 end

自动控制器

 class AutosController < ApplicationController
   before_action :set_auto, only: [:show, :edit, :update, :destroy]


   def new
     @auto = Auto.new
     @perfil = @auto.build_perfil
   end


   def create
     @auto = Auto.new(auto_params)
     @auto.user_id = current_user.id
     @auto.perfil_id = current_user.perfil.id

    respond_to do |format|
       if @auto.save
        format.html { redirect_to @auto, notice: 'Auto was successfully created.' }
         format.json { render :show, status: :created, location: @auto }
      else
        format.html { render :new }
        format.json { render json: @auto.errors, status: :unprocessable_entity }
      end
    end
   end

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


  private

     def set_auto
      @auto = Auto.find(params[:id])
     end

     def auto_params
      params.require(:auto).permit(:perfil_id, :marca, :modelo, :año, :user_id, perfil_attributes: [:user_id, :nombre, :rut, :telefono, :direccion])
    end
end

Perfil控制器

 class PerfilsController < ApplicationController
   before_filter :authenticate_user!

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

   end


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

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

      end
    end


   private

    def perfil_params
       params.require(:perfil).permit(:user_id, :nombre, :rut, :telefono, :direccion)
    end
 end

和_form.html.erb

<%= simple_form_for @auto do |f| %>

     <div class="form-inputs">
     <%= f.input :marca %>
     <%= f.input :modelo %>
     <%= f.input :año %>

       <%= f.simple_fields_for :perfil, current_user.perfil do |p| %>
        <%= p.input :nombre %>
        <%= p.input :telefono %>
        <%= p.input :direccion %>
       <% end %>


  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
 <% end %>
 <%= link_to "Save", autos_path, :class => 'button_submit' %>

的routes.rb

 Rails.application.routes.draw do
   resources :autos
   devise_for :users
   resources :perfils, only: [:index,:show, :update , :create]
   root 'autos#index'

 end

1 个答案:

答案 0 :(得分:0)

几乎完美,只有2件小事可以帮助你。

1 fields_for只需要:perfil作为对象

<%= simple_form_for @auto do |f| %>
  <div class="form-inputs">
  <%= f.input :marca %>
  <%= f.input :modelo %>
  <%= f.input :año %>
  <%= f.simple_fields_for :perfil do |p| %>
    <%= p.input :nombre %>
  <%= p.input :telefono %>
  <%= p.input :direccion %>
<% end %>
</div>
<div class="form-actions">
  <%= f.button :submit %>
</div>
 <% end %>
<%= link_to "Save", autos_path, :class => 'button_submit' %>

2控制器需要将对象创建为当前用户

def create
  @auto = current_user.autos.new(auto_params)
  respond_to do |format|
    if @auto.save
      format.html { redirect_to @auto, notice: 'Auto was successfully created.' }
      format.json { render :show, status: :created, location: @auto }
    else
      format.html { render :new }
      format.json { render json: @auto.errors, status: :unprocessable_entity }
    end
  end
end