新手在这里...在轨道上学习ryby大约4个星期,第一个项目,找不到什么错误,会很感激一些帮助。
_form(第2行中的错误):
<%= simple_form_for @workshop, :html => { :multipart => true } do |f| %>
<%= select_tag(:category_id, options_for_select(@categories), :prompt => "Escolher Categoria") %>
<%= f.file_field :workshop_img %>
<%= f.input :title, label: "Nome do Workshop" %>
<%= f.input :description, label: "Descrição" %>
<%= f.input :author, label: "Formador" %>
<%= f.button :submit %>
workshops_controller:
class WorkshopsController < ApplicationController
before_action :find_workshop, only: [:show, :edit, :update, :destroy]
def index
if params[:category].blank?
@workshops = Workshop.all.order("created_at DESC")
else
@category_id = Category.find_by(name: params[:category]).id
@workshops = Workshop.where(:category_id => @category_id).order("created_at DESC")
end
end
def show
end
def new
@workshop = current_user.workshops.build
@categories = Category.all.map{ |c| [c.name, c.id] }
end
def create
@workshop = current_user.workshops.build(workshop_params)
@workshop.category_id = params[:category_id ]
if @workshop.save
redirect_to root_path
else
render 'new'
end
end
def edit
@categories = Category.all.map{ |c| [c.name, c.id] }
end
def update
@workshop.category_id = params[:category_id]
if @workshop.update(workshop_params)
redirect_to workshop_path(@workshop)
else
render 'edit'
end
end
def destroy
@workshop.destroy
redirect_to root_path
end
private
def workshop_params
params.require(:workshop).permit(:title, :description, :author, :category_id, :workshop_img)
end
def find_workshop
@workshop = Workshop.find(params[:id])
end
end
类别模型
has_many :workshops
end
用户模型
has_many :workshops
车间模型
belongs_to :user
belongs_to :category
has_attached_file :workshop_img, styles: { workshop_index: "250x350>", workshop_show: "325x475>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :workshop_img, content_type: /\Aimage\/.*\z/
end
schema.rb
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
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.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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 "workshops", force: :cascade do |t|
t.string "title"
t.string "author"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "category_id"
t.string "workshop_img_file_name"
t.string "workshop_img_content_type"
t.integer "workshop_img_file_size"
t.datetime "workshop_img_updated_at"
end
end
答案 0 :(得分:0)
我们可以获得有关正在生成的错误的一些信息吗? rails错误消息往往比其他语言更有用。如果你说表格的第2行有错误,我可以看到,如果你可以复制并粘贴错误的输出,那么它应该突出显示问题。
答案 1 :(得分:0)
我明白了!在您的create
操作中,您呈现了新的&#39;查看(如果保存失败),需要@categories
。但是你不能在创建动作中创建它们。有nil
来自。