Zmienna niewidoczna w view erb a widoczna w kontrolerze

hej,
próbuję uruchomić savage beast 2, taki plugin do forum, i już wszystko jest ok :wink: tyle ze nie działają mi niektóre widoki.
Wywala mi się tak cudnie

[code]ou have a nil object when you didn’t expect it!
The error occurred while evaluating nil.display_name

Extracted source (around line #8):

5:
6:
7:

<%= ‘New Topic’[] %>


8:

<%= ‘by {user}’[:by_user, current_user.display_name] %>


9:
10: <%= error_messages_for :post %>
11: <%= error_messages_for :topic %>[/code]
Problem wydawał by się prosty, wyglądało by na to że zmienna current_user nie jest ustawiona, niemiej jak w kontrolerze zaloguje sobie jej wartość to jest ok

def new @topic = Topic.new logger.info "kku_new_topic: #{current_user.inspect}" end
natomiast w view nie jest :frowning:

[code]

<%= ‘New Topic’[] %>

<%= 'by {user}'[:by_user, current_user.display_name] %>

[/code] z góry dzięki za podpowiedz gdzie szukać przyczyny. Pozdrawiam Krzysiek

Jeśli metoda current_user jest widoczna tylko w kontrolerze to dodaj taki wpis:

helper_method :current_user

brakowało mi SavageBeast::AuthenticationSystem ale to nic nie zmienia :frowning:
teraz mam:

class ApplicationController < ActionController::Base helper :all # include all helpers, all the time SavageBeast::AuthenticationSystem ... ...
i metody są zaimplementowane, o czym świadczyć może przypisanie wartości do zmiennej current_user w kontrolerze, co widać w logu …
pozdro i dzieki
krzysiek

Ale metoda dodana do kontrolera nie jest widoczna w widoku. Po to prosiłem o dodanie ‘helper_method’.

Hej,
Przeczytałem chyba twojego posta przed zmianą :slight_smile:
Dodałem helper_method :current_user do mojego kontrolera aplikacji

[code]# Filters added to this controller apply to all controllers in the application.

Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
SavageBeast::AuthenticationSystem
helper_method :current_user[/code]
niemniej z tego co widzę to helper_method zakłada że zwracanym typem jest string, bo w view wywala sie teraz tak

[code] NoMethodError in Topics#new

Showing vendor/plugins/savage_beast/app/views/topics/new.html.erb where line #9 raised:

undefined method `display_name’ for #String:0x6dd7330[/code]
a view jest zdefiniowane tak

[code]

<%= ‘New Topic’[] %>


<% logger.info “kku_current_user_6666: #{current_user.methods.inspect}” %>

<%= 'by {user}'[:by_user, current_user.display_name] %>

[/code] pozdrawiam Krzysiek

No to teraz mogę napisać: RTFM :).

Musisz zaimplementować te metody (dokładnie to są 3: display_name, admin? i metoda klasowa currently_online).
Z kolei Twoja metoda current_user ma zwracać obiekt modelu usera (prawdopodobnie klasa User). U Ciebie zwraca chyba String (tak przynajmniej wnioskuję po błędzie, helper_method niczego nie oczekuje, on po prostu udostępnia daną metodę kontrolera w widoku).

Btw, jeśli chcesz coś logować w widoku to spróbuj <%= debug zmienna %>.

Hej,
Mam to zrobione, może zle

Kontroler aplikacji

[code]# Filters added to this controller apply to all controllers in the application.

Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
SavageBeast::AuthenticationSystem
helper_method :current_user

session :session_key => ‘_mpr_session_id’

init_gettext “mpr”

See ActionController::RequestForgeryProtection for details

Uncomment the :secret if you’re not using the cookie session store

protect_from_forgery # :secret => ‘ccd4c63eee306fdccec9febc505e4ce0’

#protected methods
protected

Protect a page from unauthorized access.

def protect
unless logged_in?
session[:protected_page] = request.request_uri
flash[:notice] = “Please log in first”
redirect_to :action => “login”, :controller => “family”
return false

end

end

savage beast methods

def login_required
 false
end

def current_user
  @current_user ||= ((session[:family_id] && User.find_by_id(session[:family_id])) || 0)
  logger.info "kku_current_user_666: #{@current_user.inspect}"
  logger.info "kku_current_user_6661: #{@current_user.methods.inspect}"
end

def authorized?() 
  true 
  # in your code, redirect to an appropriate page if not an admin
end

def logged_in?
  current_user != 0
end

def admin?
  true
  #logged_in? && current_user.admin?

end

end[/code]
i model Usera

[code]class User < ActiveRecord::Base
set_table_name “families”
include SavageBeast::UserInit

4 methods defined for savage beast plugin

def display_name
screen_name
end

def admin?
true
end

def currently_online
nil
end

def build_search_conditions(query)
query && [‘LOWER(screen_name) LIKE :q OR LOWER(screen_name) LIKE :q’, {:q => “%#{query}%”}]

query

end

end[/code]

[code=ruby]# poprawki
include SavageBeast::AuthenticationSystem #dodane include

helper_method :current_user # wywal za definicję metody current_user

@current_user ||= ((session[:family_id] && User.find_by_id(session[:family_id])) || 0)

strasznie przecudowane :slight_smile:

@current_user ||= session[:family_id] ? User.find_by_id(session[:family_id]) : nil[/code]

super, teraz działa, wielkie wielkie dzieki
Krzysiek

jeszcze tylko jeden problem,
przez to że dodałem do routa

map.from_plugin :savage_beast map.resources :users
przestała mi działać simple captcha
wywala sie

[code] NoMethodError in Family#register

Showing family/register.rhtml where line #24 raised:

undefined method `simple_captcha_url’ for #ActionView::Base:0x520b3f4[/code]
cały route wygląda tak

[code]ActionController::Routing::Routes.draw do |map|

kkuczek captcha

map.simple_captcha ‘/simple_captcha/:action’, :controller => ‘simple_captcha’

kkuczek mapowanie dla controlera childre

map.connect ‘/child/book_edit/:id/:page_no’, :controller => ‘child’, :action => ‘book_edit’

savage beast 2

map.from_plugin :savage_beast
map.resources :users

end of savage beast 2

kkuczek

map.connect ‘’, :controller => “site”

Install the default routes as the lowest priority.

map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’
end[/code]
z gory dzieki za porade :slight_smile:
krzysiek