0
我有一个用户模型,它有一个程序。我想根据当前用户添加一个程序。钢轨协会新方法belongs_to
我的代码有什么问题?
def new
@program = Program.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @program }
end
end
# POST /programs
# POST /programs.json
def create
@program = current_user.build_program(params[:program])
if @program.save
if request.xhr?
#do nothing
else
redirect_to @program, notice: 'User was successfully created.'
end
else
if request.xhr?
render :status => 403
else
render action: "new"
end
end
end
编辑
Additionaly,当POST请求被发送我看到控制台POST请求为:
Started POST "/programs" for 127.0.0.1 at 2013-06-12 17:42:46 +0300
Processing by ProgramController#index as */*
Parameters: {"utf8"=>"✓", "authenticity_token"=>"m21h2SRHJ1A9TlKVIdwZOwodKx+vPEx16dd5z936LmY=", "program"=>{"title"=>"baslik", "details"=>"deneme"}}
但这组不会被添加到数据库中。
会发生什么,如果我使用@program = Program.new(PARAMS [:程序] .merge(: user_id => current_user.id))在创建? – Yagiz
与使用'current_user.build_program(params [:program])'时相同。 – Mischa
谢谢。但将其添加到数据库仍然存在问题。我编辑了这个问题。 – Yagiz