用户只有在登录后才能创建指南。
当我点击“新指南”链接时,这就是Heroku的日志输出的内容:
2013-12-30T20:28:37.826032+00:00 app[web.1]: ActiveRecord::UnknownAttributeError (unknown attribute: user_id):
GuidesController:
class GuidesController < ApplicationController
before_action :set_guide, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /guides
# GET /guides.json
def index
if params[:tag]
@guides = Guide.tagged_with(params[:tag])
else
@guides = Guide.all
end
end
# GET /guides/1
# GET /guides/1.json
def show
end
# GET /guides/new
def new
@guide = current_user.guides.build(guide_params)
end
# GET /guides/1/edit
def edit
end
# POST /guides
# POST /guides.json
def create
@guide = current_user.guides.build(guide_params)
respond_to do |format|
if @guide.save
format.html { redirect_to @guide, notice: 'Guide was successfully created.' }
format.json { render action: 'show', status: :created, location: @guide }
else
format.html { render action: 'new' }
format.json { render json: @guide.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /guides/1
# PATCH/PUT /guides/1.json
def update
respond_to do |format|
if @guide.update(guide_params)
format.html { redirect_to @guide, notice: 'Guide was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @guide.errors, status: :unprocessable_entity }
end
end
end
# DELETE /guides/1
# DELETE /guides/1.json
def destroy
@guide.destroy
respond_to do |format|
format.html { redirect_to guides_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_guide
@guide = Guide.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def guide_params
params.require(:guide).permit(:title, :author, :description, :link, :tag_list) if params[:guide]
end
end
答案 0 :(得分:0)
您的new
行动
def new
@guide = current_user.guides.build(guide_params)
end
为什么呢?新操作应该只是将表单返回到浏览器以创建新指南。您可以在create
操作中重复此操作。
此外,index
还有:
def index
if params[:tag]
@guides = Guide.tagged_with(params[:tag])
else
@guides = Guide.all
end
end
您应该使用guide_params[:tag]
,因为浏览器会返回:tag
。
编辑我发现您在白名单中使用了[:tag_list]
。我假设你把它交给其他地方?您是否测试过定义标记的index
操作的能力?我认为你唯一想要使用裸params[:xxxx]
的地方是私人方法。