2015-12-10 33 views
0

我在几个关联中使用注释模型到其他模型。关系总是相同的:评论“属于”另一个模型的项目,而其他模型“有很多”评论。如何检查表单在Rails上提交的页面?

我呈现在其它型号的显示页面的评论形式,像这样:

<%= render "comments/form" %> 

当用户点击提交按钮,我想他被重定向到不同的页面,这取决于页他在之前。当我寻找一个解决方案,我发现通过APIdock(参见http://apidock.com/rails/v4.2.1/ActionView/Helpers/UrlHelper/current_page%3F)的current_page?助手的ActionView并认为这是使用它的评论控制器这样一个好主意:

respond_to do |format| 
    if (@comment.save) && (current_page?(controller: 'game', action: 'show')) 
    format.html { redirect_to game_comments_url, notice: 'Comment was successfully created.' } 
    format.json { render :show, status: :created, location: @comment } 
    elsif (@comment.save) && (current_page?(controller: 'comment')) 
    format.html { redirect_to @comment, notice: 'Comment was successfully created.' } 
    format.json { render :show, status: :created, location: @comment } 
    else 
    format.html { render :new } 
    format.json { render json: @comment.errors, status: :unprocessable_entity } 
    end 
end 

但可悲的是,我得到以下错误消息:

undefined method `current_page?' for #<CommentsController:0x007f67fe42e1a0> 

我将不胜感激,如果有人能送给我我如何能找到一个解决方案,在该页面已提交一个特定的形式,或至少在如何将用户重定向到不同的页面,然后方式。

回答

1

current_page?是一个视图助手,所以它在控制器中不可用。

您可以通过控制器中的request.referrer获取引用网址

+0

谢谢您的提示。我该如何调试该方法向我显示的结果?我改变了我的代码:'if(@ comment.save)&&(URI(request.referer).path =='/ games')'但是当我试用它时,它使用“else”分支,而不是因为结果不合适。 –

+0

您可以尝试在控制器中添加'puts URI(request.referer).path',然后查看终端中的确切路径 – richfisher

相关问题