[英]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.
我正在尝试在我的用户控制器的show动作中呈现部分内容。
<% if current_user %>
<%= render(:partial => "projects/new_project") %>
<% end %>
This is the code for the show method 这是show方法的代码
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 这是我的帮助方法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 这是局部视图_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 页面呈现为空白而没有部分
It's hard to know the problem without more information. 没有更多信息,很难知道问题。 But, judging by the limited code you provided a few things pique my interest:
但是,从有限的代码判断,你提供了一些让我感兴趣的东西:
current_user
is evaluating to false
and the partial is not being rendered? current_user
正在评估为false
而部分未被渲染? current_user
check in the view? current_user
? Why not limit the action to signed in users at the controller level? Project
instance, and it's not being passed? Project
实例,它没有被传递? I would be helpful to see the controller, the partial, and the current_user
code. 我会看到控制器,部分和
current_user
代码。
You just need to add temporary log for current_user value: 您只需要为current_user值添加临时日志:
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. 对我来说看起来更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.