Rails嵌套表单不保存或验证更新

时间:2013-12-24 03:19:47

标签: ruby-on-rails forms nested

我有以下控制器:

class Admin::ArticleChangeRequestsController < Admin::ApplicationController
  before_action :set_order, only: :index
  before_action :set_article_change_request, only: [:edit, :delete]
  helper SortClassHelper

  protected
  def set_order
    if params[:column].present?
      @column = params[:column]
    else
      @column = 'created_at'
    end

    if params[:sort].present?
      @sort = params[:sort]
    else
      @sort = 'desc'
    end

    @order = @column.to_s + ' ' + @sort.to_s
  end

  def set_article_change_request
    @article_change_request = ArticleChangeRequest.find(params[:id])
  end

  def article_change_request_params
    params[:article_change_request].permit(:title, :change, :article)
  end

  public
  def index
    @article_change_requests = ArticleChangeRequest.order(@order).page params[:page]
  end

  def edit
    if request.patch?
      if @article_change_request.update(article_change_request_params)
        redirect_to :admin_article_change_requests, :flash => { success: t(:article_change_request_updated) }
      end
    end
  end

  def delete
    if request.post?
      if @article_change_request.delete
        flash[:success] = t(:article_change_request_deleted)

        render :json => { :success => true }
      else
        render :json => { :success => false }
      end
    end
  end
end

并添加:

accepts_nested_attributes_for :article

在我的ArticleChangeRequest模型中。但是每当我点击Update时它都不会验证或保存文章。这是我的表格:

<%= form_for @article_change_request, { :url => :admin_article_change_request_edit } do |f| %>
  <%= render partial: 'form', locals: { f: f }  %>
  <div>
    <%= f.submit t(:update), class: 'submit' %>
  </div>
<% end %>

<% if @article_change_request.errors.any? %>
  <div class="message errormsg">
    <p>
      <%= t :there_is %> <%= pluralize @article_change_request.errors.count, t(:error) %>
    </p>
  </div>
<% end %>
<div class="container">
  <%= f.label :title, t(:change_request_title) + ':' %>
  <%= f.text_field :title, size: 40, class: 'text' %>
  <% unless @article_change_request.errors[:title].blank? then %>
  <span class="note error">
    <% @article_change_request.errors[:title].each do |e| %>
      <% if e == @article_change_request.errors[:title].last %>
        <%= e %>
      <% else %>
        <%= e %>,
      <% end %>
    <% end %>
  </span>
  <% end %>
</div>
<div class="container">
  <%= f.label :change, t(:change_request_change) + ':' %>
  <%= f.text_area :change, size: '100x5' %>
  <% unless @article_change_request.errors[:change].blank? then %>
  <span class="note error">
    <% @article_change_request.errors[:change].each do |e| %>
      <% if e == @article_change_request.errors[:change].last %>
        <%= e %>
      <% else %>
        <%= e %>,
      <% end %>
    <% end %>
  </span>
  <% end %>
</div>
<hr>
<%= f.fields_for :article do |a| %>
<div class="container">
  <%= a.label :category_id, t(:article_category) + ':' %>
  <%= a.select :category_id, [''].concat(Category.select_options) %>
  <% unless @article_change_request.article.errors[:category_id].blank? then %>
  <span class="note error">
    <% @article_change_request.article.errors[:category_id].each do |e| %>
      <% if e == @article_change_request.article.errors[:category_id].last %>
        <%= e %>
      <% else %>
        <%= e %>,
      <% end %>
    <% end %>
  </span>
  <% end %>
</div>
<div class="container">
  <%= a.label :title, t(:article_title) + ':' %>
  <%= a.text_field :title, size: 40, class: 'text' %>
  <% unless @article_change_request.article.errors[:title].blank? then %>
  <span class="note error">
    <% @article_change_request.article.errors[:title].each do |e| %>
      <% if e == @article_change_request.article.errors[:title].last %>
        <%= e %>
      <% else %>
        <%= e %>,
      <% end %>
    <% end %>
  </span>
  <% end %>
</div>
<div class="container">
  <%= a.label :slug, t(:article_slug) + ':' %>
  <%= a.text_field :slug, size: 40, class: 'text slug' %>
  <% unless @article_change_request.article.errors[:slug].blank? then %>
  <span class="note error">
    <% @article_change_request.article.errors[:slug].each do |e| %>
      <% if e == @article_change_request.article.errors[:slug].last %>
        <%= e %>
      <% else %>
        <%= e %>,
      <% end %>
    <% end %>
  </span>
  <% end %>
</div>
<div class="container">
  <%= a.label :article_type, t(:article_type) + ':' %>
  <%= a.select :article_type, [''].concat(ArticleType.select_options) %>
  <% unless @article_change_request.article.errors[:article_type].blank? then %>
  <span class="note error">
    <% @article_change_request.article.errors[:article_type].each do |e| %>
      <% if e == @article_change_request.article.errors[:article_type].last %>
        <%= e %>
      <% else %>
        <%= e %>,
      <% end %>
    <% end %>
  </span>
  <% end %>
</div>
<div id="article_content_container" class="container" style="display: none;">
  <%= a.label :content, t(:article_content) + ':' %>
  <%= a.text_area :content, size: '100x40', class: 'wysiwyg' %>
  <% unless @article_change_request.article.errors[:content].blank? then %>
  <span class="note error">
    <% @article_change_request.article.errors[:content].each do |e| %>
      <% if e == @article_change_request.article.errors[:content].last %>
        <%= e %>
      <% else %>
        <%= e %>,
      <% end %>
    <% end %>
  </span>
  <% end %>
</div>
<div id="article_link_container" class="container" style="display: none;">
  <%= a.label :link, t(:article_link) + ':' %>
  <%= a.text_field :link, size: 40, class: 'text' %>
  <% unless @article_change_request.article.errors[:link].blank? then %>
  <span class="note error">
    <% @article_change_request.article.errors[:link].each do |e| %>
      <% if e == @article_change_request.article.errors[:link].last %>
        <%= e %>
      <% else %>
        <%= e %>,
      <% end %>
    <% end %>
  </span>
    <% end %>
</div>
<div class="container">
  <%= a.label :summary, t(:article_summary) + ':' %>
  <%= a.text_area :summary, size: '100x5' %>
  <% unless @article_change_request.article.errors[:summary].blank? then %>
  <span class="note error">
    <% @article_change_request.article.errors[:summary].each do |e| %>
      <% if e == @article_change_request.article.errors[:summary].last %>
        <%= e %>
      <% else %>
        <%= e %>,
      <% end %>
    <% end %>
  </span>
  <% end %>
</div>
<% end %>

有谁可以告诉我为什么它没有验证或更新?

1 个答案:

答案 0 :(得分:1)

params[:article_change_request].permit(:title, :change,
:article_attributes => {:category_id, :title, :slug, :article_type})