I'm trying to change the look of the front page where the items are listed but I'm getting an error. Am I using deface correctly to change it? Its telling me that <%= link_to small_image(product, itemprop: "image"), url, itemprop: 'url' %> is giving me errors, if I removed then there are no pictures. What do I do?
update_products.rb
Deface::Override.new(:virtual_path =>"spree/shared/_products",
:name => "change site",
:replace =>"#products",
:text => '
<%
paginated_products = #searcher.retrieve_products if params.key?(:keywords)
paginated_products ||= products
%>
<% content_for :head do %>
<% if paginated_products.respond_to?(:num_pages) %>
<%= rel_next_prev_link_tags paginated_products %>
<% end %>
<% end %>
<div data-hook="products_search_results_heading">
<% if products.empty? %>
<div data-hook="products_search_results_heading_no_results_found">
<%= Spree.t(:no_products_found) %>
</div>
<% elsif params.key?(:keywords) %>
<div data-hook="products_search_results_heading_results_found">
<h6 class="search-results-title"><%= Spree.t(:search_results, keywords: h(params[:keywords])) %></h6>
</div>
<% end %>
</div>
<% if products.any? %>
<div id="products" class="row" data-hook>
<% products.each do |product| %>
<% url = spree.product_url(product, taxon_id: #taxon.try(:id)) %>
<div id="product_<%= product.id %>" class="col-md-3 col-sm-6 product-list-item" data-hook="products_list_item" itemscope itemtype="https://schema.org/Product">
<div class="panel panel-default">
<% cache(#taxon.present? ? [I18n.locale, current_currency, #taxon, product] : [I18n.locale, current_currency, product]) do %>
<div class="panel-body text-center product-body">
<br/>
<%= link_to small_image(product, itemprop: "image"), url, itemprop: 'url' %>
</div>
<div class="panel-footer text-center">
<span itemprop="offers" itemscope itemtype="https://schema.org/Offer">
<span class="price selling lead" itemprop="price"><%= display_price(product) %></span>
</span>
</div>
<% end %>
</div>
</div>
<% end %>
<% reset_cycle("classes") %>
</div>
<% end %>
<% if paginated_products.respond_to?(:num_pages) %>
<% end %>
')
one small quote issue for url <%= link_to small_image(product, itemprop: "image"), url, itemprop: "url" %> check this code :-
Deface::Override.new(:virtual_path =>"spree/shared/_products",
:name => "change site",
:replace =>"#products",
:text => '
<%
paginated_products = #searcher.retrieve_products if params.key?(:keywords)
paginated_products ||= products
%>
<% content_for :head do %>
<% if paginated_products.respond_to?(:num_pages) %>
<%= rel_next_prev_link_tags paginated_products %>
<% end %>
<% end %>
<div data-hook="products_search_results_heading">
<% if products.empty? %>
<div data-hook="products_search_results_heading_no_results_found">
<%= Spree.t(:no_products_found) %>
</div>
<% elsif params.key?(:keywords) %>
<div data-hook="products_search_results_heading_results_found">
<h6 class="search-results-title"><%= Spree.t(:search_results, keywords: h(params[:keywords])) %></h6>
</div>
<% end %>
</div>
<% if products.any? %>
<div id="products" class="row" data-hook>
<% products.each do |product| %>
<% url = spree.product_url(product, taxon_id: #taxon.try(:id)) %>
<div id="product_<%= product.id %>" class="col-md-3 col-sm-6 product-list-item" data-hook="products_list_item" itemscope itemtype="https://schema.org/Product">
<div class="panel panel-default">
<% cache(#taxon.present? ? [I18n.locale, current_currency, #taxon, product] : [I18n.locale, current_currency, product]) do %>
<div class="panel-body text-center product-body">
<br/>
<%= link_to small_image(product, itemprop: "image"), url, itemprop: "url" %>
</div>
<div class="panel-footer text-center">
<span itemprop="offers" itemscope itemtype="https://schema.org/Offer">
<span class="price selling lead" itemprop="price"><%= display_price(product) %></span>
</span>
</div>
<% end %>
</div>
</div>
<% end %>
<% reset_cycle("classes") %>
</div>
<% end %>
<% if paginated_products.respond_to?(:num_pages) %>
<% end %>
')
Related
I need to get the value after it is created, just below the section "NEW"
Below is the image I intended to do, and after the data was created I wanted it right there
phrases_term_controller.rb
class PhrasesTermsController < ApplicationController
before_action :authenticate_user!
before_action :set_term
def new
#phrases_term = PhrasesTerm.new
end
def create
#phrases_term = #term.phrases_terms.new(phrases_term_params)
if #phrases_term.save
redirect_to term_phrases_term_path(#term, #phrases_term), notice: "Phrases_Term was successfully created"
else
render "new"
end
end
def show
#phrases_term = PhrasesTerm.find(params[:id])
end
private
def phrases_term_params
params.require(:phrases_term).permit(:term_id, :phrase_id)
end
def set_term
#term = Term.find(params[:term_id])
end
end
View
phrases_term
new.html.erb
<h1><%= I18n.t("pages.phrases_term.new.title") %></h1>
<%= render 'form', phrases_term: #phrases_term %>
_form.html.erb
<%= form_with scope: :phrases_term, url: term_phrases_terms_path, local: true do |form| %>
<% if phrases_term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(phrases_term.errors.count, "error") %> prohibited this phrase from being saved:</h2>
<ul>
<% phrases_term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :term_id %> :
<%= form.number_field :id %>
</div>
<div class="dropdown">
<button class="dropbtn">Phrase</button>
<div class="dropdown-content">
<a><%= form.select :phrase_id, Phrase.all.collect { |p| [ p.id ] }, include_blank: true %></a>
</div>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Term:
_form.html.erb
<%= form_with(model: term, local: true) do |form| %>
<% if term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(term.errors.count, "error") %> prohibited this term from being saved:</h2>
<ul>
<% term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :word %>
<%= form.text_field :word %>
</div>
<div class="field">
<%= form.label :meaning %>
<%= form.text_field :meaning %>
</div>
<div class="field">
<%= form.label :reading %>
<%= form.text_field :reading %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
I am not sure I have provided all the necessary information, if I want to add some more information, please comment below. ---Thank You.----
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.
Having a hard time with this because of the numerous objects that get passed around... how would you write a partial or whatever to DRY this up?
inventory/new.html.erb
<% Categorylist.includes(:itemlists).where(itemlists: { inventory_list: true }).each do |n| %>
<div class="col-xs-4">
<div class="form-group" style="font-size:75%">
<h5> <%= n.name %> </h5>
<% n.itemlists.each do |i| %>
<%= number_field_tag "inventory[#{i.id}]", :quantity, min: 0, value: #inventory_params ? #inventory_params[i] : 0, class: "shrink_num_field" %>
<%= label_tag i.name, i.name %>
</br>
<% end %>
</div>
</div>
<% end %>
request/new.html.erb
<% Categorylist.includes(:itemlists).where(itemlists: { request_list: true }).each do |n| %>
<div class="col-xs-4">
<div class="form-group" style="font-size:75%">
<h5> <%= n.name %> </h5>
<% n.itemlists.each do |i| %>
<%= number_field_tag "borrow[][#{i.id}]", :quantity, min: 0, value: #borrowparams ? #borrowparams[i] : 0, class: "shrink_num_field" %>
<%= label_tag i.name, i.name %>
</br>
<% end %>
</div>
</div>
<% end %>
Specifically, I'm having a hard time getting the partial to differentiate between inventory_list vs. request_list in the first line.
On localhost, after launching rails server. Trying to build a simple navbar with options for social sign-ins. When I try to add "Sign in with Google" and "Sign in with Facebook" I get the following error:
"SyntaxError in PagesController#home
/Users/okazemi/Desktop/testapp/app/views/layouts/_header.html.erb:40: syntax error, unexpected keyword_ensure, expecting end-of-input"
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
With the "<%= render 'layouts/header' %>" being highlighted in red.
Here is the application.html.rb code:
<!DOCTYPE html>
<html>
<head>
<title>TestApp</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
<%= yield %>
</div>
</body>
</html>
The code for the _header.html.erb that is referenced in the render is as follows:
<nav class="navbar navbar-default" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<%= link_to "testapp", root_path, class: "navbar-brand" %>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %><li>
<li><%= link_to "Recruiter", recruiter_path %><li>
<li><%= link_to "Employer", employer_path %><li>
<li><%= link_to "Postings", postings_path %><li>
<li><%= link_to "testing", testing_path %><li>
<li><%= link_to "Learn", learn_path %><li>
<% if user_signed_in? %> Signed in as <%= current_user.name %>. Not you?
<%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or
<%= link_to "Sign in", new_user_session_path %>
<%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %>
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
<% end %>
<% end %>
</ul>
</div><!-- /.navbar-collapse -->
Any help would be greatly suggested. Have spent hours trying to figure out if I'm missing an end somewhere but nothing seems to be sticking out. Thanks!
I see a couple of problems in the _header.html.erb, the most important one out of those is that you have an extra <% end %> in the code which is causing the syntax error. I have marked it with <!-- Remove this extra end --> comment in the below code.
<nav class="navbar navbar-default" role="navigation">
<!-- ....... -->
<% if user_signed_in? %> Signed in as <%= current_user.name %>. Not you?
<%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or
<%= link_to "Sign in", new_user_session_path %>
<%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %>
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
<% end %>
<% end %> <!-- Remove this extra end -->
</ul>
</div><!-- /.navbar-collapse -->
A few suggestions:
<nav class="navbar navbar-default" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<%= link_to "testapp", root_path, class: "navbar-brand" %>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<!-- Close list item tags with </li> and NOT <li> -->
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Recruiter", recruiter_path %></li>
<li><%= link_to "Employer", employer_path %></li>
<li><%= link_to "Postings", postings_path %></li>
<li><%= link_to "testing", testing_path %></li>
<li><%= link_to "Learn", learn_path %></li>
</ul>
<!-- Below code is not used as list items so remove it from the unordered list element (<ul>)-->
<% if user_signed_in? %>
Signed in as
<%= current_user.name %>. Not you?
<%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %>
or
<%= link_to "Sign in", new_user_session_path %>
<%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %>
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
<% end %>
</div><!-- /.navbar-collapse -->
</div> <!-- Add this closing tag -->
</nav> <!-- Add this closing tag -->
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