Ruby-on-rails 保存后重定向到索引而不是显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10439724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:20:48  来源:igfitidea点击:

redirect to index rather than show after save

ruby-on-railsruby-on-rails-3

提问by Finnnn

I want to redirect to the a models index view after saving my model.

我想在保存模型后重定向到模型索引视图。

def create
  @test = Test.new(params[:test])

  respond_to do |format|
    if @test.save
      format.html { redirect_to @test, notice: 'test was successfully created.' }
    else
      format.html { render action: "new" }
    end
  end
end

I've tried

我试过了

    format.html { render action: "index", notice: 'Test was successfully created.' }

but I get the following error in /app/views/tests/index.html.erb -

但我在 /app/views/tests/index.html.erb 中收到以下错误 -

   undefined method `each' for nil:NilClass

Any idea what's going wrong?

知道出了什么问题吗?

回答by Abid

render action: "index"

will not redirect, redirecting and rendering a different, render will just render the view with the current available variables for it. while with a redirect the index function of the controller will run and then the view will be rendered from there.

不会重定向,重定向和渲染不同的,render 只会使用当前可用的变量渲染视图。而通过重定向,控制器的索引功能将运行,然后视图将从那里呈现。

you are getting the error because your index view is expecting some array which you are not giving to it since you are just rendering "index" and you dont have the variables that the view needs.

您收到错误是因为您的索引视图需要一些您没有提供给它的数组,因为您只是在呈现“索引”并且您没有视图需要的变量。

You can do it in 2 ways

您可以通过 2 种方式进行

1- using render action: "index"

1-使用 render action: "index"

make available to the view all the variables that it needs before you render, for example it may be needing a @posts variable which it uses to show list of posts so you need to get the posts in your create action before you render

在渲染之前让视图可以使用它需要的所有变量,例如它可能需要一个 @posts 变量,它用于显示帖子列表,因此您需要在渲染之前在创建操作中获取帖子

@posts = Post.find(:all)

2- don't render do a redirect_to

2-不要渲染做一个 redirect_to

instead of rendering "index" you redirect to the index action which will take care of doing the necessary things that the index view needs

而不是呈现“索引”,您重定向到索引操作,它将负责执行索引视图所需的必要操作

redirect_to action: "index"

回答by okodo

The view "index" includes "@tests.each do"-loop. And method create does not provide the variable "@tests". so you have the error. You can try this:

视图“index”包括“@tests.each do”-loop。并且方法 create 不提供变量“@tests”。所以你有错误。你可以试试这个:

format.html { redirect_to action: "index", notice: 'Test was successfully created.' }

回答by Jyothu

Its very simple.

它非常简单。

Just rewrite your method like

只需重写您的方法,例如

def create
  @test = Test.new(params[:test])

  respond_to do |format|
    if @test.save
      format.html { **redirect_to tests_path**, notice: 'test was successfully created.' }
    else
      format.html { render action: "new" }
    end
  end
end

It will redirect to the index page

它将重定向到索引页面

回答by noloman

There are two ways of doing this:

有两种方法可以做到这一点:

  1. Using the resource from the rake routes:
  1. 使用以下资源rake routes

format.html { redirect_to todo_items_url, notice: 'Todo item was successfully created.' }

format.html { redirect_to todo_items_url, notice: 'Todo item was successfully created.' }

  1. Using the controller action:
  1. 使用控制器动作:

format.html { redirect_to action: :index, notice: 'Todo item was successfully created.' }

format.html { redirect_to action: :index, notice: 'Todo item was successfully created.' }