user3283997
user3283997

Reputation:

Rails not rendering partials

My models has users who can create projects. I'm trying to render a partial inside the show action of my user controller.

<% if current_user %>
  <%= render(:partial => "projects/new_project") %>
<% end %>

This is the code for the show method

def show
    @user = User.find(params[:id])
    @project = Project.new
    @characters = Character.sorted
    @personalities = Personality.sorted
    @industries = Industry.sorted
    @project_types = ProjectType.sorted
  end

And this is my helper method current_user

def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
end

helper_method :current_user

This is the partial view _new_projects

<div class="row">
    <div class= "col-lg-5 col-md-5 col-xs-5">

        <% form_for @project do |f| %>
            <%= f.text_field(:title, placeholder: "Give your project a name", :class => 'form-control') %>

            <%= f.select(:project_type_id, @project_types.map {|n| [n.project_type_name, n.id]}, {}, :class => 'form-control') %>

            <%= f.select(:industry_id, @industries.map {|n| [n.industry_name, n.id]},{}, :class => 'form-control') %>

            <%= f.select(:character_id, @characters.map {|n| [n.character_name, n.id]},{}, :class => 'form-control') %>

            <%= f.select(:personality_id, @personalities.map {|n| [n.personality_name, n.id]},{}, :class => 'form-control') %>

            <%= f.text_area(:description, :class => 'form-control') %>

            <div class='padding-top'></div>

            <%= submit_tag("Post project", :class => 'btn btn-success btn-sm sniglet') %>

        <% end %>
    </div>
</div>

The page renders blank without the partial

Upvotes: 0

Views: 67

Answers (2)

Mohamad
Mohamad

Reputation: 35349

It's hard to know the problem without more information. But, judging by the limited code you provided a few things pique my interest:

  1. Perhaps current_user is evaluating to false and the partial is not being rendered?
  2. Why have the current_user check in the view? Why not limit the action to signed in users at the controller level?
  3. Perhaps your partial expects a Project instance, and it's not being passed?

I would be helpful to see the controller, the partial, and the current_user code.

Upvotes: 1

Sergey Moiseev
Sergey Moiseev

Reputation: 2963

You just need to add temporary log for current_user value:

def current_user
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
  Rails.logger.info("!!!#{@current_user}")
end

and you will see your problem. Also here is small tip about if in template:

<%= render(:partial => "projects/new_project") if current_user %>

reads better as it seems for me.

Upvotes: 1

Related Questions