Tag: 重定向

Rails中重定向的格式错误

我想在重定向时将: :user_id发送到我的索引控制器: format.html { redirect_to @employee_personal, notice: ‘Personal was successfully updated.’} 在我的索引控制器中,我必须发送: @user_id = @employee_personal.user_id 变量。 我怎么发这个? 在重定向中,我试过: format.html { redirect_to @employee_personal(:user_id => @user_id), notice: ‘Personal was successfully updated.’ } 这是错误的。 format.html { redirect_to :action => :index, :user_id => @user_id } 是正确的。 如何以该格式正确地发出错误?

如何使用RSpec测试Rails 4应用程序并从Railscast#53(修订版)中自定义exception处理?

使用此Railscast剧集 , ActiveRecord::RecordNotFound错误最终会重定向到ErrorsController和/app/views/errors/404.html.erb视图。 但是,我在我的一个测试中有以下内容(Item belongs_to List): scenario “item cannot be created without a list” do visit new_item_path # Because it’s not the proper nested new_list_item_path(@some_list) path, # @list is not created in the ItemsController. page.should have_content(‘Not Found’) page.status_code.should be 404 end 但是,测试失败并出现以下情况: Failure/Error: visit new_item_path ActiveRecord::RecordNotFound: Couldn’t find List without an ID 因此控制器在需要列表时正常工作,但测试失败,因为它没有考虑应该将用户发送到404的自定义exception处理。 如何正确测试重定向?

会话值在重定向rails 4后重置

我正在使用设计3.5.1,rails 4.0.0和ruby 2.0.0-p0。 如果用户未登录并尝试打开需要身份validation的私有页面,请使用lvh.me:3000/users/1/edit ,然后设计将针对关键user_return_to在会话中添加users/1/edit 。 问题是当用户在身份validation失败后被重定向到登录页面时,会话中的user_return_to值会重置。 我的应用程序遍布此问题。 如果我在会话中存储值并从同一操作重定向到另一个路径,则会话值将消失。 注意:我已将rails应用程序从(rails 3.0.4,ruby 1.8.7)升级到(rails 4.0.0,ruby 2.0.0)。 对不起,问题中有任何含糊之处。 如果您有任何疑问,请发表评论。 更新: 在我的应用程序控制器中,我添加了一个beforefilter来validation用户身份。 应用程序控制器中仅显示相关代码。 application_controller.rb class ApplicationController :devise_controller? # rest of the code end 我正在使用inheritanceDevise::FailureApp的自定义失败类。 代码看起来像这样。 custom_failure.rb class CustomFailure < Devise::FailureApp def redirect_url eval "new_user_session_path" end def redirect store_location! flash[:alert] = i18n_message # session[:user_return_to] returns '/foobar' here. redirect_to redirect_url end […]

控制器和动作中定义的路径被忽略,Ruby on Rails

根据http://guides.rubyonrails.org/layouts_and_rendering.html 我应该能够在我的micropostscontroller中的创建操作中定义来自不同控制器的路径: def create @micropost = current_user.microposts.build(params[:micropost]) if @micropost.save flash[:success] = “Micropost created!” redirect_to profile_path else render ‘static_pages/profile’ end end 但是,当我创建post失败时(将其留空或使其太长),将呈现页面’/ microposts’,即控制器不存在的主页。 当我成功创建微博时,我被重定向到配置文件路径’/ profile’,当我将render ‘static_pages/profile’更改为redirect_to profile_path ,重定向工作。 为什么浏览器会忽略渲染请求并进入微博控制器主页? 此外,渲染的微博页面提供NoMethodError: NoMethodError in Microposts#create undefined method `name’ for nil:NilClass app/views/static_pages/profile.html.erb:16:in `_app_views_static_pages_profile_html_erb___1610169404003779010_70327969935820′ app/controllers/microposts_controller.rb:10:in `create’ 当重定向到配置文件时,配置文件可以自行呈现,因为在static_pages控制器中的配置文件操作中定义了@user。 @user = User.find_by_remember_token(cookies[:remember_token])

Rails重定向301编码

