w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
  Home » RUBY ON RAILS » Page 2
Get summart information for Sunspot search paginated collection
@search = Sunspot.search(Blogpost,Employee) do fulltext 'showcase OR dan' end ###group by class ###edited answer using results########## @search_items=@search.results.flatten.compact.group_by(&:class) ##get individual models @blogs=@search_items[Blog] ##get the count @blogs_count=@search_items[Blog].count @employees_count=@search_items[Employee].count ##

Categories : Ruby On Rails

How to add attributes that are not present in the model to the object in rails?
You can simply add an attr_accessor to your model: class Cloth < ActiveRecord::Base attr_accessor :cloth_type end c = Cloth.first c.cloth_type = "whatever" c.cloth_type # => "whatever" You can also add the attribute to all records (it will not be an attribute in the traditional sense) using the select method. clothes = Cloth.select("*, 'whatever' as cloth_type") clothes.first.cloth_

Categories : Ruby On Rails

How to test that a view helper renders a partial?
Where are you testing it in? Make sure render_views is called. In any case, do you really care it's rendering that partial? What if the file name is changed, or you decide to change the implementation using html helpers instead. None of this matters to the output. I would personally assert the output instead. Depending on how complex the output is you could do it in a view test or just simple uni

Categories : Ruby On Rails

many belongs_to and has_many in single line
Unfortunately, not. AFAIK this is not possible and even the documentation doesn't seems to allow this. What you can do, is to define a metaprogramming method that iterate over an array and create this associations. But not sure if it worth it. EDIT Just an example of what I mean by metaprogramming method Create a file app/models/concerns/associate.rb module Associate extend ActiveSupport::Con

Categories : Ruby On Rails

remove the need for main_app prefix in path helpers in main app
I found an answer to my question here: http://candland.net/2012/04/17/rails-routes-used-in-an-isolated-engine/ What I needed to do was to specify a helper like this in the engine: module MyEngine module ApplicationHelper def method_missing method, *args, &block if method.to_s.end_with?('_path') or method.to_s.end_with?('_url') if main_app.respond_to?(method) mai

Categories : Ruby On Rails

Rails Internacionalization datagrid
class PatientsInnsGrid include Datagrid scope do Patient.includes(:plan) end filter(:name, :string, :header => "Nome") {|value, scope| scope.where("name ilike '%#{value}%'")} # filter(:created_at, :date, :range => true) column(:name, :header => "Nome") do |asset| format(asset.name) do |value| content_tag(:strong, value) end end column_names_filter(:header =&

Categories : Ruby On Rails

Update the database with new values after a migration
You can do it in migration: class AddDurationToSessions < ActiveRecord::Migration def change add_column :sessions, :duration, :integer # Find and update all older session record Session.find_each do |s| # new duration calc_duration = s.end_time - s.start_time # update session with new duration s.update(duration: calc_duration.to_i) end end end

Categories : Ruby On Rails

ruby solr search within object rather than in models
I'm not aware of such syntax current_user.customers.search Guess it just works, because the search method is defined on the Customer class. You should index all user_ids within Customer Index, then pass the proper with scope. class Customer has_and_belongs_to_many :users searchable do integer :user_ids, multiple: true .... end end Customer.search do with :user_ids, curre

Categories : Ruby On Rails

Javascript isn´t precompiled
For Rails 3.2 you have to tell Sprockets explicitly which additional libraries it has to precompile. #config/application.rb config.assets.precompile += %w(js/jquery.dataTables.js js/jquery.dataTables.bootstrap.js configure_sort_datatable.js) By default it is: [ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) }, /application.(css|js)$/ ] http://guides.rubyonrails.org/v3.2.13/asset_p

Categories : Ruby On Rails

Encrypted cookies in Rails 4
Why you can't simply use the session instead of custom cookies? Would be simpler in my opinion... session[:discount] = 45 User will not be able to see this information, because it is stored in the server memory. This way, you can use just the built-in, easy-to-use session cookie that rails automatically gives you.

Categories : Ruby On Rails

How to pass custom url to SOAP Action in Savon
You can add the SOAP action to your client call: response = client.call( :send_actions, message: { }, soap_action: "my_action_as_string" )

Categories : Ruby On Rails

Apache Solr - Error Creating SolrCore
I created a new core by cloning the folder collection1, then chooose the file core.properties and change name="Your folder". P/S: http://www.nopaccelerate.com/wiki/configuring-apache-solr-to-use-with-nopaccelerate/

Categories : Ruby On Rails

