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" %>
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.----
this is the site: https://deadindustry-newnew-recruit.herokuapp.com/
here is the github: daveasdf/deadindustry_newnew_recruit
when you enter the form, it's supposed to render the form, and the list of supporters on the right, but instead it spits out the text version of them at https://deadindustry-newnew-recruit.herokuapp.com/supporters.js, which is some random webpage.
i'm no developer, i'm just a musician, so i appreciate any and all help. also, please check out my band: soundcloud.com/whorwood
here's the specifics:
page:
<div class="row">
<div class="col-md-12">
TEXT
</div>
</div>
<br>
<div class="row">
<div class="col-md-4">
<div id="home_form">
<%= render "supporters/form" %>
</div>
</div>
<div class="col-md-2">
</div>
<div class="col-md-6" id="home_list">
<br>
<%= render 'supporters/show_supporters' %>
</div>
</div>
controller:
class SupportersController < ApplicationController
...
def create
#supporter = Supporter.new(supporter_params)
#supporters = Supporter.all.order(created_at: :desc)
respond_to do |format|
format.js
end
end
...
create.js
<% if #supporter.save %>
$("#home_list").html("<%= j render 'show_supporters' %>")
$("home_form").html("<%= j render 'form' %>")
<% else %>
$("#home_form").html("<%= j render 'form', supporter: #supporter %>")
$('#build_error').remove();
<% if #supporter.errors.any? %>
var build_error = "<div class='alert alert-danger' id='build_error'>";
build_error += "You've got <%= pluralize(#supporter.errors.count, 'error') %>.";
build_error += "<ul id='error_explanation'>";
<% #supporter.errors.full_messages.each do |msg| %>
<% if msg === "Ip has already been taken" %>
<% msg = "You've already posted your support. Thanks!" %>
<% end %>
build_error += "<li><%= j msg %></li>";
<% end %>
build_error += "</ul>";
build_error += "</div>";
$(build_error).insertBefore( "#home_form" );
<% end %>
<% end %>
You pass a wrong action into the form
<form novalidate="novalidate" class="simple_form new_supporter" id="new_supporter"
action="/supporters.js" accept-charset="UTF-8" data-remote="true" method="post">
Instead of /supporter.js should be just /supporters
You need to remove format: :js from this line:
<%= simple_form_for(#supporter, remote: true, format: :js) do |f| %>
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 %>
')
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>
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