i'm making a Rails app that requires the use of a search form.
In my app, a user can create a post, 'Ad', and then specify if he wants it to be a small, medium or featured sized post.
This was working fine until I attempted to incorporate Sunspot Solr Search into my app.
No when ever I create a post, be it small, medium or featured, it creates on of every size whenever it should only create the selected one.
Ad Controller
class AdsController < ApplicationController
before_action :set_ad, only: [:show, :edit, :update, :destroy]
# GET /ads
# GET /ads.json
def index
#search_medium = Ad.where(:size => "medium").search do
fulltext params[:search]
end
#search_featured = Ad.where(:size => "featured").search do
fulltext params[:search]
end
#search_small = Ad.where(:size => "small").search do
fulltext params[:search]
end
#ads = Ad.all
#ads_small = #search_small.results
#ads_medium = #search_medium.results
#ads_featured = #search_featured.results
end
# GET /ads/1
# GET /ads/1.json
def show
end
# GET /ads/new
def new
#ad = Ad.new
end
# GET /ads/1/edit
def edit
end
# POST /ads
# POST /ads.json
def create
#ad = Ad.new(ad_params)
#ad.user_id = current_user.id
respond_to do |format|
if #ad.save
format.html { redirect_to #ad, notice: 'Ad was successfully created.' }
format.json { render action: 'show', status: :created, location: #ad }
else
format.html { render action: 'new' }
format.json { render json: #ad.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /ads/1
# PATCH/PUT /ads/1.json
def update
respond_to do |format|
if #ad.update(ad_params)
format.html { redirect_to #ad, notice: 'Ad was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #ad.errors, status: :unprocessable_entity }
end
end
end
# DELETE /ads/1
# DELETE /ads/1.json
def destroy
#ad.destroy
respond_to do |format|
format.html { redirect_to ads_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_ad
#ad = Ad.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def ad_params
params.require(:ad).permit(:title, :url, :preview, :location, :size, :info)
end
end
My Index View
<style type="text/css">
.ads_link {
font-family: NexaBold, Helvetica, Arial, sans-serif;
color: #e74c3c!important;
}
</style>
<%= form_tag ads_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<% #ads_featured.each do |ad| %>
<div class="adspace_grid_f">
<div class="pic_sec_f">
<%= image_tag ad.preview.url(:featured) %>
</div>
<div class="text_info_f">
<span class="bold"><%= link_to ad.title, ad, :class => "title" %></span></br>
<p><%= truncate(ad.info, length: 90) %></p>
<span class="bold"><%= ad.location %></span></br>
<span class="bold"><%= ad.url %></span>
</div>
<% if can? :destroy, ad %>
<%= link_to 'Delete', ad, :method => :delete, :class => "delete" %>
<% end %>
</div>
<% end %>
<% #ads_medium.each do |ad| %>
<div class="adspace_grid">
<div class="pic_sec">
<%= image_tag ad.preview.url(:medium) %>
</div>
<div class="text_info">
<span class="bold"><%= link_to ad.title, ad, :class => "title" %></span></br>
<p><%= truncate(ad.info, length: 60) %></p>
<span class="bold"><%= ad.location %></span></br>
<span class="bold"><%= ad.url %></span>
</div>
<% if can? :destroy, ad %>
<%= link_to 'Delete', ad, :method => :delete, :class => "delete" %>
<% end %>
</div>
<% end %>
<% #ads_small.each do |ad| %>
<div class="adspace_grid">
<div class="pic_sec">
<%= image_tag ad.preview.url(:medium) %>
</div>
<div class="text_info">
<span class="bold"><%= ad.title %></span></br>
<p><%= truncate(ad.info, length: 30) %></p>
<div class="right-text">
<span class="bold"><%= ad.location %></span></br>
<span class="bold"><%= ad.url %></span>
</div>
</div>
<% if can? :destroy, ad %>
<%= link_to 'Delete', ad, :method => :delete %>
<% end %>
</div>
<% end %>
My Model File
class Ad < ActiveRecord::Base
attr_accessible :title, :url, :preview, :size, :location, :info
belongs_to :user
has_attached_file :preview, :default_url => "missing.jpg", :styles => { :medium => "125x125^", :featured => "250x250^", :showpg => "400x400^" }, :convert_options => {:medium => "-gravity center -extent 125x125", :featured => "-gravity center -extent 250x250", :showpg => "-gravity center -extent 400x400"}
validates :title, length: { maximum: 35 }
validates :url, length: { maximum: 40 }
searchable do
text :title, :info
end
end
the local sunspot server is running on the port 8983 by default, and it provide a web admin there. You can do a search by entering the query as ":", and this will return all the indexed record inside sunspot.
I suggest you add the size field into the searchable definition and do the search without the where method call.
Related
Good Day,
Excuse me for the noob question.
In my rails app I have /YYYY/MM/DD/Title-Slug URL structure with Friendly_Id .
news_controller.rb
class NewsController < ApplicationController
before_action :set_news, only: [:show, :edit, :update]
before_action :authenticate_user!, except: [:show, :index, :search ]
def index
if params[:search]
#news = News.search(params[:search]).paginate(:page => params[:page], :per_page => 12).order("created_at DESC")
else
#news = News.where( state: true ).paginate(:page => params[:page], :per_page => 12).order('id DESC')
end
end
def latest_news
if current_user.admin?
#news = News.paginate(:page => params[:page], :per_page => 12).order('id DESC')
else
flash[:error] = "you are not authorised to visit this section"
redirect_to root_path
end
end
def new
if current_user.state == true
#news = News.new
else
flash[:error] = "your account has not been activated"
redirect_to root_path
end
end
def show
#news_photos = #news.news_photos
end
def create
#news = News.new(news_params)
respond_to do |format|
if #news.save
if params[:images]
params[:images].each do |image|
#news.news_photos.create(image: image)
end
end
#news_photos = #news.news_photos
format.html { redirect_to show_news_path(#news.year,#news.month,#news.date,#news ), notice: 'News was successfully created.' }
format.json { render :show, status: :created, location: #news }
else
render :new
end
end
end
def edit
#news_photos = #news.news_photos
end
def update
respond_to do |format|
if current_user.admin?
if #news.update(admin_news_params)
if params[:images]
params[:images].each do |image|
#news.news_photos.create(image: image)
end
end
#redirect_to root_path, notice: "Updated..."
# redirect_to latest_news_path, notice: "Updated..."
format.html { redirect_to show_news_path(#news.year,#news.month,#news.date,#news ), notice: 'News was successfully edited' }
# format.json { render :show, status: :created, location: #news }
else
render :edit
end
else
if #news.update(news_params)
if params[:images]
params[:images].each do |image|
#news.news_photos.create(image: image)
end
end
# redirect_to news_index_path, notice: "Updated..."
#redirect_to latest_news_path, notice: "Updated..."
format.html { redirect_to show_news_path(#news.year,#news.month,#news.date,#news ), notice: 'News was successfully edited.' }
# format.json { render :show, status: :created, location: #news }
else
render :edit
end
end
end
end
def destroy
#news = News.find(params[:id])
#news.destroy
respond_to do |format|
format.html { redirect_to news_index_path }
# format.json { head :no_content }
end
end
private
def set_news
#news = News.find(params[:id])
end
def news_params
params.require(:news).permit( :title, :description, :category, :keywords, :user_id, :email, :user_name)
end
def admin_news_params
params.require(:news).permit( :title, :description, :category, :keywords, :user_id, :email, :user_name, :state)
end
end
news_helper.rb
module NewsHelper
def show_news_path(news)
"/news/#{news.year}/#{news.month}/#{news.date}/#{news.slug}"
end
end
news.rb
class News < ApplicationRecord
extend FriendlyId
include PgSearch
has_many :news_photos
friendly_id :slug_candidates, use: [:slugged, :finders, :history]
def slug_candidates
[ :title,
[:title, :id]
]
end
def year
created_at.localtime.strftime("%Y")
end
def month
created_at.localtime.strftime("%m")
end
def date
created_at.localtime.strftime("%d")
end
end
routes.rb
Rails.application.routes.draw do
resources :news, :except => [:latest_news, :search, :show, :edit, :update]
root :to => "news#index"
scope 'news' do
get '/:year/:month/:date/:id', to: 'news#show', as: 'show_news'
get '/:year/:month/:date/:id/edit', to: 'news#edit', as: 'edit_news'
patch '/:year/:month/:date/:id', to: 'news#update'
put '/:year/:month/:date/:id', to: 'news#update'
delete '/:year/:month/:date/:id', to: 'news#destroy'
end
end
_form.html.erb
<div class="row">
<div class="col-md-8 col-md-offset-2">
<%= form_for #news, :html => { :class => 'form-horizontal', multipart: true } do |f| %>
<div class="row">
<div class="col-md-12">
<%= f.label :title, :class => 'control-label' %>
<div class="form-group">
<%= f.text_field :title, :class => 'form-control' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.label :keywords, :class => 'control-label' %>
<div class="form-group">
<%= f.text_field :keywords, :class => 'form-control' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.label :description, :class => 'control-label' %>
<div class="form-group">
<%= f.text_area :description, :class => 'form-control', id: "input" %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<label>Upload Image</label>
<div class="form-group">
<span class="btn btn-success btn-file">
<i class="fa fa-cloud-upload fa-lg"></i> Upload Photos
<%= file_field_tag "images[]", type: :file, multiple: true %>
</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= f.submit nil, :class => 'btn btn-info' %>
</div>
</div>
<% end %>
</div>
</div>
here's what works:
news/index
/news/2017/4/27/example
Creating a new post
Destroying a post
What doesn't work:
Editing a post (the edit page with the form will render, but after submitting my changes i am getting the aforementioned error - and the changes never make it to the DB)
error:
ActionController::RoutingError (No route matches [PATCH] "/news/example"):
I have fallowed the below post to implement the date based slugs:
/YYYY/MM/Title-Slug URL structure with Friendly_Id Solution Chokes on #edit
I DONT UNDERSTAND WHAT I HAVE GONE WRONG IN news_helper.rb
Any help is Highly Appreciated.Thanks in Advance
try below code:
Rails.application.routes.draw do
resources :news, :except => [:latest_news, :search, :show, :edit]
remove :update from except
It's because you've disabled the update action in your routes
resources :news, :except => [:latest_news, :search, :show, :edit, :update]
The solution is to allow the update by removing it from the except clause like this
resources :news, except: [:latest_news, :search, :show, :edit]
The user will never see this url unless they "view-source" it so it doesn't matter for usability sake.
I have a weird problem with "nested_form" in Rails. I made a model "evaluate" associated to other model "proyect", but when I try to show theres fields, on "proyects" form, just show fields from "proyects".
Here is my code:
Models:
proyect.erb
class Proyect < ActiveRecord::Base
belongs_to :user
has_many :vercions #I know is versions
has_many :evaluates #I know is evaluators
accepts_nested_attributes_for :evaluates, allow_destroy: true
validates :titulo,:presence => true,
:length => { :minimum => 3 }
validates :descripcion,:presence => true,
:length => { :minimum => 3 }
end
evaluate.erb
class Evaluate < ActiveRecord::Base
belongs_to :proyect
has_and_belongs_to_many :users
end
Controller
proyects_controller.erb
class ProyectsController < ApplicationController
before_action :set_proyect, only: [:show, :edit, :update, :destroy]
# GET /proyects
# GET /proyects.json
def index
if current_user.tipo == 'i'
#proyects = Proyect.where(:user_id => current_user.id)
else
#proyects = #Proyect.where(:id_user => current_user.id)
Proyect.all
end
end
# GET /proyects/1
# GET /proyects/1.json
def show
#vercion = Vercion.new
end
# GET /proyects/new
def new
#proyect = Proyect.new
#proyect.evaluates.build
end
# GET /proyects/1/edit
def edit
end
# POST /proyects
# POST /proyects.json
def create
#proyect = current_user.proyects.new(proyect_params)
respond_to do |format|
if #proyect.save
format.html { redirect_to #proyect, notice: 'Proyecto creado!.' }
format.json { render :show, status: :created, location: #proyect }
else
format.html { render :new }
format.json { render json: #proyect.errors, status: :unprocessable_entity }
end
# Llamamos al ActionMailer que creamos
Usermailer.bienvenido_email(current_user,#proyect).deliver
end
end
# PATCH/PUT /proyects/1
# PATCH/PUT /proyects/1.json
def update
respond_to do |format|
if #proyect.update(proyect_params)
format.html { redirect_to #proyect, notice: 'Proyect was successfully updated.' }
format.json { render :show, status: :ok, location: #proyect }
else
format.html { render :edit }
format.json { render json: #proyect.errors, status: :unprocessable_entity }
end
end
end
# DELETE /proyects/1
# DELETE /proyects/1.json
def destroy
#proyect.destroy
respond_to do |format|
format.html { redirect_to proyects_url, notice: 'Proyect was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_proyect
#proyect = Proyect.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def proyect_params
params.require(:proyect).permit(
:titulo, :descripcion,:evaluador, :id_user, :codigo, :user_assign,evaluates_attributes: [:id,:nombre, :prioridad, :_destroy, user_ids: [] ])
end
end
Views
_form.html.erb (Proyects)
<%= nested_form_for(#proyect) do |f| %>
<% if #proyect.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#proyect.errors.count, "error") %> prohibited this proyect from being saved:</h2>
<ul>
<% #proyect.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :titulo %><br>
<%= f.text_field :titulo %>
</div>
<div class="field">
<%= f.label :descripcion %><br>
<%= f.text_area :descripcion %>
</div>
<div class="field">
<%= f.hidden_field :id_user, :value => current_user.id %>
</div>
<!--Aqui aƱadi algo-->
<fieldset id="evaluates">
<%= f.fields_for :evaluates do |evaluates_form| %>
<div class="field">
<%= evaluates_form.label :status %><br>
<%= evaluates_form.text_field :status %>
</div>
<%= evaluates_form.link_to_remove "Eliminar esta tarea" %>
<% end %>
<p><%= f.link_to_add "Agregar una tarea", :evaluates %></p>
</fieldset>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
_evaluate_fields.html.erb
<div class="field">
<%= f.label :status, 'Nombre de la tarea' %><br>
<%= f.text_field :status %>
</div>
<div class="field">
<%= f.collection_check_boxes :user_ids, User.where(:tipo => 'e'), :id, :cedula %>
</div>
<%= f.link_to_remove "Eliminar Evaluador" %>
I am having issues understanding this.
What am I trying to accomplish?
Selected items in dropdown submitted to inventory.
Change the status of each item in a form in its own model.
Problem
I submit a form and the status of each item in the dropdown doesn't get submitted.
Any help would be appreciated.
Thanks in advance!
Form Code
<%= form_for(#inventory, html: {class: "form-horizontal"}) do |f| %>
<% if #inventory.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#inventory.errors.count, "error") %> prohibited this inventory from being saved:</h2>
<ul>
<% #inventory.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :tablet_id, "Tablet #", class: "col-md-2 control-label" %>
<div class="col-md-4">
<%= select("inventory", "tablet_id", Tablet.where("status = '1'").all.collect{ |t| [t.alias, t.id] }, {}, { class: "form-control"} ) %>
</div>
</div>
<%= f.fields_for :tablet do |t| %>
<%= t.hidden_field :status, value: 2 %>
<% end %>
<div class="form-group">
<%= f.label :chauffeur_id, "Chauffeur #", class: "col-md-2 control-label" %>
<div class="col-md-4">
<%= select("inventory", "chauffeur_id", Chauffeur.where("status = '1'").all.collect{ |t| [t.chaufNum, t.id] }, {}, { class: "form-control"} ) %>
</div>
</div>
<%= f.fields_for :chauffeur do |t| %>
<%= t.hidden_field :status, value: 2 %>
<% end %>
<div class="form-group">
<%= f.label :vehicle_id, "Vehicle #", class: "col-md-2 control-label" %>
<div class="col-md-4">
<%= select("inventory", "vehicle_id", Vehicle.where("status = '1'").all.collect{ |t| [t.vehNum, t.id] }, {}, { class: "form-control"} ) %>
</div>
</div>
<%= f.fields_for :vehicle do |t| %>
<%= t.hidden_field :status, value: 2 %>
<% end %>
<div class="form-group">
<div class="col-md-2 col-md-offset-2">
<%= f.submit class: "btn btn-sm btn-success" %>
</div>
</div>
<% end %>
Model (Inventory)
class Inventory < ActiveRecord::Base
belongs_to :tablet
belongs_to :chauffeur
belongs_to :vehicle
accepts_nested_attributes_for :tablet
accepts_nested_attributes_for :chauffeur
accepts_nested_attributes_for :vehicle
has_paper_trail
validates :tablet_id, :chauffeur_id, :vehicle_id, presence: true
validates :tablet_id, :chauffeur_id, :vehicle_id, uniqueness: { message: " is already checked out." }
end
Inventory Controller
class InventoriesController < ApplicationController
before_action :authenticate_user!
before_filter :set_paper_trail_whodunnit
before_action :set_inventory, only: [:show, :edit, :update, :destroy]
# GET /inventories
# GET /inventories.json
def index
#inventories = Inventory.all.paginate(:page => params[:page], :per_page => 15)
end
# GET /inventories/1
# GET /inventories/1.json
def show
#versions = PaperTrail::Version.order('created_at DESC')
end
# GET /inventories/new
def new
#inventory = Inventory.new
end
# GET /inventories/1/edit
def edit
end
def history
#versions = PaperTrail::Version.order('created_at DESC')
end
# POST /inventories
# POST /inventories.json
def create
# #inventory = Inventory.new(inventory_params)
render plain: params[:inventory].inspect
# respond_to do |format|
# if #inventory.save
# format.html { redirect_to #inventory, notice: 'Inventory was successfully created.' }
# format.json { render :show, status: :created, location: #inventory }
# else
# format.html { render :new }
# format.json { render json: #inventory.errors, status: :unprocessable_entity }
# end
# end
end
# PATCH/PUT /inventories/1
# PATCH/PUT /inventories/1.json
def update
respond_to do |format|
if #inventory.update(inventory_params)
format.html { redirect_to #inventory, notice: 'Inventory was successfully updated.' }
format.json { render :show, status: :ok, location: #inventory }
else
format.html { render :edit }
format.json { render json: #inventory.errors, status: :unprocessable_entity }
end
end
end
# DELETE /inventories/1
# DELETE /inventories/1.json
def destroy
#inventory.destroy
respond_to do |format|
format.html { redirect_to inventories_url, notice: 'Inventory was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_inventory
#inventory = Inventory.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def inventory_params
params.require(:inventory).permit(:tablet_id, :chauffeur_id, :vehicle_id, tablet_attributes: [:status => '2'], chauffeur_attributes: [:status => '2'], vehicle_attributes: [:status => '2'])
end
end
Clean all your nested attribute code. This is not what you want. So model like this:
class Inventory < ActiveRecord::Base
belongs_to :tablet
belongs_to :chauffeur
belongs_to :vehicle
has_paper_trail
validates :tablet_id, :chauffeur_id, :vehicle_id, presence: true
validates :tablet_id, :chauffeur_id, :vehicle_id, uniqueness: { message: " is already checked out." }
end
Now, what you want to use a rails callback so that when you create an inventory then you update the status of other things. This goes on your model too:
class Inventory < ActiveRecord::Base
...
after_create :update_status
protected
def update_status
self.tablet.update_attribute(:status, 2)
self.chauffeur.update_attribute(:status, 2)
self.vehicle.update_attribute(:status, 2)
end
end
Also remember to clean all your fields_for code and your strong parameters on your controller...you don't need the nested ones anymore.
So, I have a rails app that allows users to submit youtube links and rank the songs/links using the thumbs up gem.
However, users must physically copy and paste the link into the browser to listen to the song after the link is uploaded. For a better UX I'm going to use this gem to dynamically embed the YouTube video into my app once the users upload the link.
However, I'm not quite sure how this can be done?
I was thinkin' of doing something like this in the index:
<%= YouTubeAddy.extract_video_id('song.url)' %> <%= YouTubeAddy.youtube_embed_url('song.url') %> ?
or would it have to be in the controller? and if so how?
song#new.html.erb:
<%= form_for #song, :html => { :multipart => true } do |f| %>
<% if #song.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#song.errors.count, "error") %> prohibited this song from being saved:</h2>
<ul>
<% #song.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row">
<div class="large-6 columns">
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label 'website url' %>
<%= f.text_area :url %>
</div>
<div class="field">
<%= f.label :tag_list, "Genres (separated by commas)" %><br />
<%= f.text_field :tag_list %>
</div>
<p>
<%= f.file_field :track%>
</p>
<div class="actions">
<%= f.submit value: "Upload" %>
</div>
<% end %>
</div>
song_controller.rb
class SongsController < ApplicationController
before_filter :authenticate_user!, only: [:create ,:edit, :update, :destroy, :vote_for_song]
before_action :set_song, only: [:show, :edit, :update, :destroy, :vote_for_song]
def vote_for
#song = Song.find(params[:id])
current_user.vote_for(#song)
#song.plusminus = #song.votes_for
#song.save
respond_to do |format|
format.js { render 'update_votes' }
end
end
def vote_against
#song = Song.find(params[:id])
current_user.vote_against(#song)
respond_to do |format|
format.js { render 'update_votes' }
end
end
def new_songs
#songs = Song.order "id DESC"
end
# GET /Songs
# GET /Songs.json
def index
if params[:genre]
#songs = Song.tagged_with(params[:genre]).paginate(:page => params[:page], :per_page => 15)
else
#songs = Song.order('plusminus').paginate(:page => params[:page], :per_page => 15)
end
end
# GET /Songs/1
# GET /Songs/1.json
def show
#comment = Comment.new(song: #song)
end
# GET /Songs/new
def new
#song = Song.new
end
# GET /Songs/1/edit
def edit
end
# POST /Songs
# POST /Songs.json
def create
#song = Song.new(song_params)
respond_to do |format|
if #song.save
format.html { redirect_to #song, notice: 'Song was successfully created.' }
format.json { render action: 'show', status: :created, location: #song }
else
format.html { render action: 'new' }
format.json { render json: #song.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /Songs/1
# PATCH/PUT /Songs/1.json
def update
respond_to do |format|
if #song.update(song_params)
format.html { redirect_to #song, notice: 'Song was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #song.errors, status: :unprocessable_entity }
end
end
end
# Song /Songs/1
# Song /Songs/1.json
def destroy
#song.destroy
respond_to do |format|
format.html { redirect_to songs_url }
format.json { head :no_content }
end
end
private
def set_song
#song = Song.find(params[:id])
end
def song_params
params.require(:song).permit(:title, :artist, :url, :track, :user_id, :tag_list)
end
end
index.html.erb
<h6>Top ranked songs</h6>
<hr>
<ol><% #songs.each do |song| %>
<span class="title">
<li><%=link_to image_tag('thumbs.png', size: '16x50'), vote_for_song_path(song), :remote => true, :method => :put %> <%= link_to song.title, song %><span class="subtext"> (<%= song.url %>)<br></li></span>
<%#=link_to '▼'.html_safe, vote_against_song_path(song), :remote => true, :method => :put %>
<span class="subtext"><span class="votes_<%= song.id %>"><%= pluralize(song.votes.count, 'like') %>,</span>
posted <%= time_ago_in_words(song.created_at) + " ago" %>
<small><span class="comments"></small> | <%= pluralize(song.comments.size, 'comment') %></span></small><br /></span>
<%#= link_to 'Show', song, class: "button small secondary" %>
<%= link_to('Edit', edit_song_path(song), class: "button small secondary") if can? :update, song %>
<%= link_to('Destroy', song, method: :delete, data: {confirm: 'Are you sure?'}, class: "button small secondary") if can? :destroy, song %>
<% end %>
</ol>
<div class="pagination-centered">
<ul class="pagination">
<%#= will_paginate #songs %>
<!-- or custom pagination -->
<% if #songs.previous_page %>
<%= link_to "Back", params.merge(page: #songs.previous_page) %>
<% end %>
<% if #songs.next_page %>
<%= link_to "More", params.merge(page: #songs.next_page) %>
<% end %>
</ul></div>
YouTubeAddy.youtube_embed_url('song.url') generates a string of iframe tag, so it should be used in your view exactly where you want an embedded video to be placed. But note that you should return an html safe string. So in your view you have:
<%=raw YouTubeAddy.youtube_embed_url('song.url') %>
More about unescaping html.
Not really any error messages, just no file uploaded!
Please help!!!!
everything get created and is fine apart from the file upload.
Models:
class Assets < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
delegate :url, :to => :attachment
attr_accessible :asset, :asset_file_name
end
class Video < ActiveRecord::Base
attr_accessible :body, :title, :url, :covers, :assets
has_many :covers, :as => :assetable, :class_name => "Video::Cover", :dependent => :destroy
accepts_nested_attributes_for :covers, :allow_destroy => true
class Video::Cover < Assets
has_attached_file :attachment, :styles => { :small => "300x0>", :large => "800x0>" }
end
end
video controller:
class VideosController < ApplicationController
#load_and_authorize_resource
# GET /videos
# GET /videos.xml
def index
#videos = Video.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #videos }
end
end
# GET /videos/1
# GET /videos/1.xml
def show
#video = Video.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #video }
end
end
# GET videos/new
# GET /videos/new.xml
def new
#video = Video.new
1.times do #video.covers.build end
#40.times do #video.images.build end
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #video }
end
end
# GET /collections/1/edit
def edit
#video = Video.find(params[:id])
1.times do #video.covers.build end
#1.times do #video.images.build end
end
# POST /videos
# POST /videos.xml
def create
#video = Video.new(params[:video])
respond_to do |format|
if #video.save
format.html { redirect_to(#video, :notice => 'Video was successfully created.') }
format.xml { render :xml => #video, :status => :created, :location => #video }
else
format.html { render :action => "new" }
format.xml { render :xml => #video.errors, :status => :unprocessable_entity }
end
end
end
# PUT /videos/1
# PUT /videos/1.xml
def update
#video = Video.find(params[:id])
respond_to do |format|
if #video.update_attributes(params[:video])
format.html { redirect_to(#video, :notice => 'Video was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #video.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /videos/1
# DELETE /videos/1.xml
def destroy
#video = Video.find(params[:id])
#video.destroy
respond_to do |format|
format.html { redirect_to(videos_url) }
format.xml { head :ok }
end
end
end
Views:
form
<div id="form_bk">
<div id="field_left">
<%= form_for #video, :html => {:multipart => true} do |f| %>
<% if #video.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#video.errors.count, "error") %> prohibited this video from being saved:</h2>
<ul>
<% #video.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="cover">
<br />
<h2>Cover Image</h2>
<%= f.fields_for :covers do |cover_image| %>
<% if cover_image.object.new_record? %>
<p><%= cover_image.file_field :attachment %></p>
<% end %>
<% end %>
<%= f.fields_for :covers do |cover_image| %>
<% unless cover_image.object.new_record? %>
<br />
<p>
<%=image_tag cover_image.object.url(:small) %><br />
<br /> Delete cover <%= cover_image.check_box :_destroy %><br /><br />
</p>
<% end %>
<% end %>
</div>
<div id="form_right">
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div></div>
<div class="field">
<%= f.label :url %><br />
<%= f.text_area :url %>
</div></div>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<br />
</div>
Index view
<% #videos.each do |video| %>
<div class="video">
<%- video.covers.each do |cover| %>
<div class="video_cover"><%=link_to image_tag (cover.url(:small)), video %></div>
<% end %>
<div class="video_title">
<%=link_to video.title, video %>
<%= video.body %>
<%= link_to 'Show', video %>
<%= link_to 'Edit', edit_video_path(video) %>
<%= link_to 'Destroy', video, :confirm => 'Are you sure?', :method => :delete %>
</div>
</div>
<% end %>
<br /><br /><br />
<%= link_to 'New Video', new_video_path %>
<%= debug #videos %>