Anss
Anss

Reputation: 674

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)

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):

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, i.e. 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.

Upvotes: 3

Views: 2430

Answers (1)

branch14
branch14

Reputation: 1262

Any query parameter you pass into a Rails Url Helper, like so:

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.

If the parameter is already part of the url that Rails generates for you, its most likely :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.

Does this answer your question?

Upvotes: 5

Related Questions