Has_many嵌套表单不会更新db rails

时间:2015-05-09 22:24:12

标签: ruby-on-rails

我正在建立一个博客类型的平台,用户可以在其中创建一个帐户,该用户有很多帖子,这些帖子应显示在用户/节目页面上。我已经设置了帖子和用户表,并添加到belongs_to&事后有很多关系。当我提交表单时,它会警告"用户帐户已更新"但帖子没有显示在帖子表中。我想我必须错过一些关键的东西。

Schema.rb

  create_table "posts", force: true do |t|
    t.text     "title"
    t.text     "body"
    t.string   "category1"
    t.string   "category2"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image"
    t.integer  "user_id"
    t.integer  "category_id"
  end

  add_index "posts", ["category_id"], name: "index_posts_on_category_id", using: :btree
  add_index "posts", ["user_id"], name: "index_posts_on_user_id", using: :btree

  create_table "users", force: true do |t|
    t.string   "email"
    t.string   "password_digest"
    t.boolean  "admin"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

模型

class User < ActiveRecord::Base
  has_many :posts
  accepts_nested_attributes_for :posts
end

class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
end

控制器

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)

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

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:email, :password, :password_confirmation)
    end
end

文章/新

<%= form_for current_user do |ff| %>
  <%= ff.fields_for :post do |f| %>
    <%= f.label :title %>
    <%= f.text_field :title, class: "title", placeholder: "Post Title..." %>

    <%= f.label :body %>
    <%= f.text_area :body, id: "edit" %>

    <%= f.submit %>
  <% end %>
<% end %>

2 个答案:

答案 0 :(得分:1)

我通过删除fields_for块并简化:

来解决这个问题
# posts/new

<%= form_for @post do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title, class: "title", placeholder: "Post Title..." %>

  <%= f.label :body %>
  <%= f.text_area :body, id: "edit" %>

  <%= f.submit %>
<% end %>

并将此行添加到posts控制器中的create方法:

@post = Post.new post_params.merge(user_id: current_user.id)

current_user是先前定义的辅助方法

答案 1 :(得分:0)

您只允许存储用户参数,因为嵌套属性也允许使用post参数。用以下代码替换user_params方法:

def user_params
  params.require(:user).permit(:email, :password, :password_confirmation, post_attributes: [:title, :body])
end