在rails redirect_to中传递参数

我们如何在rails的redirect_to中传递参数?我知道我们可以使用以下方式传递ID:


redirect_to :action => action_name,:id => 3

如果我想传递其他参数(例如某些表单数据)怎么实现呢?


编辑:


对于Ruby 2语法,您必须将上面的代码段更新为:


redirect_to action: action_name, id: 3


汪汪一只猫
浏览 768回答 3
3回答

繁华开满天机

只需将它们附加到选项:redirect_to controller: 'thing', action: 'edit', id: 3, something: 'else'会屈服 /thing/3/edit?something=else

慕桂英3389331

如果您正在使用RESTful资源,则可以执行以下操作:redirect_to action_name_resource_path(resource_object, param_1: 'value_1', param_2: 'value_2')or#You can also use the object_id instead of the objectredirect_to action_name_resource_path(resource_object_id, param_1: 'value_1', param_2: 'value_2')or#if its a collection action like index, you can omit the id as followsredirect_to action_name_resource_path(param_1: 'value_1', param_2: 'value_2')#An example with nested resource is as follows:redirect_to edit_user_project_path(@user, @project, param_1: 'value_1', param_2: 'value_2')

胡子哥哥

您可以使用flash参数将任意对象传递到模板。&nbsp;redirect_to :back, flash: {new_solution_errors: solution.errors}然后通过哈希在模板中访问它们。<% flash[:new_solution_errors].each do |err| %>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Ruby