I am new using ruby on rails, and what I want to do is to use the information from another form generated with scaffold, in the requested tab, I want to call the field of tpedido
and the information appears in the form, but at the moment to save it, I get the error of the title.
My requested model:
class Pedido < ApplicationRecord
has_many :solicitud
belongs_to :tpedido
end
controlador
class PedidosController < ApplicationController
before_action :set_pedido, only: [:show, :edit, :update, :destroy]
# GET /pedidos
# GET /pedidos.json
def index
@pedidos = Pedido.all
end
# GET /pedidos/1
# GET /pedidos/1.json
def show
end
# GET /pedidos/new
def new
@pedido = Pedido.new
end
# GET /pedidos/1/edit
def edit
end
# POST /pedidos
# POST /pedidos.json
def create
@pedido = Pedido.new(pedido_params)
respond_to do |format|
if @pedido.save
format.html { redirect_to @pedido, notice: 'Pedido was successfully created.' }
format.json { render :show, status: :created, location: @pedido }
else
format.html { render :new }
format.json { render json: @pedido.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /pedidos/1
# PATCH/PUT /pedidos/1.json
def update
respond_to do |format|
if @pedido.update(pedido_params)
format.html { redirect_to @pedido, notice: 'Pedido was successfully updated.' }
format.json { render :show, status: :ok, location: @pedido }
else
format.html { render :edit }
format.json { render json: @pedido.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pedidos/1
# DELETE /pedidos/1.json
def destroy
@pedido.destroy
respond_to do |format|
format.html { redirect_to pedidos_url, notice: 'Pedido was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pedido
@pedido = Pedido.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def pedido_params
params.require(:pedido).permit(:Nombre, :Direccion, :telefono, :campana, :placa, :zona, :ncaja, :tipopedido)
end
end
modelo tpedido
class Tpedido < ApplicationRecord
has_many :pedido
accepts_nested_attributes_for :pedido
end
controlador Tpedido
class TpedidosController < ApplicationController
before_action :set_tpedido, only: [:show, :edit, :update, :destroy]
# GET /tpedidos
# GET /tpedidos.json
def index
@tpedidos = Tpedido.all
end
# GET /tpedidos/1
# GET /tpedidos/1.json
def show
end
# GET /tpedidos/new
def new
@tpedido = Tpedido.new
end
# GET /tpedidos/1/edit
def edit
end
# POST /tpedidos
# POST /tpedidos.json
def create
@tpedido = Tpedido.new(tpedido_params)
respond_to do |format|
if @tpedido.save
format.html { redirect_to @tpedido, notice: 'Tpedido was successfully created.' }
format.json { render :show, status: :created, location: @tpedido }
else
format.html { render :new }
format.json { render json: @tpedido.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tpedidos/1
# PATCH/PUT /tpedidos/1.json
def update
respond_to do |format|
if @tpedido.update(tpedido_params)
format.html { redirect_to @tpedido, notice: 'Tpedido was successfully updated.' }
format.json { render :show, status: :ok, location: @tpedido }
else
format.html { render :edit }
format.json { render json: @tpedido.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tpedidos/1
# DELETE /tpedidos/1.json
def destroy
@tpedido.destroy
respond_to do |format|
format.html { redirect_to tpedidos_url, notice: 'Tpedido was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tpedido
@tpedido = Tpedido.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def tpedido_params
params.require(:tpedido).permit(:tipopedido)
end
end
form donde me muestra el select
<div class="field">
<%= form.label :tipo_de_pedido %>
<%= form.collection_select :tpedido_tipopedido, Tpedido.all, :id, :tipopedido %>
</div>
schema.db
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20181228010838) do
create_table "pedidos", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "Nombre"
t.string "Direccion"
t.string "telefono"
t.string "campana"
t.string "placa"
t.string "zona"
t.string "ncaja"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "tpedido_id"
t.index ["tpedido_id"], name: "index_pedidos_on_tpedido_id"
end
create_table "solicituds", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.date "fecha"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id"
t.bigint "pedido_id"
t.index ["pedido_id"], name: "index_solicituds_on_pedido_id"
t.index ["user_id"], name: "index_solicituds_on_user_id"
end
create_table "tpedidos", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "tipopedido"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "nombre", default: "", null: false
t.string "apellido", default: "", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "superadmin_role", default: false
t.boolean "admin_role", default: false
t.boolean "user_role", default: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
create_table "vehiculos", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "marca"
t.string "vehiculo"
t.string "placa"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id"
t.index ["user_id"], name: "index_vehiculos_on_user_id"
end
add_foreign_key "pedidos", "tpedidos"
add_foreign_key "solicituds", "pedidos"
add_foreign_key "solicituds", "users"
add_foreign_key "vehiculos", "users"
end
thanks in advance.