数据未保存在数据库rails中

时间:2016-03-16 19:59:30

标签: ruby-on-rails database forms

我刚开始学习rails,所以请宽容。 我有一个创建建议的表单。建议具有名称,内容并属于类别。 当我尝试创建一个新的建议时," name"和"内容"数据保存在数据库中,但不保存在category_id中。我不知道做错了什么。

请注意,我已经运行了rails g scaffold Advice,包括" name"和"内容"然后我在表单中添加了collection_select来选择类别。也许问题出在这里。

查看:

<%= form_for(@advice) do |f| %>
  <% if @advice.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@advice.errors.count, "error") %> prohibited this advice from being saved:</h2>

      <ul>
      <% @advice.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>
   <div class="field">
    <%= f.label :category_id %><br>
    <%= f.collection_select :category_id, Category.all, :id, :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>

<% end %>

控制器:

class AdvicesController < ApplicationController
  before_action :set_advice, only: [:show, :edit, :update, :destroy]

  # GET /advices
  # GET /advices.json
  def index
    @advices = Advice.all
    @categories = Category.all
  end

  # GET /advices/1
  # GET /advices/1.json
  def show
  end

  # GET /advices/new
  def new
    @categories = Category.all
    @advice = Advice.new
  end

  # GET /advices/1/edit
  def edit
    @categories = Category.all
  end

  # POST /advices
  # POST /advices.json
  def create
    @categories = Category.all
    @advice = Advice.new(advice_params)

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

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

  # DELETE /advices/1
  # DELETE /advices/1.json
  def destroy
    @advice.destroy
    respond_to do |format|
      format.html { redirect_to advices_url, notice: 'Advice was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def advice_params
      params.require(:advice).permit(:name, :content)
    end
end

架构:

ActiveRecord::Schema.define(version: 20160316111225) do

  create_table "advices", force: :cascade do |t|
    t.string   "name"
    t.text     "content"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "category_id"
  end

  create_table "categories", force: :cascade do |t|
    t.string   "name",       null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

路线:

Rails.application.routes.draw do
  resources :categories, only: [:index, :show]
  resources :advices
  root 'advices#index'
end

2 个答案:

答案 0 :(得分:0)

我喜欢在这种情况下使用options_for_select:

<%= f.select :category_id, options_for_select(Category.choices) %>

在您的类别模型中:

def self.choices
  cat_arr = []
  Category.find_each do |cat|
     cat_arr << [cat.name, cat.id]
  end
  cat_arr
end

传递一个二维数组将显示要从中选择的用户名,但会保存你想要的整数id。

答案 1 :(得分:0)

看起来您的问题出在控制器中。您不允许在强参数中使用category_id

def advice_params
  params.require(:advice).permit(:name, :content)
end

应该是:

def advice_params
  params.require(:advice).permit(:name, :content, :category_id)
end