当用户向我的Rails应用程序中的url / mobile请求时,我希望将一个参数自动附加到在请求后加载的URL上(例如/mobile?tree_width=5
)
我已经尝试了一些方法,但是都没有用。
我得到的最接近的是我的控制器中的代码:
def mobile
respond_to do |format|
format.html{
# pass tree width here
render mobile_project_steps_path(@project, :tree_width => @project.tree_width)
}
end
end
我收到错误
Missing template /projects/8/steps/mobile?tree_width=5
但是我认为这条路应该根据我的耙路而存在:
mobile_project_steps GET /projects/:project_id/steps/mobile(.:format) steps#mobile
如何从控制器向URL添加参数?
您需要检查参数是否丢失以及是否将其重定向到带有额外参数的当前操作。我会用before_action来压缩它:
before_action :check_tree_width, only: :mobile
def mobile
# Your actual logic
end
private
def check_tree_width
redirect_to(tree_width: @project.tree_width) unless params[:tree_width].present?
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句