在Rails中更新嵌套的fields_for和collection_select

时间:2016-10-27 23:59:10

标签: ruby-on-rails forms

真的难倒在这里。我正在尝试让我的表单更新edit表单上的类别。问题是,除了类别之外,我提交的表格中的所有内容都会更新。它最终会插入所选择的新类别,就像它通过create方法而不是update方法一样,因此当提交后再次显示编辑表单时,它会使类别的字段加倍。每次提交后1,然后是2,然后是4,然后是8等。请帮助任何人。欣赏它。

视图/ blog_posts / edit.html.erb

<div class="col-md-6 col-md-offset-3 blog-submit">
            <%= form_for @blog_post do |b| %>


             <%= b.label :title %>
             <%= b.text_field :title %><br>

                <%= b.fields_for :categorizations do |cat| %>
                <%= cat.label :category_name, "Category 1" %>
                <%= cat.collection_select(:category_id, Category.all, :id, :category_name, {blank: "Select Category"}) %>
                <%= link_to "Add Categories", new_category_path %>
                  <br>
                <% end %>

                <%= b.submit "Submit", class: "btn btn-primary" %>
          <% end %>
        </div>

Blog_post控制器:

class BlogPostsController < ApplicationController
    protect_from_forgery
    before_action :authenticate_admin!, only: [:new, :edit]

    def index
      @blog_posts = BlogPost.order(id: :desc)
    end

    def new
      @blog_post = BlogPost.new
      @blog_post.categorizations.build.build_category
      @blog_post.categories.build
    end

    def edit
         @blog_post = BlogPost.find(params[:id])
    end

    def create
      @blog_post = BlogPost.new(blog_post_params)
      respond_to do |format|
          if @blog_post.save
            format.html { redirect_to @blog_post, notice: 'Your blog was submitted successfully' }
            format.json { render :show, status: :created, location: @blog_post }
          else
            format.html { render :new }
            format.json { render json: @blog_post.errors, status: :unprocessable_entity }
          end
        end
        puts @blog_post.errors.inspect
    end

    def update
        @blog_post = BlogPost.find(params[:id])
        if @blog_post.update_attributes(blog_post_params)
          render 'show'  
        else
          render 'edit'
        end
    end

    def show
        @blog_post = BlogPost.find(params[:id])

    end



private

  def blog_post_params
    params.require(:blog_post).permit(:title, :content, :posted_by, :comments, :blog_pic, {categorizations_attributes: [:category_id, :category_name]})
  end

end

模型:

class BlogPost < ApplicationRecord
    has_many :categorizations
    has_many :categories, :through => :categorizations
    accepts_nested_attributes_for :categorizations
    has_many :comments
    mount_uploader :blog_pic, BlogPicUploader
end

class Categorization < ApplicationRecord
    belongs_to :blog_post
    belongs_to :category
end

class Category < ApplicationRecord
    has_many :categorizations
    has_many :blog_posts, :through => :categorizations 
end

1 个答案:

答案 0 :(得分:2)

id中添加blog_post_params,如下所示。这对你有用。

def blog_post_params
    params.require(:blog_post).permit(:title, :content, :posted_by, :comments, :blog_pic, {categorizations_attributes: [:id,:category_id, :category_name]})
  end