[英]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.