如何使用redirect_to传递变量?
在我的控制器销毁function中,我希望在项目删除后重定向到索引,并且我想在重定向时传递一个名为’checked’的变量:
def destroy @Car = Car.find(params[:id]) checked = params[:checked] if @car.delete != nil end redirect_to cars_path #I would like to pass "checked" with cars_path URL (call index) end
如何通过cars_path传递这个’checked’变量,以便在我的索引函数中我能得到它? ( cars_path调用索引函数)
def index checked = params[checked] end
如果您不介意在url中显示参数,您可以:
redirect_to cars_path(:checked => params[:checked])
如果你真的介意,你可以传递会话变量:
def destroy session[:tmp_checked] = params[:checked] redirect_to cars_path end def index checked = session[:tmp_checked] session[:tmp_checked] = nil # THIS IS IMPORTANT. Without this, you still get the last checked value when the user come to the index action directly. end
- Rails_admin:我应该有admin_user或具有admin角色的用户来管理用户和管理面板
- 如何在Rails 3中指定使用迁移添加的字段顺序?
- 如何重命名Ruby库模块的命名空间?
- 无法自动加载常量JWTBlacklist,预期/home/sourabh/dev/celebration/app/models/jwt_blacklist.rb来定义它(LoadError)
- rails在集成测试中设计经过身份validation的路由
- Rails通过多个标记充当filter上的标记
- Rails“无桌面”模型
- Paperclip:url不会创建路由
- 创建与.build的关联并解决参数validation问题