我正在编写本教程的条目提交控制器:http://www.communityguides.eu/articles/1
当我尝试提交条目时,我得到Users can't be blank
...我不想将user_id作为隐藏字段传递,对吧?那么我应该如何更改它以自动获取用户的ID?
我正在使用设计进行身份验证。 :)而且我是一个完整的rails新手。 这是我的控制者:
def submit
@entry = current_user.articles.find(params[:id])
# submit only, if article is currently in draft or rejected-state
if (@entry.state == 0) or (@article.state == 2)
@entry.state = 1
@entry.submitted = Time.now
if @entry.save
flash[:notice] = 'Your article was successfully submitted for approval.'
else
flash[:error] = 'There was an error while submitting your article.'
end
else
flash[:error] = 'This article can not be submitted.'
end
respond_to do |format|
format.html { redirect_to(:action => 'myarticles') }
format.xml { head :ok }
end
end
# GET /entries/1/edit
def edit
@entry = Entry.find(params[:id])
end
# POST /entries
# POST /entries.xml
def create
@entry = Entry.new(params[:entry])
respond_to do |format|
if @entry.save
format.html { redirect_to(@entry, :notice => 'Entry was successfully created.') }
format.xml { render :xml => @entry, :status => :created, :location => @entry }
else
format.html { render :action => "new" }
format.xml { render :xml => @entry.errors, :status => :unprocessable_entity }
end
end
end
# PUT /entries/1
# PUT /entries/1.xml
def update
@entry = current_user.entries.find(params[:id])
#if the entry has been approved, the user cannot change the title or URL.
if @entry.state > 2
params[:entry].delete(:title)
params[:entry].delete(:url)
end
respond_to do |format|
if @entry.update_attributes(params[:entry])
format.html { redirect_to(@entry, :notice => 'Entry was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @entry.errors, :status => :unprocessable_entity }
end
end
end
答案 0 :(得分:0)
在您的描述中,您似乎在尝试创建新条目时会遇到此错误。
def create
@entry = current_user.entries.new(params[:entry]) # <-- Scope to the current user
respond_to do |format|
if @entry.save
format.html { redirect_to(@entry, :notice => 'Entry was successfully created.') }
format.xml { render :xml => @entry, :status => :created, :location => @entry }
else
format.html { render :action => "new" }
format.xml { render :xml => @entry.errors, :status => :unprocessable_entity }
end
end
end