The probleming code:
<%= if flash[:notice] %>
<div class="notification is-primary global-notification">
<p class="notice"><%= notice %></p>
</div>
<% end %>
<%= if flash[:alert] %>
<div class="notification is-danger global-notification">
<p class="alert"><%= alert %></p>
</div>
<% end %>
And when running my server the following error drops:
Already changed the syntax to: if (flash[:notice]). But still, drop this. Any idea?
<%= if flash[:notice] %>
The above code should be:
<% if flash[:notice] %>
Note
<% %> - Executes the ruby code within the brackets.
<%= %> - Prints something into ERB file.
Related
Below is code extract from my file app/views/discussions/show.html.erb where line 16 raised this error:
undefined method `markdown' for #<#<Class:0x000000000c94e0d8>:0x000000000c94c6e8>
s<div class="columns">
<div class="column is-8">
<h1 class="title is-2 has-text-grey discussion-title"><%= #discussion.title %></h1>
<h3 class="subtitle is-5 has-text-grey-lighter">by <%= #discussion.user.username %> in <%= link_to #discussion.channel.channel, #discussion.channel %></h3>
<div class="level">
<div class="level-left"></div>
<div class="level-right">
<% if discussion_url(#discussion) %>
<div class="buttons">
<%= link_to 'Edit Discussion', edit_discussion_path(#discussion), class:'button'%>
<%= link_to 'Delete', discussion_path(#discussion), method: :delete, data: { confirm: "Delete discussion?" }, class:'button' %>
</div>
<% end %>
</div>
</div>
<div class="content"><%= markdown (#discussion.content) %></div>
<!-- ^^^^^^^^ -->
<h2 class="subtitle is-5 has-text-grey"><%= #discussion.replies.count %> Replies</h2>
<div id="discussion-replies">
<%= render #discussion.replies %>
</div>
<hr/>
<h3 class="subtitle is-3 has-text-grey">Leave a reply</h3>
<% if user_signed_in? %>
<%= render 'replies/form' %>
<% else %>
<p>To reply you need to <%= link_to 'login', new_user_session_path %>. Don't have an account?
<%= link_to 'Sign up', new_user_registration_path %> for one.</p>
<% end %>
</div>
<%= render 'sidebar' %>
</div>
cant view reply or comment section
Rails doesn't include a markdown method. You'll have to use a gem, write something yourself or a combination of the two. You could for example use the redcarpet gem, or one of the other markup processor gems.
Then write your own helper using this using this gem.
# app/helpers/markdown_helper.rb
module MarkdownHelper
MARKDOWN = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
def markdown(markdown_string)
MARKDOWN.render(markdown_string).html_safe
end
end
For usage and possible render configurations checkout out the redcarpet documentation.
With this helper present you can simply do the following in the view:
<%= markdown(#discussion.content) %>
#conversation doesn't seem to load the actual conversation. I get a nilClass error when loading an individual conversation and I'm not sure why. I feel like I'm missing something silly.
Here is the error (which persists to any use of conversation in the show view):
undefined method `participants' for nil:NilClass
Extracted source (around line #1):
<% conversation.participants.each do |participant| %>
<% unless participant == current_user %>
<%= gravatar_for participant %>
<% end %>
<% end %>
Rails.root: /home/alex/workspace/meetexplore_app
Application Trace | Framework Trace | Full Trace
app/views/conversations/_participants.html.erb:1:in `_app_views_conversations__participants_html_erb___865731980245228929_7035 9899418680'
app/views/conversations/show.html.erb:4:in `_app_views_conversations_show_html_erb__775537665966091139_41963900'
My conversations/show.html.erb file:
<% page_header "Conversation" %>
<p>Chatting with
<%= render 'conversations/participants', conversation: #conversation %>
</p>
<div class="panel panel-default">
<div class="panel-heading"><%= #conversation.subject %></div>
<div class="panel-body">
<div class="messages">
<% #conversation.receipts_for(current_user).each do |receipt| %>
<div class="media">
<% message = receipt.message %>
<div class="media-left">
<%= gravatar_for message.sender, 45, message.sender.name %>
</div>
<div class="media-body">
<h6 class="media-heading"><%= message.sender.name %>
says at <%= message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></h6>
<%= message.body %>
</div>
</div>
<% end %>
</div>
</div>
</div>`
My _participants.html.erb partial:
<% conversation.participants.each do |participant| %>
<% unless participant == current_user %>
<%= gravatar_for participant %>
<% end %>
<% end %>
Yet this index works:
<% page_header "Your Conversations" %>
<p><%= link_to 'Start conversation', new_message_path, class: 'btn btn-lg btn-primary' %></p>
<ul class="list-group">
<%= render partial: 'conversations/conversation', collection: #conversations %>
</ul>
<%= will_paginate %>
_conversation.html.erb partial:
<li class="list-group-item clearfix">
<%= link_to conversation.subject, conversation_path(conversation) %>
<p><%= render 'conversations/participants', conversation: conversation %></p>
<p><%= conversation.last_message.body %>
<small>(<span class="text-muted"><%= conversation.last_message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></span>)</small></p>
</li>
So I was missing the before_action and function get_conversation to actually find the conversation with the proper id... woops!
I'm learning Rails and am implementing an app for which I decided to go through the Site Point tutorial.
According to it, I've done the following in apps/views/users/new.html.erb
<%= form_for #user do |f| %>
<%= render 'shared/errors', object: #user %> #=> error
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, placeholder: 'Your display name', class: 'form-control', required: true %>
</div>
<% end %>
But I'm getting
ActionController:MissingTemplate in Users#new
How can I get rid of it? I've posted the full trace here
The tutorial you're following assumes you have an _errors.html.erb file in the app/views/shared directory.
<%= render 'shared/errors', object: #user %>
That line of code is trying to render that partial and pass #user which will be used in the partial.
If you want to remove that error then remove that line of code. If you want to display those errors then create the relevant file and read up on how to display errors in the view.
<% if object.errors.any? %>
<div id="error_explanation">
<ol>
<% object.errors.full_messages.each do |msg| %>
<li class="alert alert-danger"><%= msg %></li>
<% end %>
</ol>
</div>
<% end %>
Put this in your app/views/shared/_errors.html.erb
I'm wondering what's the best practice for displaying flash messages. The two main ways I've seen are using something like this scaffold generated code
<p id="notice"><%= notice %></p>
or placing code like this in your application header.
<% if !flash.empty? %>
<div id="flash">
<% flash.keys.each do |k| %>
<div class="<%= k %>">
<%= flash[k] %>
</div>
<% end %>
</div>
<% end %>
It appears to me that the first method adds more flexibility while the latter improves code readability and eliminates redundancy. Is there a method most rails developers prefer? As a side question how does scaffolding implement notice? Is it just a helper that accesses the flash hash? Why go through the trouble of using the helper when you can directly use the flash hash? Thanks
I'm doing it this way:
<% flash.each do |key, value| %>
<%= content_tag :div, value, class: "flash #{key}" %>
<% end %>
Calling a partial keeps your application.html.erb even cleaner..
<%= render 'shared/flash_messages' if !flash.empty? %>
.. and in the partial do something like what #zolter mentioned:
<div id="flash_messages">
<% flash.each do |key, value| %>
<%= content_tag(:div, value, :class => "flash #{key}") %>
<% end %>
</div>
Why not put the second method on a helper function so it doesn't affect code readability on layouts?
<% if flash[:notice] %>
<div class="notification is-primary global-notification">
<p class="notice"><%= notice %></p>
</div>
<% end %>
<% if flash[:alert] %>
<div class="notification is-danger global-notification">
<p class="alert"><%= alert %></p>
</div>
<% end %>
I'm trying to adjust the standard application template in Rails to insert a class around the notice and alert messages, but I can't seem to find an elegant way of doing it.
At present, I have the scaffold:
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
I want it to only show the surrounding tags if a notice or alert is present.
Use an if statement.
<% if notice %>
<p class="notice"><%= notice %></p>
<% end %>
<% flash.each do |name, msg| %>
<%= content_tag :div, :id => "flash_#{name}", :class => "my_class" do %>
<%= msg %>
<% end %>
<% end %>
Define styling for the .my_class
Wrap the parts in if like this:
<% if alert %>
<p class="alert"><%= alert %></p>
<% end %>
You can also use unless if you like