gpt4 book ai didi

ruby-on-rails - Ruby - redirect_to 重定向到正确的页面,但不更改 URL

转载 作者:数据小太阳 更新时间:2023-10-29 08:07:26 25 4
gpt4 key购买 nike

我正在自学 Ruby 和 Rails...直到今天,我对它感觉还不错。 :)

然而,今天我遇到了一个似乎无法解决的问题。我正在通过创建博客来练习。我设置了路由和 View - 代码将写入数据库 - 但 redirect_to 似乎无法正常工作。

如果用户导航到:.../article/new,他们可以输入一篇新文章。成功提交后,应用程序应重定向到 URL ../article/id,在 show.html.erb 上显示文章并闪现文章保存成功的消息。

出于某种原因,我无法使 URL 重定向部分工作。文章保存,show.html.erb 显示正确的消息...但 url 保持../article/new。如果重定向不起作用,应用程序的其余部分将不起作用,因为其他操作需要不同的 URL(例如,../article/id/edit)。

我一直在用

<%= @article.inspect %>
on the show page and it looks like everything has saved correctly (which I suspected because it calls the correct view).

Any thoughts would be very, very welcome. I'm really excited to be learning Ruby and Rails and look forward to getting through this.

articles_controller.rb:

class ArticlesController < ApplicationController
def new
@article = Article.new
end

def create
@article = Article.new(article_params)
if @article.save
flash[:notice] = "Article was successfully created"
redirect_to article_path(@article)
else
render 'new'
end
end

def show
@article = Article.find(params[:id])
end

private
def article_params
params.require(:article).permit(:title, :description)
end
end

rake 路:

Prefix Verb   URI Pattern                  Controller#Action
root GET / pages#home
about GET /about(.:format) pages#about
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy

最佳答案

您是否尝试过将 article_path 更改为 article_url?这可能会触发您正在寻找的行为。

关于ruby-on-rails - Ruby - redirect_to 重定向到正确的页面,但不更改 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36091645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com