【发布时间】:2016-03-17 04:45:26
【问题描述】:
提交表单后如何渲染相同的视图?
我无法呈现自定义的部分错误消息。我想这是因为我需要渲染相同的视图,但我不知道该怎么做。我尝试了所有注释的行,因为我需要
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = @micropost.comments.create(comment_params)
@comment.user_id = current_user.id #or whatever is you session name
if @comment.save
flash[:success] = "Comment created!"
redirect_to :back
else
@new_comment = @micropost.comments.new
#flash[:danger] = "Max 140 caracteres - No puede estar en blanco!" #works but is static single comment.
#render 'microposts/show', locals: {micropost: @micropost} # I need to define all variables to in this controller again and pass to the partial (It's not DRY)
#redirect_to micropost_url(@micropost) # works but not show partial errors messages
end
end
路线:
resources :microposts do
member do
get :likes
end
[:create, :destroy, :show, :index]
resources :comments
end
【问题讨论】:
标签: validation ruby-on-rails-4 activerecord