我重新创建了一个网站,有很多301处理(从phpurl到Railsurl)。 它完美地适用于: match “/produits/les-dallages/dallage-riva.html”, :to => redirect(“/produits/dallages/dalle-riva”) 我的问题是这种旧url(从谷歌网站管理员工具提供): “/produits/les-pavages/paves-carres/item/48-pav%C3%A9s-carr%C3%A9s.html” 编码未被理解,因为url被浏览器转换而Rails不理解带有“é”而不是“%C3%A9”的url… 如何管理这种url? 第二个问题:我可以在routes.rb文件中添加多少条路由(301)? 谢谢

Ruby on Rails Ajax调用运行控制器代码,返回成功,但不渲染视图

我的页面上有一个工作搜索机制; 带有文本字段和“搜索”标记的提交按钮的表单。 我的页面上还有一个基于raphael.js的动画项目,上面有可点击的项目,我想要执行搜索。 点击后,我发送给我的控制器params数据,这些数据是从raphael对象中获取的搜索项。 正在传递数据,并且控制器代码运行,但相应的视图无法呈现,因此单击对用户显示为什么都不做。 所以点击后,我就像这样做一个Ajax调用: 文件searchthing.js // Add a search function circle.click(function (e) { var csrch = this.data(‘label’); // alert(‘clicked ‘ + csrch); e.preventDefault(); $.ajax({ type: “GET”, url: “/bubbleBar”, data: $.param({ srchterm: csrch} ), success: function(){ // window.location.replace(“/pictures”); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert(“— error: ” + errorThrown); } }); }); 我的控制器中的’bubbleBar’操作成功触发,传输搜索条件,并重定向到处理搜索的工作索引操作: 文件myController.rb: […]

Respond_to并重定向Rails中的相同操作

在Rails控制器的Update操作中,通常会有如下代码: def update @book = Book.find(params[:id]) if @book.update_attributes(params[:book]) redirect_to(@book) else render :edit end end 在else的情况下,这将呈现编辑模板。 但是,如果我想使用respond_to,与编辑操作中的方式完全相同,如下所示: def update @book = Book.find(params[:id]) if @book.update_attributes(params[:book]) redirect_to(@book) else respond_to do |format| format.html # edit.html.erb format.json { render :json => @team } end end end 因此,如果更新失败,请确保根据请求的格式返回json或html。 这有道理吗? 如果是这样,你将如何避免错误:“在此操作中多次调用渲染和/或重定向”

登录原始任务后重定向

所以我想向用户发送电子邮件,其中包含一些链接,您可以单击这些链接来执行操作,但如果用户尚未登录,我希望能够让用户登录,然后将其重定向回他试图去做什么。 我在application_controller.rb中有这些方法 这被称为URL链接到的地方的before_filter,捕获用户不应该在他试图去的任何地方并存储意图 def user_in_beta unless user_signed_in? && current_user.beta if user_signed_in? redirect_to :home_betawait else session[:original_uri] = request.request_uri redirect_to new_user_session_path end end end 并且在他登入并将他放回他所属的地方之后,这应该抓住他。 def after_sign_in_path_for(resource_or_scope) if current_user.beta if session[:original_uri].nil? :home_index else session[:original_uri] end else :home_betawait end end 但事实并非如此。 相反,我得到一个非常讨厌的“不能重定向到零!” 如果我将它读入一个局部变量并存储它,那么更多的事情就会破裂。 我感到非常困惑,这是一件非常普遍的事情。

为什么Rails重定向我的POST而不是GET?

我正在尝试将数据发布到某个controller#action对,但我的应用程序重定向我的POST(但不是GET),我无法弄清楚原因。 我用一种方法构建了一个裸骨控制器: class NavigationsController ‘in foo’ end end 我的路由文件只有一个规则: map.connect ‘:controller/:action/:id’ 这是我获取和发布时的结果: $ curl http://localhost:3000/navigations/foo/1 in foo $ curl -d ‘a=b’ http://localhost:3000/navigations/foo/1 You are being redirected. 规格:rails 2.3.8,ruby 1.8.7

如何获得text_field rails的价值

我有一个文本字段: ‘input-xxlarge search-query’,:id=>’keyword’ %> 然后单击链接我想将此文本字段中的值传递给控制器​​方法。 我不想使用表单和表单提交。 我的链接为: GO 并且对于这个’搜索’方法我想传递文本字段值….也可以直接转到设置为此操作“搜索”的页面“/ home / search” 我该怎么做呢??? 谢谢…