【问题标题】:Rails - Root Path seems to be ignored when render is involvedRails - 涉及渲染时似乎忽略了根路径
【发布时间】:2013-03-13 12:12:54
【问题描述】:

我有一个控制器#new,用作根路径。

Routes.rb

  resources :participants

  root :to => 'participants#new'

Rake 路线

    participants GET    /participants(.:format)          participants#index
                 POST   /participants(.:format)          participants#create
 new_participant GET    /participants/new(.:format)      participants#new
edit_participant GET    /participants/:id/edit(.:format) participants#edit
     participant GET    /participants/:id(.:format)      participants#show
                 PUT    /participants/:id(.:format)      participants#update
                 DELETE /participants/:id(.:format)      participants#destroy
            root        /                                participants#new

这在访问 xxx.xx/ 时效果很好

但是当我在控制器中渲染 #new 时,我被重定向到 /participants ,我该如何阻止这种情况发生?

  def create

    @participant = Participant.new(params[:participant])

    respond_to do |format|
      if @participant.save
        format.html { redirect_to root_path, notice: "<h2>Tack!</h2> <p>Registrering genomförd, vi har skickat ut ett mail med instruktioner till #{@participant.email}</p>".html_safe }
        format.json { render json: @participant, status: :created, location: @participant }
      else
        format.html { render action: "new" }
        format.json { render json: @participant.errors, status: :unprocessable_entity }
      end
    end
  end

日志:

Started POST "/participants" for 127.0.0.1 at 2013-03-13 13:21:29 +0100
Processing by ParticipantsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"dXmuTX/ugwgNjc21PPdiSHDGlNXEEGZCRHVIWKELOuw=", "participant"=>{"company"=>"asd", "f_name"=>"asd", "l_name"=>"asd", "email"=>"asd@asd.com", "phone_number"=>"asd", "allergy"=>"asd"}, "commit"=>"Anmäl mig!"}
  MOPED: 127.0.0.1:27017 COMMAND      database=damn_development command={:count=>"models", :query=>{"company"=>"asd", "_type"=>{"$in"=>["Participant"]}}} (0.7780ms)
  MOPED: 127.0.0.1:27017 QUERY        database=damn_development collection=models selector={"email"=>"asd@asd.com", "_type"=>{"$in"=>["Participant"]}} flags=[] limit=1 skip=0 batch_size=nil fields={:_id=>1} (0.5569ms)
  Rendered participants/_form.html.erb (4.7ms)
  Rendered participants/new.html.erb within layouts/application (5.5ms)
Completed 200 OK in 25ms (Views: 19.8ms)

【问题讨论】:

  • 你试过render 'new'吗?
  • 显示您在渲染新日志时创建的日志。
  • @Zippie - 是的。没有帮助。
  • @SaurabhJain - 用日志更新了我的 q!谢谢!
  • 它正在由您的日志呈现新的。见Rendered participants/new.html.erb

标签: ruby-on-rails ruby redirect routes document-root


【解决方案1】:

您被发送到/participants 的原因是因为那是创建操作的路径。除非您更改路线和表格,否则您无能为力。在您的路线中,您可以将创建操作与“/”匹配,但只能通过 post。然后在您的表单中,使用“/”作为操作。

【讨论】:

  • 谢谢,从您的回答中我了解到,我不能使用渲染来渲染 / 而不是 /participants,因为这只能通过帖子进行?
  • 在请求完成后使用渲染,因此您无法在不重定向的情况下更改 url。
【解决方案2】:

当您执行rake routes 时,请参阅root / participants#new 行。因此,您的路线会将您带到participants#new

routes.rb,当你使用root :to =&gt; 'participants#new'然后redirect_to root_path,它会转到participants#new,而无论你使用/,它都相当于root_url

例如:

当你在本地写下面的url时:

http://my_host_name/

实际上是——

http://my_host_name/participants/new

因此,在您的创建操作中,您有这一行:

format.html { redirect_to root_path, notice: "...."}

它正在重定向到 -

http://my_host_name/participants/new

【讨论】:

  • 所以没有办法解决这个问题?我无法让渲染器渲染 / 而不是 /participants?
  • 在这种情况下答案是否定的,因为render / 只不过是render root_url。所以无论你在root :toroutes.rb 中输入什么,这将是你的渲染路径。这里是/participants,所以你得到的只是渲染。
  • 这是不正确的。 render 仅用于显示要发送给浏览器的模板、文件或响应。它与网址无关。
  • @jvnill 啊,我知道你说的,但我不这么认为。这是正确的。对困惑感到抱歉。实际上,我的意思是在 routes.rb 的上下文中说,当你使用root :to =&gt; 'participants#new' 然后redirect_to root_path 时,它会转到participants#new,而无论你使用/,它都相当于root_url
猜你喜欢
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 2021-10-28
  • 2016-09-30
  • 2016-03-16
  • 2014-02-03
相关资源
最近更新 更多