Brittany
Brittany

Reputation: 1439

Print @User data from Show (show.html.erb) action to otherpage.html.erb

On my page _user.html.erb, I'm generating user search results. The searched user's credentials (name, email, etc) are successfully populated. However, when my visitor clicks the "Go" button on their resulting username, it currently takes them to show.html.erb (full user profile).

<%= link_to image_tag("/assets/go.png", :alt => "Image Description", :action => "show"), user_path(user.id) %>

That said, I want to generate this same user data (firstname, lastname, etc) on a page OTHER than show.html.erb, e.g. otherpage.html.erb.

I thought this might work:

<%= link_to image_tag("/assets/go.png", :alt => "Image Description", :action => "otherpage"), user_path(user.id) %>

Though I assume it doesn't because it's not grabbing the resulting user ID... Does anyone know how I can do this? Help GREATLY appreciated - I'm under a time crunch!

Note: <%= render user %> successfully displays ALL users on otherpage.html.erb, but I want the selected user only.

users_controller.rb

def show
  @user = User.find(params[:id])
end

def otherpage

     @user = User.find(params[:id])
     @user = User.all
      if params[:search]
        @user = User.search(params[:search]).order("created_at DESC")
      else
        @user = User.all.order('created_at DESC')
      end
    end

_user.html.erb

   <div class="name">
          <%= link_to user.firstname, user %> <%= user.lastname %> 
    <%= link_to image_tag("/assets/go.png", :alt => "Image Description", :action => "show"), user_path(user.id) %>

    </div>

otherpage.html.erb

  <%= render @user %>

Upvotes: 1

Views: 311

Answers (1)

Lucas Moulin
Lucas Moulin

Reputation: 2530

You must define a route for your new action at config/routes.rb:

get '/otherpage', to: 'users#otherpage'

The you can use:

<%= link_to image_tag("/assets/go.png", :alt => "Image Description", :action => "otherpage"), otherpage_path(user.id) %>

As mentioned by @archana you can run rake routes from the terminal to see all available routes including the one you just created.

Upvotes: 1

Related Questions