Ruby on rails 4 Rails 4 ActiveRecord与嵌套表单字段的关联

Ruby on rails 4 Rails 4 ActiveRecord与嵌套表单字段的关联,ruby-on-rails-4,rails-activerecord,Ruby On Rails 4,Rails Activerecord,我读了一半我可以得到我的手,虽然有非常清楚的解释我试图做的部分,我找不到一个最新的完整教程。我有页面和类别。页面有很多类别,类别有很多。我还设置了第三个模型categories_pages,并创建了一个索引联接表,其中包含两列it categority_id和page_id。因此,我的模型当前为: class Categorization < ActiveRecord::Base belongs_to :category belongs_to :page end class Cate

我读了一半我可以得到我的手,虽然有非常清楚的解释我试图做的部分,我找不到一个最新的完整教程。我有页面和类别。页面有很多类别,类别有很多。我还设置了第三个模型categories_pages,并创建了一个索引联接表,其中包含两列it categority_id和page_id。因此,我的模型当前为:

class Categorization < ActiveRecord::Base

belongs_to :category
belongs_to :page
end


class CategoriesPages < ActiveRecord::Base

belongs_to :category
belongs_to :page
end

class Page < ActiveRecord::Base

#categories
has_many :categories_pages
has_many :categories, :through => :categories_pages
end
编辑3:页面\u控制器

class PagesController < ApplicationController
  before_action :set_page, only: [:show, :edit, :update, :destroy]

  # GET /pages
  # GET /pages.json
  def index
      @pages = Page.all
  end

  # GET /pages/1
  # GET /pages/1.json
  def show
    @page = Page.new(page_params)
  end

  # GET /pages/new
  def new
    @category = Category.new
    @page = Page.new
  end

  # GET /pages/1/edit
  def edit

  end


  # POST /pages
  # POST /pages.json
  def create
    @page = Page.new(page_params)
    @category = @page.build_category(category_params)

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


  # PATCH/PUT /pages/1
  # PATCH/PUT /pages/1.json
  def update
    @page = Page.new(page_params)
    @category = @page.category

    @category.assign_attributes(category_params)
    respond_to do |format|
      if @page.update_attributes(page_params)
        format.html { redirect_to @page, notice: 'Page was successfully updated.' }
        format.json { render :show, status: :ok, location: @page }
      else
        format.html { render :edit }
        format.json { render json: @page.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /pages/1
  # DELETE /pages/1.json
  def destroy
    @page.destroy
    respond_to do |format|
      format.html { redirect_to pages_url, notice: 'Page was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_page
      @page = Page.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def page_params
      params.require(:page).permit(:title, :text, :image, :published, :seo_keywords, :seo_description)
    end

    def category_params
      params.require(:category).permit(:name)
    end
end
标签的表单_为标签生成所有必要的html,包括操作、方法等。看看生成的源代码,了解“魔力”的作用。您会看到“什么也没有发生”,因为您的提交按钮位于form_for块之外的另一个块中,该块没有设置操作属性。你看,不采取行动就会导致“不采取行动”

更新:

为了纠正这种行为,请按如下方式连接表单:

<%= form_for @page, html: { role: 'form' } do |f| %>
  <div id="error_explanation">
    <h2><%= pluralize(@page.errors.count, "error") %> prohibited this page from being saved </h2>
    <ul>
      <% @page.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  </div>
  <div class="form-group">
    <%= f.label :title %><br>
    <%= f.text_field :title, :class => 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :text %><br>
    <%= f.text_field :text, :class => 'form-control' %>
  </div>
  ...
  <div class="action">
    <%= f.submit 'Save', :class => 'btn btn-default' %>
  </div>
<% end %>

你有什么错误吗?另外,请说明确切的问题是什么。对不起。。我更新了问题。问题是表单提交按钮完全没有响应。值是否正确保存?请发布日志信息。如果它与类别有很多关系,那么这一行也应该是。好的,修复了我尝试一对一关系时遗留下来的类别问题。我粘贴了development.log,单击页面上的编辑,然后尝试更新和保存它。正如你所看到的,当我点击f.submitDone时,什么也没有发生。我只发布了pages控制器,因为我假设这是唯一相关的控制器?第二个表单是根据他们的说明制作的回形针gem。在我引入category字段之前,图片上传工作得很好。但是你的form_for block除了显示错误消息外不包含任何内容。我指的是blockOk。。。但这丝毫没有影响我的表单提交功能是一些引导的标记。我的观点是:在块的表单_中没有提交按钮!抱歉,如果我最后的评论不清楚。。。删除没有任何效果,因此它不是导致我当前问题的原因。
class PagesController < ApplicationController
  before_action :set_page, only: [:show, :edit, :update, :destroy]

  # GET /pages
  # GET /pages.json
  def index
      @pages = Page.all
  end

  # GET /pages/1
  # GET /pages/1.json
  def show
    @page = Page.new(page_params)
  end

  # GET /pages/new
  def new
    @category = Category.new
    @page = Page.new
  end

  # GET /pages/1/edit
  def edit

  end


  # POST /pages
  # POST /pages.json
  def create
    @page = Page.new(page_params)
    @category = @page.build_category(category_params)

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


  # PATCH/PUT /pages/1
  # PATCH/PUT /pages/1.json
  def update
    @page = Page.new(page_params)
    @category = @page.category

    @category.assign_attributes(category_params)
    respond_to do |format|
      if @page.update_attributes(page_params)
        format.html { redirect_to @page, notice: 'Page was successfully updated.' }
        format.json { render :show, status: :ok, location: @page }
      else
        format.html { render :edit }
        format.json { render json: @page.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /pages/1
  # DELETE /pages/1.json
  def destroy
    @page.destroy
    respond_to do |format|
      format.html { redirect_to pages_url, notice: 'Page was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_page
      @page = Page.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def page_params
      params.require(:page).permit(:title, :text, :image, :published, :seo_keywords, :seo_description)
    end

    def category_params
      params.require(:category).permit(:name)
    end
end
<%= form_for @page, html: { role: 'form' } do |f| %>
  <div id="error_explanation">
    <h2><%= pluralize(@page.errors.count, "error") %> prohibited this page from being saved </h2>
    <ul>
      <% @page.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  </div>
  <div class="form-group">
    <%= f.label :title %><br>
    <%= f.text_field :title, :class => 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :text %><br>
    <%= f.text_field :text, :class => 'form-control' %>
  </div>
  ...
  <div class="action">
    <%= f.submit 'Save', :class => 'btn btn-default' %>
  </div>
<% end %>