ruby on rails - NoMethodError in UsersController#show -
i'm getting following error in app when visit user profile page not signed app (i'm using devise):
nomethoderror in userscontroller#show undefined method `connections' nil:nilclass
app/controllers/users_controller.rb:19:in `show'
when i'm logged in error goes away. know why it's failing. need coming proper solution.
the error occurs on line in users controller:
def show @connection = current_user.connections.build(user_id: current_user, otheruser_id: @user) end
a connection form appears users logged app (simply put, button appears asks if connect person friend). however, i'm checking if user logged in on user view "show" page <% if user_signed_in? %>
before form.
here's relevant code view:
<%= render 'connect_form' if user_signed_in? %>
connect_form
<% unless current_user == @user %> <% if @contact.present? && user_signed_in? %> <%= @user.first_name %> <%= @contact.description %> (<%= link_to "edit contact", edit_connection_path(:id => @contact.id, :user => @user) %>)<br \> <% else %> <% if user_signed_in? %>how know <%= @user.first_name %>? (<%= link_to "edit contact", new_connection_path(:user => @user) %> ) <% else %> <% end %><br \> <% end %> <% end %>
connection_form (creating new one)
<% if user_signed_in? %> how know <%= @user.first_name %>? <%= form_for(@connection) |f| %> <%= f.collection_select :description, connection::connectiontype, :to_s, :to_s, :include_blank => true %> <%= f.hidden_field(:otheruser_id, :value => @user.id) %> <%= f.submit "save", class: "btn btn-primary btn-small" %> <% else %> <% end %>
why app trying load nil array `@connections when have check user_signed_in? appreciated!!
first thing put check build connection if current user exist.
def show @connection = current_user.connections.build(user_id: current_user, otheruser_id: @user) if current_user end
the main thing notice the: if current_user
@ end of line
Comments
Post a Comment