0

In a class, I'm built a Pintrest style app, and I recently set up a show action for my users. The link works fine on the individual pins, but when I try to add the users#show link to the Navbar called "Profile" it does not navigate anywhere. The Profile link should navigate to users#show, but the URL is showing for Profile is my pins#index which is my root route. Here are the links in my Navbar under _header.html.erb.

      <div class="nav-collapse collapse">
        <ul class="nav pull-right">
        <% if user_signed_in? %>
          <li><%= link_to "Add Trip", new_pin_path %></li>
          <li><%= link_to "About", about_path %></li>
          <li><%= link_to "Profile", @user %></li>
          <li><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
        <% else %>
          <li><%= link_to "About", about_path%></li>
          <li><%= link_to "Login", new_user_session_path %></li>
        <% end %>
          </ul>
      </div>

Here is my users_controller.rb

class UsersController < ApplicationController
def show
    @user = User.find(params[:id])
    @pins = @user.pins.page(params[:page]).per_page(20)
  end
end

But the show user link on the pins in _pin.html.erb which is rendered in my pins#index works fine.

<div class="box">
  <p class= "description">
    <%= pin.description %>
  </p>
  <%= link_to (image_tag pin.image(:medium)), pin %>
  <p class= "author">
    <strong>
      Posted by <%= link_to pin.user.name, pin.user %>
    </strong>
  </p>
</div>

Lastly, if I copy the same link that is in _pin.html.erb <%= link_to pin.user.name, pin.user %> to the Navbar, I get undefined local variable or methodpin' for #<#:0x00000100c82520>`.

Here is my routes.rb incase that helps.

devise_for :users
match 'users/:id' => 'users#show', as: :user
4

1 回答 1

2

您需要使用current_user而不是@user,因为@user在大多数情况下将为零(除了您的 users#show 页面。

/profile创建一个始终加载当前用户的路由也是一个好主意。

于 2013-09-05T16:25:07.480 回答