How to test this (with or without RSpec)?
def create
#foo = Model1.find(params[:array_of_ids])
#foo.each do |f|
#boo = Model2.new
#boo.attributes = f.attributes
#boo.save! unless Model2.exists?(#boo.id)
end
end
Related
this is the code that i have:
list_views = ListView.where(:user_id => current_user.id)
total_list_views = list_views.size
top_recommended_lists = recommendations_based_on_list_views(list_views, language_score_hash, level_score_hash, category_score_hash)
and then there is this method
def recommendations_based_on_list_views(list_views, language_score_hash, level_score_hash, category_score_hash)
viewed_lists_ids = list_views.map {|list_view| list_view.list_id}.uniq
....
problem is with this code 2 queries are executed on ListView model
one at list_views.size and then at list_views.map {|list_view| list_view.list_id}.uniq
if we can force query execution at ListView.where(:user_id => current_user.id) then only single query to ListView model will be needed OR if any other way
how to achieve this ??
Found the way to this. We can use load:
list_views = ListView.where(:user_id => current_user.id).load
https://apidock.com/rails/ActiveRecord/Relation/load
I want to combine similar methods and views into one, but still keep the url name, like the following:
Home/recommends/categories/shopping
Home/recommends/categories/nightview
Home/recommends/categories/food
Home/recommends/categories/area
I don't want to use params like "?something=xyz" in url.
In routes.rb:
resources :recommends, only: :index do
collection do
resources :categories, only: :show, controller: 'recommends' do
collection do
get :food
get :area
get :shopping
get :nightview
end
end
end
end
In controllers:
def food
set_paginator
#recommends = UserRecommend.where(category: "food").order('created_at desc').offset(#offset).limit(#limit).all
#number_of_recommends = UserRecommend.where(category: "food").count
end
def area
set_paginator
#recommends = UserRecommend.where(category: "area").order('created_at desc').offset(#offset).limit(#limit).all
#number_of_recommends = UserRecommend.where(category: "area").count
end
...
In views I have:
food.html.slim
area.html.slim
shopping.slim
nightview.slim
Which are using the same code, just different names in h1:
h1
| Shopping ( or Area or Food... )
= " (#{#number_of_recommends})"
= render partial: "layouts/paginator",
locals: { total_items: #number_of_recommends, per_page: #limit, current_page: #page }
= render partial: "table", locals: { recommends: #recommends }
Can anyone help me refactor this code?
You can (and should) have a single route, a single action, and a single view. The key is to make the variable portion of your URL into an actual variable. You do this using dynamic segments.
First, a single route. There is no need to use resources if you're not actually generating multiple RESTful actions:
get "/recommends/categories/:category" => "categories#show"
You can add criteria on what is allowed for the :category segment:
get "/recommends/categories/:category" => "categories#show", category: /food|area|shopping|nightview/
Next, a single action:
class CategoriesController < ApplicationController
before_action :set_paginator
def show
# params[:category] is "food"/"area"/etc
categories = UserRecommend.where(category: params[:category]).order('created_at desc')
#recommends = categories.offset(#offset).limit(#limit)
#number_of_recommends = categories.count
end
end
Finally, a single view:
# app/views/categories/show.slim
h1
= params[:category].capitalize
= " (#{#number_of_recommends})"
= render partial: "layouts/paginator",
locals: { total_items: #number_of_recommends, per_page: #limit, current_page: #page }
= render partial: "table", locals: { recommends: #recommends }
I would consider it better to use localization to turn the params[:category] into a title, which would give you more control, rather than relying on simple capitalization of the URL segment:
# app/views/categories/show.slim
h1
= t params[:category]
And:
# config/locals/en.yml
en:
categories:
show:
food: 'Food'
area: 'Area'
nightview: 'Night View'
I'm trying to save pdf on server using rails model buts its generate a blank pdf. Earlier did it in controller without problem but now its creating a blank one. Any idea What's i did wrong?
def generate_bulk_pdf
view = ActionView::Base.new(ActionController::Base.view_paths, {})
view.extend(ApplicationHelper)
view.extend(AbstractController::Rendering)
view.extend(Rails.application.routes.url_helpers)
students = Student.all.order('id ASC')
students.each do | aStudent |
pdf = WickedPdf.new.pdf_from_string(
view.render_to_string(
:template => "#{Rails.root.join('templates/challen.pdf.erb')}",
:locals => { '#student' => aStudent }
)
)
save_path = Rails.root.join('pdfs','filename.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end
end
end
Any idea What's i did wrong? I can't find any solution
A good test is just put a simple line of text in your template and see if you get a PDF with that line. Strip everything back so you just generating a PDF with no coming locals, just that 1 string and let me know.
Here is how I set up mine and it works fine, it might click something :)
def generate_pdf_voucher(voucher, dir_name)
view = ActionView::Base.new(Rails.root.join('app/views'))
view.class.include ApplicationHelper
view.class.include Rails.application.routes.url_helpers
pdf = view.render :pdf => a_name,
:template => 'layouts/pdfs/voucher_pdf',
:layout => 'layouts/pdfs/pdf.html.erb',
:header => {:right => '[page] of [topage]'},
:locals => {:#voucher => voucher}
# then save to a file
pdf = WickedPdf.new.pdf_from_string(pdf)
save_path = Rails.root.join('public', 'pdfs', dir_name, "#{voucher[:user].id.to_s}.pdf")
File.open(save_path, 'wb') do |file|
file << pdf
end
end
pdf.html.erb is the structure of the PDF
voucher_pdf is all the dynamic stuff
If this wasn't helpful, then put a comment on and I will delete it.
I have radiobuttons creaated in loop.
- #annual_packages.each do |plan|
= f.radio_button('plan_id', plan.id)
= plan.title
How can I make condition to check radio if plan.id == #plan.id
Doesn't work:
= f.radio_button('plan_id', plan.id, checked: (plan.id == #plan.id))
Loop code:
= form_for #organization, url: subscription_create_path, remote: true, method: :post do |f|
- unless #annual_packages.blank?
- #annual_packages.each do |plan|
= f.radio_button('plan_id', plan.id)
= plan.title
Can you post your form_for code?
Cause if your column plan_id have any value, it should be checked automatically if it matches the tag_value`.
If the current value of method is tag_value the radio button will be
checked.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-radio_button
If you are using simple_form or form_for, and f is your form object then you can simply do:
for form_for:
radio_button(object_name, method, tag_value, options = {}) refer
in your case:
- #annual_packages.each do |plan|
radio_button("your object name", "plan_id", plan.id, options = {})
If the current value of method(object.plan_id) is tag_value(plan) the radio button will be checked.
for simple_form:
= f.input :plan_id, as: :radio_buttons, collection: #annual_packages
How to add new fields for spree::pre_order in Spree-3.0 + Rails4 ?
Like my old customization: ==========================
app/Controllers/spree/api/pre_order_controller
class PreOrderController < Spree::Api::BaseController
def create
#pre_order = PreOrder.new(pre_order_params)
if #pre_order.save
end
respond_with(#pre_order)
end
private
def pre_order_params
params.require(:pre_order).permit(:user_id,:is_order_created)
end
end
I Got following Error
{
"exception": "param is missing or the value is empty: pre_order"
}
Please let me know your comments.