简体   繁体   English

使用link_to将数据从视图传递到另一个控制器 - Ruby on Rails

[英]Passing data from view to another controller using link_to - Ruby on Rails

I am trying to send this user id from my view to a controller, now thing is that the value is passed correctly to the controller(manage_users_controller) corresponding to this view, but it is not being sent to the other controller(server_management_controller) 我试图将此用户ID从我的视图发送到控制器,现在的问题是该值正确传递给与此视图对应的控制器(manage_users_controller),但它没有被发送到其他控制器(server_management_controller)

u.column :name => "Action" do |user|
  #Works fine
  link_to('Edit', edit_manage_user_path(user.id)) + " | " + 
    #This call does not send the value
    link_to('Assign Servers', edit_server_management_path(user.id)) 
end

Controller Action(server_management_controller): 控制器动作(server_management_controller):

def show
  if @uid == 1 #The value being sent from the view
    @servers_grid = initialize_grid(Server)
    @servers = Server.all
    @name = current_user[:username]
    @email = current_user[:email]
    render 'index'
  end
end

def edit
  @uid = params[:id]
  show
end

Another worth mentioning point is that the value IS appended into the URL when the Assign Server link is clicked, ie xyz.com/server_management/1/edit 另一个值得一提的是,单击Assign Server链接时,值IS会附加到URL中,即xyz.com/server_management/1/edit

Any help will be appreciated. 任何帮助将不胜感激。

---Solved--- - -解决了 - -

One Tip 一个提示

The parameter comes as a string so make sure you don't treat it as an integer right away. 参数以字符串形式出现,因此请确保您不要立即将其视为整数。

Any query parameter you pass into a Rails Url Helper, like so: 您传递给Rails Url Helper的任何查询参数,如下所示:

edit_manage_user_path(anyname: user.id)

can be accessed from within the called action in the following way 可以通过以下方式从被调用的操作中访问

params[:anyname]

anyname of course just being an example. anyname当然只是一个例子。

If the parameter is already part of the url that Rails generates for you, its most likely :id . 如果参数已经是Rails为您生成的URL的一部分,则最有可能:id You can look this up by executing rake routes , which will give you a full list of all routes that are configured within your application. 您可以通过执行rake routes来查找,这将为您提供应用程序中配置的所有路由的完整列表。

Does this answer your question? 这回答了你的问题了吗?

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

相关问题 使用Ruby on Rails和link_to帮助器时将参数传递给控制器 - Passing parameters to controller when using Ruby on Rails and link_to helper Link_to另一个视图在轨道上的红宝石 - Link_to another view ruby on rails 使用Ruby on Rails link_to链接到控制器操作 - Using Ruby on Rails link_to to link to controller action 使用 link_to 助手从子视图链接到父视图 - Ruby on Rails - Using link_to helper to link to a parent view from child view - Ruby on Rails Ruby on Rails:如何使用link_to将参数从视图传递到控制器,而不会在URL中显示参数 - Ruby on Rails: How to pass parameters from view to controller with link_to without parameters showing up in URL 使用link_to将变量传递给控制器​​并在另一页上使用它? - Passing a variable to the controller using link_to and use it on another page? Rails-link_to-删除操作-如何使用link_to内部的另一个控制器操作重定向到另一个页面 - Rails - link_to - delete action - how to redirect to another page using another controller action inside a link_to Ruby on Rails-通过if条件在link_to中传递参数? - Ruby on Rails - passing parameters in link_to with if conditions? Ruby on Rails - 将值传递给link_to - Ruby on Rails - Passing values to a link_to Ruby On Rails:访问控制器中的link_to参数 - Ruby On Rails: access link_to parameter in controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM