简体   繁体   English

Rails将数据从视图传递到另一个控制器

[英]Rails passing data from a view to a different controller

I am trying to pass a int value from one view to a controller. 我试图将int值从一个视图传递给控制器​​。 The view and controller are not related. 视图和控制器无关。 So in one view I display a int value, I want to click on that int value and be redirected to another view which will display some content based on my int value from the previous view. 因此,在一个视图中,我显示一个int值,我想单击该int值,然后重定向到另一个视图,该视图将根据上一个视图中的int值显示一些内容。 Can anyone help me with the code please. 谁能帮我提供代码。

At this point the controller which will receive the int value, it receives that value only if I added manually. 此时,将接收int值的控制器,仅当我手动添加时,它才会接收该值。

Thank you 谢谢

One option would be to create a custom action in that controller. 一种选择是在该控制器中创建自定义动作。

class YourController

    def custom_action_for_different_view
      @your_integer = params[:id]
      render or redirect_to
    end

end

You would also need to add an entry in your routes.rb so rails nows how to handle the route. 您还需要在routes.rb中添加一个条目,以便Rails现在介绍如何处理该路由。

get "custom_url" => "your_controller#custom_action_for_different_view"

and of course use that url in your first view (which displays the integer which will be clicked). 并且当然要在您的第一个视图中使用该网址(该网址显示将被点击的整数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM