Domanda

I am new to Ruby on Rails I am using Michael Hartl's tutorial on RoR and trying to add Paperclip to do profile pictures for users. I am also using Friendly_Id on the users if that helps.

I get this error for one of my views. I am not sure why it is saying I am missing required keys, shouldn't @signed_in_user already reference the current signed in user object?

No route matches {:controller=>"users", :action=>"show"} missing required keys: [:id]

Offending line in the view is

<%= link_to image_tag(@signed_in_user.avatar.url(:thumb), class:"profile_picture"), user_url %>

The controller that calls it is StoriesController#create: (Stories is basically the same as microposts a lá his tutorial)

class StoriesController < ApplicationController
  before_action :signed_in_user, only: [:create, :destroy]
  before_action :correct_user,   only: [:edit, :update]

def create
  if signed_in?
    @signed_in_user = self.current_user
    @story = current_user.stories.build
  end
  if @story.save
    flash[:success] = "Story Created!"
    redirect_to root_url
  else
    render 'users/show'
  end
end

The SessionsHelper that has methods being used above:

module SessionsHelper

  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.hash(remember_token))
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    remember_token = User.hash(cookies[:remember_token])
    @current_user ||= User.find_by(remember_token: remember_token)
  end

  def current_user?(user)
    user == current_user
  end

  def sign_out
    current_user.update_attribute(:remember_token, User.hash(User.new_remember_token))
    cookies.delete(:remember_token)
    self.current_user = nil
  end

  def signed_in_user

    unless signed_in?
      store_location
      redirect_to signin_url, notice: "Please Sign in."
    end
  end

end
È stato utile?

Soluzione

I think you are mixing up two different semantics. Either you use

link_to image_tag(...), @signed_in_user

or you use

link_to image_tag(...), user_url(@signed_in_user)  

(or user_path(user) )

Currently you're using the second option, but without the user, that's what the error is complaining about: "missing required keys: [id]"

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top