Ruby on rails button that will not redirect to another page but does it's action on the same view
Suppose that you want to have that button in an index page of some controller. If you add something like this (I'm using HAML here): = link_to "Something" Then clicking the link will refresh the page but calling the index method of your controller again before rendering. Now the only problem is to figure out if the index method has been called by clicking on the Something link or just by callin

Categories : Ruby On Rails

Mailer not sending emails rails 4
I have just copied my code from previous project config.action_mailer.default_url_options = { host: 'localhost:3000' } config.action_mailer.delivery_method = :smtp ActionMailer::Base.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'gmail.com', user_name: 'mailme@gmail.com', password: 'pwd'

Categories : Ruby On Rails

No route matches [PATCH] "/"
You should simply have: <%= form_for @post do |f| %> Rails will create proper action path and use proper HTTP method automatically. Your example doesn't work directly because you used wrong URL helper. You don't want to route to root path, but to posts#update action, so it should be: url: post_path(@post). You should also set @post variable to hold actual instance of Post that you wan

Categories : Ruby On Rails

Sending basic email notifications to self
Cancel the render using: def send_mail ActionMailer::Base.mail(:from => "me@example.com", :to => "myemail@gmail.com", :subject => "Contact request", :body => "test").deliver render nothing: true end

Categories : Ruby On Rails

Custom Method Route in Rails
Your routes mean that this random action will be accessible via /answers/random path. Plus your resources :answers line (the one without block) should be removed. You may find more info in Rails Guides.

Categories : Ruby On Rails

Ruby URI.enocde with Google Maps encoded polylines
Try this : require 'cgi' unescaped_url = "http://maps.googleapis.com/maps/api/staticmap?size=400x400&path=weight:3%7Ccolor:orange%7Cenc:`qdlEa`zh[?@?????@???????????@????A??@???????????????A?AAA?A@CBCBC@EDE@C@C?E@C?A??AA?AAA?A@@??@A???AA????@?@@@@BBB@BDD@BBBDD??@??@B@@@BBD@B@B?D?B?DADCFCDEBGDIBIBG@EBG@E@G?EAEAECEEIGAG?GBKHEFEJEJELEJCJCJEJEJCH?@?@???@??A?C@EBG@G@E@C?GBIDKHILGR?XBLFFFBF?JCD

Categories : Ruby On Rails

Get number of assertions in rails application
Each test will stop at the first assertion that fails. So if you have more than one assertion in a test the number will grow as the earlier assertions start to pass. The number of assertions shows is the number of assertions that were executed, not the number in the test code. There is no way that I am aware of to get the number of assertions in the test code, regardless of passing or not. Why do

Categories : Ruby On Rails

Wicked PDF silently fails on OS X
Have you done this? : create/edit file: config/initializers/wicked_pdf.rb If your wkhtmltopdf executable is not on your webserver's path, you can configure it in an initializer: WickedPdf.config = { :exe_path => '/Users/USER/*/wkhtmltopdf_darwin_386' } soure

Categories : Ruby On Rails

How do I prevent RSpec from using "Rspec.describe... " and "type: ..." in generated specs?
If you look at the template in the rspec-rails github repo you can see there is no option to do this via config variables. But you should be able to customize it by just creating a file in your lib/generators/rspec/model/templates/ that is called model_spec.rb for creating a custom generator for your models. It's just an erb template as you can see in the link above: require 'rails_helper' <

Categories : Ruby On Rails

Any option to post-process returned value in long conditional, other than setting variables for each statement?
def some_method(x) date = if x == 1 Date.today elsif x == 5 Date.today + 2 else Date.today - 2 end date + 20 end If you have 10 if statements it is probably better to refactor code using case-when like this: def some_method(x) date = case x when 1; Date.today when 5; Date.today + 2 else; Date.today - 2 end date + 20 end

Categories : Ruby On Rails

Highmaps with US States in Rails
Finally able to make it work with the gem itself, but i have to explicitly include the //= require highcharts/modules/map with the file that is available in http://www.highcharts.com/download for highmaps plugin, available in Highmaps-1.0.4/js/modules/map.src.js Check the issue in github

Categories : Ruby On Rails

Creating a Contact Us
I'm going to expand my comment here as an answer. The error that you're getting No route matches {:action=>"send_mail", :controller=>"home"} makes it pretty clear what the underlying problem is with your contact form not working. Anytime you're going to do something on the server side (like call a controller action) you need a route that maps a request to a controller and action. If you open up y

Categories : Ruby On Rails

Generate parameters in ruby on rails
I think you want this, I am assuming that both id and description are in performance_area object <%= form_tag({:performance => :update}, :method => :put) do %> <% @performance_areas.each_with_index do |performance_area, i| %> <%= hidden_field_tag :id, performance_area.id, :name => "performance_areas[#{i}][id]" %> <%= hidden_field_tag :description, performan

Categories : Ruby On Rails

Devise: Is it possible to NOT send a confirmation email in specific cases ?
So according to devise confirmable module you can skip to send confirmation and email by following code. def confirm_your_type_user_without_confirmation_email # check your condition here and process the following self.skip_confirmation! self.confirm! # condition may end here end Now lets call it on create hook. class User < ActiveRecord::Base after_create :confirm_y

Categories : Ruby On Rails

Port-forwarded Rails app in Docker seems to cause CSRF exception
From a cursory reading of the 'protect_from_forgery method', we find the following: def protect_from_forgery(options = {}) self.forgery_protection_strategy = protection_method_class(options[:with] || :null_session) self.request_forgery_protection_token ||= :authenticity_token prepend_before_action :verify_authenticity_token, options append_after_action :verify_same_origin_reque

Categories : Ruby On Rails

Nginx not serve static assets in rails 4 assets pipeline
I use something like this to serve static assets if they exist location / { try_files $uri @app; } location @app { # rails app stuff here... } Perhaps you're just missing a try_files directive like above.

Categories : Ruby On Rails

Rails: add addtional html into content_tag
Could you not make the change in css? .active { font-weight: bold; } You can certainly nest content_tag to add strong node: def nav_link(link_text, link_path, additional_class = nil) class_name = current_page?(link_path) ? 'active' : nil content_tag(:li, :class => class_name) do content_tag(:strong) do link_to link_text, link_path end end end Update: You could ch

Categories : Ruby On Rails

Group sales by the person who made them and time they were made. Rails and Postgresql
May I suggest a little tweak on your current approach? Add an orders model that groups these sales together because it makes more sense and it's a lot more "rails way" than trying to group by created_at date which might cause some bugs down the line. Here's what I suggest: order id user_id class Order < ActiveRecord::Base has_many :products belongs_to :user end sales id order_id pro

Categories : Ruby On Rails

Rails: undefined local variable or method `signup_path' for main:Object
Are you just running this in irb? If you so this needs to be in your RSpec test not just run from the console. The "get" method here is part of RSpec: https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec

Categories : Ruby On Rails

Checking for a successful charge using Stripe for rails
Yes, that's all you need to do. If the charge succeeded, Stripe will return a Charge object, and you can check its paid parameter. If the charge failed, we'd throw an exception. Cheers, Larry PS I work on Support at Stripe.

Categories : Ruby On Rails

SSH and -bash: fork: Cannot allocate memory VPS Ubuntu
Seems you have run out of memory. Many VPS servers are setup with no swap, so when you run out of memory, it will kill things off in a seemingly random manner. The easiest way to fix it is to get more memory provisioned to your VPS, likely costing more money. The next best way (other than running less stuff and memory optimizing everything running) would be to add a swap partition or swap file.

Categories : Ruby On Rails

Cant run db:migrate
What's the data store being used here out of curiosity? Is it possible to just destroy the database and start fresh by chance? If so, you can do that with: rake db:drop db:create db:migrate

Categories : Ruby On Rails

Im getting an error when trying to run a cucumber test, which manipulates a confirm dialogue using selenium-webdriver
Check the version of selenium-webdriver that you are using compared to your version of Firefox for compatibility and make the appropriate version change in your gemfile.lock file. https://selenium.googlecode.com/git/rb/CHANGES Older Firefox versions can be found here https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/

Categories : Ruby On Rails

Stripe webhook for when trial ends
When the trial period ends, there will be a customer.subscription.updated event and an invoice.created event. An hour (or so) later, you'll then either see an invoice.payment_succeeded event or an invoice.payment_failed event. From those, you'll know whether the payment went through or not. Cheers, Larry PS I work on Support at Stripe.

Categories : Ruby On Rails

Submitting form data to controller action
Check your log. In your terminal, from the window in which you started rails server, you should see a running log of the activities happening in your app. After you submit the form, you should see something like the following: Started POST "/home/send_mail: for 127.0.0.1 at xxxx-xx-xx Processing by HomeController#send_mail as xxxx Completed 200 OK in xxms This will verify a) the correct route wa

Categories : Ruby On Rails

Scope using an argument and accessing many-to-many relationship
You can't call products as a class method, because it's instance method (usage: Resource.last.products) . So that, the same applies to has_product? method: class Resource < ActiveRecord::Base has_and_belongs_to_many :products def has_product?(product) product_ids.include?(product.id) end end Usage: product = Product.first Resource.last.has_product?(product) # => true or false

Categories : Ruby On Rails

post to an update action with out an id in rails 4.1.x
You could start with something like this: # routes.rb post 'permissions_management' => 'permissions_management#update', as: 'change_permissions_management' # view form_tag(change_permissions_management_path) If it were my app, I would treat this as a singular resource, for which Rails provides out-of-the-box support. As you can see in the rake routes output, the :id param is not required fo

Categories : Ruby On Rails




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.