Я только начинаю изучать рельсы, поэтому, пожалуйста, будьте терпимы. У меня есть форма, которая создает советы. Совет имеет имя, содержание и относится к категории. Когда я пытаюсь создать новый совет, данные «имя» и «контент» сохраняются в базе данных, но не category_id. Я не знаю, чего не хватает для этого.данные не сохранены в рельсах базы данных
Обратите внимание, что я использовал rails g scaffold. Совет, включая «имя» и «контент», а затем я добавил коллекцию в виде, чтобы выбрать категорию. Может быть, проблема здесь.
Вид:
<%= 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
К сожалению я не видел ваш ответ там, я просто удалил мое, как ваш был здесь первым и был очень хорошо и тот же ответ. мои извинения. –