function Discuss.TopicView.render/2 is undefined (module Discuss.TopicView is not available) - phoenix-framework

Creating a Phoenix framework application where developers can add topics for discussion and everything was running smoothly until I added this code inside of templates/view/index.html.eex:
<h5>Topics</h5>
<ul class="collection">
<%= for topic <- #topics do %>
<li class="collection-item">
<%= topic.title %>
</li>
<% end %>
</ul>
<div class="fixed-action-btn">
<%= link to: topic_path(#conn, :new), class: "btn-floating btn-large waves-effect waves-light red" %>
<i class="material-icons">add</i>
<% end %>
</div>
The problem does seem to be the syntax above, but how do I go about applying the plus icon inside of the round red button?
In my terminal I am getting:
== Compilation error in file web/views/topic_view.ex ==
** (EEx.SyntaxError) web/templates/topic/index.html.eex:14: unexpected end of expression <% end %>
I am working with: Phoenix v1.2.5

A big gotcha here if you are not careful.
It was definitely a syntax error. When creating link tags in Phoenix ensure you add the dokeyword at the end before closing Phoenix tag.
So instead of:
<div class="fixed-action-btn">
<%= link to: topic_path(#conn, :new), class: "btn-floating btn-large waves-effect waves-light red" %>
<i class="material-icons">add</i>
<% end %>
</div>
write it like this:
<div class="fixed-action-btn">
<%= link to: topic_path(#conn, :new), class: "btn-floating btn-large waves-effect waves-light red" do %>
<i class="material-icons">add</i>
<% end %>
</div>
I only noticed it missing after seeing this post:
https://elixirforum.com/t/how-to-add-i-tag-in-to-link-function/12040

Related

how to "Each do |article|" but after every n-articles it has to start in a new row(foundation)

I got this Rails blogs app and for the indexpage where the articles are posted it shows a short version of the article.
<%= render 'layouts/header' %>
<%= render 'layouts/topbar' %>
<div class="contentnews">
<%= render 'layouts/bannerad' %>
<div class="row">
<% if current_user.try(:admin?) %>
<%= link_to 'Nieuws toevoegen', new_article_path, :class => "button ala" %>
<% end %>
</div>
<div class="row">
<%= render 'overview' %>
</div>
</div>
The overview-partial :
<div class="articlebox ">
<div class="large-4 columns">
<% #articles.reverse_each do |article| %>
<div class="articlebox2">
<div class="articleindex">
<%= link_to article.title, article %>
</div>
<div class="articlebody">
<%= article.short.to_s.html_safe %>
</div>
</div>
<% end %>
</div>
</div>
The problem is that this way all the articles end up in one row. I want it to show the short articles inline on the page and after every two or three article it has to start in a new row with again two or three short articles.
Anybody got any idea?
You don't actually need to create a row every n-elements. With foundation just wrap all the elements in a row, and give them columns class:
<div class="articlebox">
<div class="large-4 columns">
<div class="row"> <!-- Wrap in a row -->
<% #articles.reverse_each do |article| %>
<div class="articlebox2 large-6 columns"> <!-- Give a column and width class here -->
...
</div>
<% end %>
</div>
So you can nesting grids with foundation, and creating new rows is not necessary, foundation will just wrap if the elements exceeds the grid.
If you actually really want to create rows, you can have a look at the method each_with_index API and then insert row elements if index % 2 == 0. But this is unnecessary complexity.

undefined method `errors' for nil:NilClass using RoR

I am getting the following error in my login form when i am trying to attempt by putting the wrong data which should usually shows the login failed message.
Error:
NoMethodError in Sessions#loginadmin
Showing C:/Site/swargadwar_admin/app/views/homes/index.html.erb where line #71 raised:
undefined method `errors' for nil:NilClass
Extracted source (around line #71):
68: <% end %>
69: </div>
70: <div class="error-div">
71: <% if #admin.errors.any? %>
72: <div id="error_explanation">
73: <h2><%= pluralize(#admin.errors.count, "error") %> prohibited this post from being saved:</h2>
74:
Please check my codes below and let me to know where i am doing the mistake.
views/homes/index.html.erb
<div class="container">
<div style="text-align:center;"><img src="/assets/admin.png" style="width:100px; height:120px; " /></div>
<div class="text-div" style="text-align:center;">Swargadwar, Puri Municipality,govt of odisha</div>
<section>
<% if !current_user %>
<div id="container_demo" >
<!-- hidden anchor to stop jump http://www.css3create.com/Astuce-Empecher-le-scroll-avec-l-utilisation-de-target#wrap4 -->
<a class="hiddenanchor" id="toregister"></a>
<a class="hiddenanchor" id="tologin"></a>
<div id="wrapper">
<div id="login" class="animate form">
<%= form_for :admin,:url => {:action =>'loginadmin',:controller => 'sessions' } do |f| %>
<h1>Log in</h1>
<p>
<label for="username" class="uname" data-icon="u" > Your email or username </label>
<%= f.email_field :email,placeholder:"mysupermail#mail.com",:id => "username" %>
</p>
<p>
<label for="password" class="youpasswd" data-icon="p"> Your password </label>
<%= f.password_field :password,placeholder:"eg. X8df!90EO",:id => "password" %>
</p>
<p class="keeplogin">
<%= f.check_box :remember_me,:id => "loginkeeping" %>
<label for="loginkeeping">Keep me logged in</label>
<%= link_to 'Forgetting password ?',admins_forget_path %>
</p>
<p class="login button">
<%= f.submit "Login" %>
</p>
<p class="change_link">
Not a member yet ?
Join us
</p>
<% end %>
</form>
</div>
<div id="register" class="animate form">
<%= form_for :admin,:url => {:action => 'create_registration',:controller => "admins" } do |f| %>
<h1> Sign up </h1>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Your username</label>
<%= f.text_field :user_name,placeholder:"mysuperusername690",:id => "usernamesignup" %>
</p>
<p>
<label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
<%= f.email_field :email,placeholder:"mysupermail#mail.com",:id => "emailsignup" %>
</p>
<p>
<label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
<%= f.password_field :password,placeholder:"eg. X8df!90EO",:id => "passwordsignup" %>
</p>
<p>
<label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Please confirm your password </label>
<%= f.password_field :password_confirmation,placeholder:"eg. X8df!90EO",:id => "passwordsignup" %>
</p>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Add Image</label>
<%= f.file_field :picture %>
</p>
<p class="signin button">
<%= f.submit "Sign Up"%>
</p>
<p class="change_link">
Already a member ?
Go and log in
</p>
<% end %>
</div>
<div class="error-div">
<% if #admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#admin.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #admin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
</section>
</div>
controller/sessions_controller.rb
class SessionsController < ApplicationController
def loginadmin
#admin=Admin.authenticate(params[:admin][:email], params[:admin][:password])
if #admin
session[:user_id]=#admin.id
cookies.signed[:user_id]=#admin.id
params[:admin][:remember_me] == '1' ? remember(#admin) : forget(#admin)
flash[:notice]="Login Successfull"
flash[:color]="valid"
redirect_to :action => "new", :controller => "admins"
else
flash[:notice]="Login Failed"
flash[:color]="invalid"
render 'homes/index'
end
end
def removeuser
session[:user_id] = nil
cookies.delete :user_id
flash[:notice]="user logged out successfully"
flash[:color]="valid"
redirect_to :action => 'index', :controller => 'homes'
end
end
controller/homes_controller.rb
class HomesController < ApplicationController
def index
#admin=Admin.new
end
end
Please help me to resolve this error.
Error is self explanatory.
NoMethodError in Sessions#loginadmin
undefined method `errors' for nil:NilClass
You are getting this error because #admin is nil and you are calling errors method on a nil object..
If you look loginadmin action is sessions controller you are rendering 'index' template when there is no #admin object, which is causing this error.
Fix:
Either create separate templates for both these actions with common code in partials or simply use rails try method which returns nil rather than raising an exception
<% if #admin.try(:errors).try(:any?) %>
OR
Just use a condition at top to separate out error code:
<% if #admin %>
<div class="error-div">
<% if #admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#admin.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #admin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
<% end %>
P.S I would recommend you to use different templates with common code separated in partials.

Use partial in flash message

In my ActionController I have the following:
format.html {
flash[:notice] = "Success"
redirect_to #event
}
However, I want to use a partial for the flash notice. I tried using render :partial but this gave me double render errors. Any ideas if this can be done?
Try using render_to_string This should let you render a partial a string without getting a double render error.
Flash messages partial code
<div class="container">
<div id="messages">
<div class="message-container">
<% flash.each_with_index do |(k,v),i| %>
<div id="message-<%= i %>" class="actual-message alert-<%= k.to_s.sub('_stay', '') %>">
<a class="close" data-dismiss="alert" style="float:right">x</a>
<% if v.is_a?(Array) %>
<ul>
<% v.each do |vv| %>
<li><%= vv %></li>
<% end %>
</ul>
<% else %>
<%= raw v %>
<% end %>
</div>
<% end %>
</div>
</div>
</div>
render in your application.html.erb using
"layouts/flash_messages" %>

Creating Gallery Page/Nested Resource

I am trying to create a gallery page that links to photo albums. The albums work fine, but I am trying to pull the first image from each gallery_id to the gallery page. I have a Gallery has many photos, and photos belong to gallery. What I am getting is the first image loading for each album.
class GalleriesController < ApplicationController
def index
#gallery = Gallery.paginate(page: params[:page]).per_page(6)
#photos = Photo.find(:all, :limit => 1)
end
def show
#gallery = Gallery.find(params[:id])
#photos= #gallery.photos.all
end
end
galleries/index.html.
<% provide(:title, 'Photo Galleries') %>.
<div id="galleries">
<%= will_paginate #gallery %>
<ul class="thumbnails">
<% #gallery.each do |gallery| %>
<li class="span4">
<div class="thumbnail">
<% #photos.each do |photo| %>
<%= link_to image_tag(photo.image), gallery_path(gallery)%>
<% end %>
<h4><%= gallery.name %></h4>
</div>
</li>
<% end %>
</ul>
</div>
routes
resources :galleries, :has_many => :photos
any help would be appreciated.
Pretty sure this is what you want:
class GalleriesController < ApplicationController
def index
#gallery = Gallery.paginate(page: params[:page]).per_page(6)
end
end
_
# galleries/index.html
<% provide(:title, 'Photo Galleries') %>
<div id="galleries">
<%= will_paginate #gallery %>
<ul class="thumbnails">
<% #gallery.each do |gallery| %>
<li class="span4">
<div class="thumbnail">
<%= link_to image_tag(gallery.photos.first.image), gallery_path(gallery) %>
<h4><%= gallery.name %></h4>
</div>
</li>
<% end %>
</ul>
</div>
The problem in yours is that in your index action you grabbed ALL the images, regardless of what gallery they belong to, and then you looped through all of them in your view and displayed them.
Because of your association (gallery has_many photos), you can access a gallery's photos with gallery.photos.
In my example, I am displaying the first image for each gallery: gallery.photos.first
If you want a random image from the gallery in question you can use sample. i.e. gallery.photos.sample
You have to use relations, which is what you need here. I tried to fix the code and added comments.
class GalleriesController < ApplicationController
def index
#gallery = Gallery.paginate(page: params[:page]).per_page(6)
# You don't need the photos. You have to access them through gallery,
# or you will get always all photos independent of the gallery.
##photos = Photo.find(:all, :limit => 1)
end
This is the view you are looking for
# galleries/index.html.erb
<% provide(:title, 'Photo Galleries') %>.
<div id="galleries">
<%= will_paginate #gallery %>
<ul class="thumbnails">
<% #gallery.each do |gallery| %>
<li class="span4">
<div class="thumbnail">
<% gallery.photos.each do |photo| %>
<%= link_to image_tag(photo.image), gallery_path(gallery)%>
<% end %>
<h4><%= gallery.name %></h4>
</div>
</li>
<% end %>
</ul>
</div>
If you only want to show the first picture of every gallery, you have to change the view this way:
# galleries/index.html.erb
<% provide(:title, 'Photo Galleries') %>.
<div id="galleries">
<%= will_paginate #gallery %>
<ul class="thumbnails">
<% #gallery.each do |gallery| %>
<li class="span4">
<div class="thumbnail">
<%= link_to image_tag(gallery.photos.first.image), gallery_path(gallery)%>
<h4><%= gallery.name %></h4>
</div>
</li>
<% end %>
</ul>
</div>

Issue in kaminari ajax pagination

Am using kaminari ajax pagination in my project. It is working fine, But contents are displaying number of pages times in the page. For example if number of items per pages is 7, then it is displaying 7 times the same content. What am doing is
In product_details controller
def index
#products=ProductDetail.where("department_id = ? and category_id = ?",1, 1).page(params[:page]).per(15)
end
In product_details/index.html.erb
<div id="product_details">
<%= render #products %>
</div>
<div id="paginator">
<%= paginate #products, :remote=>true %>
</div>
In product_details/index.js.erb
$('#product_details').html('<%= escape_javascript render (#products) %>');
$('#paginator').html('<%= escape_javascript(paginate(#products, :remote=>true).to_s)%>');
In product_details/_product_detail.html.erb
<div id="product_list">
<% #products.each do | product | %>
<div class="product_container">
<div class="product_box" >
<div id="future_image_container">
<div class="image_block" >
<%= image_tag(product.image_path, :alt => "product image", :height=>"215px") %>
</div>
<span id="future_price" style="text-decoration: line-through; color: #9e9c9c;">
<span style="color: black;">
<%= "$#{product.price}" %>
</span>
</span>
<div id="circle">
<p>
<% if(product.discount > 0) %>
<% new_price=((2.0*((product.price*product.discount)/100)).round)/2.0 %>
<% else %>
<% new_price=product.price %>
<% end %>
<%= "$#{new_price}"%>
</p>
</div>
</div>
<p class="future_product_name">
<%= product.name %>
</p>
<% #brands=Brand.where("id=?",product.brand_id)
#brands.each do |brand|
#brandname=brand.name
end
%>
<p class="future_product_name">
<%= "Brand : #{#brandname}" %>
</p>
</div>
</div>
<% end %>
</div>
Please help me to solve this problem
I noticed that when i use <%= render #products %> how many items i have taken per page that number of times it repeates. Therefore I solved this by the following code:
In product_details controller
def index
#products=ProductDetail.where("department_id = ? and category_id = ?",1, 1).page(params[:page]).per(15)
end
In product_details/home.html.erb
<div id="product_details">
<%= render 'index' %>
</div>
In product_details/index.js.erb
$('#product_details').html('<%= escape_javascript render ('index') %>');
In product_details/_index.html.erb
<div id="product_list">
<%= paginate #products, :remote=>true %>
<% #products.each do | product | %>
<div class="product_container">
<div class="product_box" >
<div id="future_image_container">
<div class="image_block" >
<%= image_tag(product.image_path, :alt => "product image", :height=>"215px") %>
</div>
<span id="future_price" style="text-decoration: line-through; color: #9e9c9c;">
<span style="color: black;">
<%= "$#{product.price}" %>
</span>
</span>
<div id="circle">
<p>
<% if(product.discount > 0) %>
<% new_price=((2.0*((product.price*product.discount)/100)).round)/2.0 %>
<% else %>
<% new_price=product.price %>
<% end %>
<%= "$#{new_price}"%>
</p>
</div>
</div>
<p class="future_product_name">
<%= product.name %>
</p>
<% #brands=Brand.where("id=?",product.brand_id)
#brands.each do |brand|
#brandname=brand.name
end
%>
<p class="future_product_name">
<%= "Brand : #{#brandname}" %>
</p>
</div>
</div>
<% end %>
</div>
Now no product repeates and ajax pagination is also working fine

Resources