如何在rails中的Home#index中解决这个NoMethodError?

时间:2012-05-09 08:37:41

标签: ruby-on-rails session

我在会话中存储了一些值,这就是我在控制器中存储值的控制器:

def callback
    client = LinkedIn::Client.new("ssssssssssss", "444444444444")
    if session[:atoken].nil?
      pin = params[:oauth_verifier]
      atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], pin)
      session[:atoken] = atoken
      session[:asecret] = asecret
    else
      client.authorize_from_access(session[:atoken], session[:asecret])
    end
    @loggedin_linkedin_userinfo = client.profile(:fields => [:id,:summary, :headline, :first_name, :last_name, :picture_url])
    @loggedin_linkedin_education = client.profile(:fields => [:educations])
    @loggedin_linkedin_position = client.profile(:fields => [:positions])
    @loggedin_linkedin_connections = client.connections

    session[:link_total] = @loggedin_linkedin_connections["total"]
    session[:link_headline] = @loggedin_linkedin_userinfo["headline"]
    session[:link_first_name] = @loggedin_linkedin_connections["first_name"]
    session[:link_last_name] = @loggedin_linkedin_connections["last_name"]
    session[:link_summary] = @loggedin_linkedin_connections["summary"]
    session[:picture_url] = @loggedin_linkedin_connections["picture_url"]

    redirect_to  :controller => 'home', :action => 'index'  
  end

这就是我的观点:

<div class="datainfo profile">
                        <h2 class="inner">Welcome, 
                            <% if session[:user].nil? 
                                session[:link_first_name] + " " + session[:link_last_name]
                            else
                                session[:user].first_name + " " + session[:user].last_name
                            end %>
                            </h2>
                        <p class="loc">
                            <%  
                            if session[:user].nil? 
                                Linkedinuser
                             else
                             @countryDetails = Country.find(:all, :conditions => {:id => session[:user].country_id})    
                            end
                                #for c in countryDetails
                                #c.country_name  #loop
                            %>
                            <% if session[:user].nil? 
                               Linkedinuser 
                             else
                                session[:user].city + "," + @countryDetails[0].country_name.to_s    
                            end
                            %>
                        </p>
                        <p class="job">

                        </p>
                        <p class="status">
                            <spam>
                                <% if session[:user].nil? 
                                        Linkedinuser
                                    else
                                        link_to "Complete Profile", session[:user], :class=>"small-btn" 
                                end
                            %>
                            </spam>
                            - Profile Completed %.
                        </p>
                    </div>

但是当我运行这个时,我得到以下错误,请帮助我。

undefined method `+' for nil:NilClass

Extracted source (around line #35):

32:                     <div class="datainfo profile">
33:                         <h2 class="inner">Welcome, 
34:                             <% if session[:user].nil? 
35:                                 session[:link_first_name] + " " + session[:link_last_name]
36:                             else
37:                                 session[:user].first_name + " " + session[:user].last_name
38:                             end %>

我从早上开始挣扎,但找不到解决办法。

1 个答案:

答案 0 :(得分:1)

您的会话中没有:link_first_name密钥(或nil),这就是为什么它没有为nils定义+方法。

在执行此操作之前检查您的会话:

<% if session[:user].nil? && !session[:link_first_name].nil?
  session[:link_first_name] + " " + session[:link_last_name]
else ...